Skip to content

Commit

Permalink
feat(core): add Equal and Incomplete methods to datastore.Key
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Nov 21, 2017
1 parent 14569e0 commit 5668f1b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
26 changes: 26 additions & 0 deletions aedatastore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ func (k *keyImpl) Encode() string {
return toOriginalKey(k).Encode()
}

func (k *keyImpl) Equal(o w.Key) bool {
var a w.Key = k
var b = o
for {
if a == nil && b == nil {
return true
} else if a != nil && b == nil {
return false
} else if a == nil && b != nil {
return false
}
if a.Kind() != b.Kind() || a.Name() != b.Name() || a.ID() != b.ID() || a.Namespace() != b.Namespace() {
return false
}

// NOTE Don't checking appID. align to Cloud Datastore API.

a = a.ParentKey()
b = b.ParentKey()
}
}

func (k *keyImpl) Incomplete() bool {
return k.Name() == "" && k.ID() == 0
}

func (p *pendingKeyImpl) StoredContext() context.Context {
return context.WithValue(p.ctx, contextPendingKey{}, p)
}
25 changes: 25 additions & 0 deletions clouddatastore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ func (k *keyImpl) Encode() string {
return toOriginalKey(k).Encode()
}

func (k *keyImpl) Equal(o w.Key) bool {

var a w.Key = k
var b = o
for {
if a == nil && b == nil {
return true
} else if a != nil && b == nil {
return false
} else if a == nil && b != nil {
return false
}
if a.Kind() != b.Kind() || a.Name() != b.Name() || a.ID() != b.ID() || a.Namespace() != b.Namespace() {
return false
}

a = a.ParentKey()
b = b.ParentKey()
}
}

func (k *keyImpl) Incomplete() bool {
return k.Name() == "" && k.ID() == 0
}

func (p *pendingKeyImpl) StoredContext() context.Context {
return context.WithValue(context.Background(), contextPendingKey{}, p)
}
2 changes: 2 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Key interface {
MarshalJSON() ([]byte, error)
UnmarshalJSON(buf []byte) error
Encode() string
Equal(o Key) bool
Incomplete() bool
}

type PendingKey interface {
Expand Down
92 changes: 92 additions & 0 deletions testsuite/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package testsuite

import (
"context"
"testing"

"go.mercari.io/datastore"
)

func Key_Equal(t *testing.T, ctx context.Context, client datastore.Client) {
defer func() {
err := client.Close()
if err != nil {
t.Fatal(err)
}
}()

specs := []struct {
A datastore.Key
B datastore.Key
Result bool
}{
{
client.IncompleteKey("A", nil),
client.IncompleteKey("A", nil),
true,
},
{
client.IncompleteKey("A", nil),
client.IncompleteKey("B", nil),
false,
},
{
client.IDKey("A", 1, nil),
client.IDKey("A", 1, nil),
true,
},
{
client.IDKey("A", 1, nil),
client.IDKey("A", 2, nil),
false,
},
{
client.NameKey("A", "a", nil),
client.NameKey("A", "a", nil),
true,
},
{
client.NameKey("A", "a", nil),
client.NameKey("A", "b", nil),
false,
},
{
client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
true,
},
{
client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
client.NameKey("A", "a", client.IDKey("Parent", 2, nil)),
false,
},
{
client.NameKey("A", "a", nil),
client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
false,
},
{
client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
client.NameKey("A", "a", nil),
false,
},
}

for idx, spec := range specs {
if v := spec.A.Equal(spec.B); v != spec.Result {
t.Errorf("unexpected: #%d %v", idx, v)
}
}
}

func Key_Incomplete(t *testing.T, ctx context.Context, client datastore.Client) {
if v := client.IncompleteKey("A", nil).Incomplete(); v != true {
t.Errorf("unexpected: %v", v)
}
if v := client.NameKey("A", "a", nil).Incomplete(); v != false {
t.Errorf("unexpected: %v", v)
}
if v := client.IDKey("A", 1, nil).Incomplete(); v != false {
t.Errorf("unexpected: %v", v)
}
}
2 changes: 2 additions & 0 deletions testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var TestSuite = map[string]Test{
"PutEntityType": PutEntityType,
"PutInterface": PutInterface,
"GeoPoint_PutAndGet": GeoPoint_PutAndGet,
"Key_Equal": Key_Equal,
"Key_Incomplete": Key_Incomplete,
"PLS_Basic": PLS_Basic,
"KL_Basic": KL_Basic,
"PropertyTranslater_PutAndGet": PropertyTranslater_PutAndGet,
Expand Down

0 comments on commit 5668f1b

Please sign in to comment.