diff --git a/datastore/datastore_test.go b/datastore/datastore_test.go index ebfcfb77af68..6c1d52f87bc0 100644 --- a/datastore/datastore_test.go +++ b/datastore/datastore_test.go @@ -1821,3 +1821,26 @@ func (c *fakeDatastoreClient) AllocateIds(ctx context.Context, in *pb.AllocateId } return c.allocateIds(in) } + +func TestNewKeyFunctions(t *testing.T) { + ctx := context.Background() + parent := NewKey(ctx, "k", "", 17, nil) + + want := NewIncompleteKey(ctx, "k", parent) + got := IncompleteKey("k", parent) + if *got != *want { + t.Errorf("got %v, want %v", got, want) + } + + want = NewKey(ctx, "k", "name", 0, parent) + got = NameKey("k", "name", parent) + if *got != *want { + t.Errorf("got %v, want %v", got, want) + } + + want = NewKey(ctx, "k", "", 22, parent) + got = IDKey("k", 22, parent) + if *got != *want { + t.Errorf("got %v, want %v", got, want) + } +} diff --git a/datastore/key.go b/datastore/key.go index da8242a1370d..6164940d2f2f 100644 --- a/datastore/key.go +++ b/datastore/key.go @@ -286,3 +286,37 @@ func (c *Client) AllocateIDs(ctx context.Context, keys []*Key) ([]*Key, error) { return multiProtoToKey(resp.Keys) } + +// IncompleteKey creates a new incomplete key. +// The supplied kind cannot be empty. +// The namespace of the new key is empty. +func IncompleteKey(kind string, parent *Key) *Key { + return &Key{ + kind: kind, + parent: parent, + } +} + +// NameKey creates a new key with a name. +// The supplied kind cannot be empty. +// The supplied parent must either be a complete key or nil. +// The namespace of the new key is empty. +func NameKey(kind, name string, parent *Key) *Key { + return &Key{ + kind: kind, + name: name, + parent: parent, + } +} + +// IDKey creates a new key with an ID. +// The supplied kind cannot be empty. +// The supplied parent must either be a complete key or nil. +// The namespace of the new key is empty. +func IDKey(kind string, id int64, parent *Key) *Key { + return &Key{ + kind: kind, + id: id, + parent: parent, + } +}