go.mercari.io/datastore@v1.8.2/aedatastore/key.go (about) 1 package aedatastore 2 3 import ( 4 "context" 5 6 w "go.mercari.io/datastore" 7 "google.golang.org/appengine/datastore" 8 ) 9 10 var _ w.Key = (*keyImpl)(nil) 11 var _ w.PendingKey = (*pendingKeyImpl)(nil) 12 13 type keyImpl struct { 14 ctx context.Context 15 kind string 16 id int64 17 name string 18 parent *keyImpl 19 namespace string 20 } 21 22 type pendingKeyImpl struct { 23 ctx context.Context 24 key *datastore.Key 25 } 26 27 type contextPendingKey struct{} 28 29 func (k *keyImpl) Kind() string { 30 if k == nil { 31 panic("k is nil") 32 } 33 return k.kind 34 } 35 36 func (k *keyImpl) ID() int64 { 37 return k.id 38 } 39 40 func (k *keyImpl) Name() string { 41 return k.name 42 } 43 44 func (k *keyImpl) ParentKey() w.Key { 45 if k.parent == nil { 46 return nil 47 } 48 return k.parent 49 } 50 51 func (k *keyImpl) Namespace() string { 52 return k.namespace 53 } 54 55 func (k *keyImpl) SetNamespace(namespace string) { 56 k.namespace = namespace 57 } 58 59 func (k *keyImpl) String() string { 60 return toOriginalKey(k).String() 61 } 62 63 func (k *keyImpl) GobEncode() ([]byte, error) { 64 return toOriginalKey(k).GobEncode() 65 } 66 67 func (k *keyImpl) GobDecode(buf []byte) error { 68 origKey := &datastore.Key{} 69 err := origKey.GobDecode(buf) 70 if err != nil { 71 return err 72 } 73 74 k.kind = origKey.Kind() 75 k.id = origKey.IntID() 76 k.name = origKey.StringID() 77 k.parent = toWrapperKey(k.ctx, origKey.Parent()) 78 k.namespace = origKey.Namespace() 79 80 return nil 81 } 82 83 func (k *keyImpl) MarshalJSON() ([]byte, error) { 84 return toOriginalKey(k).MarshalJSON() 85 } 86 87 func (k *keyImpl) UnmarshalJSON(buf []byte) error { 88 origKey := &datastore.Key{} 89 err := origKey.UnmarshalJSON(buf) 90 if err != nil { 91 return err 92 } 93 94 k.kind = origKey.Kind() 95 k.id = origKey.IntID() 96 k.name = origKey.StringID() 97 k.parent = toWrapperKey(k.ctx, origKey.Parent()) 98 k.namespace = origKey.Namespace() 99 100 return nil 101 } 102 103 func (k *keyImpl) Encode() string { 104 return toOriginalKey(k).Encode() 105 } 106 107 func (k *keyImpl) Equal(o w.Key) bool { 108 var a w.Key = k 109 var b = o 110 for { 111 if a == nil && b == nil { 112 return true 113 } else if a != nil && b == nil { 114 return false 115 } else if a == nil && b != nil { 116 return false 117 } 118 if a.Kind() != b.Kind() || a.Name() != b.Name() || a.ID() != b.ID() || a.Namespace() != b.Namespace() { 119 return false 120 } 121 122 // NOTE Don't checking appID. align to Cloud Datastore API. 123 124 a = a.ParentKey() 125 b = b.ParentKey() 126 } 127 } 128 129 func (k *keyImpl) Incomplete() bool { 130 return k.Name() == "" && k.ID() == 0 131 } 132 133 func (p *pendingKeyImpl) StoredContext() context.Context { 134 return context.WithValue(p.ctx, contextPendingKey{}, p) 135 }