forked from mercari/datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
132 lines (105 loc) · 2.31 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package clouddatastore
import (
"context"
"cloud.google.com/go/datastore"
w "go.mercari.io/datastore"
)
var _ w.Key = (*keyImpl)(nil)
var _ w.PendingKey = (*pendingKeyImpl)(nil)
type keyImpl struct {
kind string
id int64
name string
parent *keyImpl
namespace string
}
type pendingKeyImpl struct {
pendingKey *datastore.PendingKey
}
type contextPendingKey struct{}
func (k *keyImpl) Kind() string {
if k == nil {
panic("k is nil")
}
return k.kind
}
func (k *keyImpl) ID() int64 {
return k.id
}
func (k *keyImpl) Name() string {
return k.name
}
func (k *keyImpl) ParentKey() w.Key {
if k.parent == nil {
return nil
}
return k.parent
}
func (k *keyImpl) Namespace() string {
return k.namespace
}
func (k *keyImpl) SetNamespace(namespace string) {
k.namespace = namespace
}
func (k *keyImpl) String() string {
return toOriginalKey(k).String()
}
func (k *keyImpl) GobEncode() ([]byte, error) {
return toOriginalKey(k).GobEncode()
}
func (k *keyImpl) GobDecode(buf []byte) error {
origKey := &datastore.Key{}
err := origKey.GobDecode(buf)
if err != nil {
return err
}
k.kind = origKey.Kind
k.id = origKey.ID
k.name = origKey.Name
k.parent = toWrapperKey(origKey.Parent)
k.namespace = origKey.Namespace
return nil
}
func (k *keyImpl) MarshalJSON() ([]byte, error) {
return toOriginalKey(k).MarshalJSON()
}
func (k *keyImpl) UnmarshalJSON(buf []byte) error {
origKey := &datastore.Key{}
err := origKey.UnmarshalJSON(buf)
if err != nil {
return err
}
k.kind = origKey.Kind
k.id = origKey.ID
k.name = origKey.Name
k.parent = toWrapperKey(origKey.Parent)
k.namespace = origKey.Namespace
return nil
}
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)
}