Skip to content

Commit bd8a5e8

Browse files
committed
datastore: add new key functions
Add the functions IncompleteKey, NameKey and IDKey. This is the minimal change that will let people start converting to avoid breakage. Examples and tests have not been updated. Change-Id: I0993ed54cc52ab642d088e6df588856f5d22b98a Reviewed-on: https://code-review.googlesource.com/8994 Reviewed-by: Ross Light <[email protected]>
1 parent 09d95d9 commit bd8a5e8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

datastore/datastore_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,3 +1821,26 @@ func (c *fakeDatastoreClient) AllocateIds(ctx context.Context, in *pb.AllocateId
18211821
}
18221822
return c.allocateIds(in)
18231823
}
1824+
1825+
func TestNewKeyFunctions(t *testing.T) {
1826+
ctx := context.Background()
1827+
parent := NewKey(ctx, "k", "", 17, nil)
1828+
1829+
want := NewIncompleteKey(ctx, "k", parent)
1830+
got := IncompleteKey("k", parent)
1831+
if *got != *want {
1832+
t.Errorf("got %v, want %v", got, want)
1833+
}
1834+
1835+
want = NewKey(ctx, "k", "name", 0, parent)
1836+
got = NameKey("k", "name", parent)
1837+
if *got != *want {
1838+
t.Errorf("got %v, want %v", got, want)
1839+
}
1840+
1841+
want = NewKey(ctx, "k", "", 22, parent)
1842+
got = IDKey("k", 22, parent)
1843+
if *got != *want {
1844+
t.Errorf("got %v, want %v", got, want)
1845+
}
1846+
}

datastore/key.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,37 @@ func (c *Client) AllocateIDs(ctx context.Context, keys []*Key) ([]*Key, error) {
286286

287287
return multiProtoToKey(resp.Keys)
288288
}
289+
290+
// IncompleteKey creates a new incomplete key.
291+
// The supplied kind cannot be empty.
292+
// The namespace of the new key is empty.
293+
func IncompleteKey(kind string, parent *Key) *Key {
294+
return &Key{
295+
kind: kind,
296+
parent: parent,
297+
}
298+
}
299+
300+
// NameKey creates a new key with a name.
301+
// The supplied kind cannot be empty.
302+
// The supplied parent must either be a complete key or nil.
303+
// The namespace of the new key is empty.
304+
func NameKey(kind, name string, parent *Key) *Key {
305+
return &Key{
306+
kind: kind,
307+
name: name,
308+
parent: parent,
309+
}
310+
}
311+
312+
// IDKey creates a new key with an ID.
313+
// The supplied kind cannot be empty.
314+
// The supplied parent must either be a complete key or nil.
315+
// The namespace of the new key is empty.
316+
func IDKey(kind string, id int64, parent *Key) *Key {
317+
return &Key{
318+
kind: kind,
319+
id: id,
320+
parent: parent,
321+
}
322+
}

0 commit comments

Comments
 (0)