/
key.go
111 lines (86 loc) · 2.14 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
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) String() string {
// TODO 手で実装しなおしたほうがいいかも 互換性のため
return toOriginalKey(k).String()
}
func (k *keyImpl) GobEncode() ([]byte, error) {
// TODO 手で実装しなおしたほうがいいかも 互換性のため
return toOriginalKey(k).GobEncode()
}
func (k *keyImpl) GobDecode(buf []byte) error {
// TODO 手で実装しなおしたほうがいいかも 互換性のため
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) {
// TODO 手で実装しなおしたほうがいいかも 互換性のため
return toOriginalKey(k).MarshalJSON()
}
func (k *keyImpl) UnmarshalJSON(buf []byte) error {
// TODO 手で実装しなおしたほうがいいかも 互換性のため
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 (p *pendingKeyImpl) StoredContext() context.Context {
return context.WithValue(context.Background(), contextPendingKey{}, p)
}