| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package datastore | |
| 6 | |
| 7 import ( | |
| 8 "encoding/json" | |
| 9 ) | |
| 10 | |
| 11 // GenericKey is an implementation of Key which doesn't rely on the | |
| 12 // SDK's implementation. It differs slightly in that it's not recursive (and | |
| 13 // thus cannot express some of the invalid Key's that the SDK implementation | |
| 14 // can). In particular, it's not possible to have a GenericKey in a namespace | |
| 15 // whose Parent() is in a different namespace. | |
| 16 // | |
| 17 // GenericKey also implements json.Marshaler and json.Unmarshaler so it's | |
| 18 // suitable for use in structs which need to serialize both to json and to | |
| 19 // datastore. | |
| 20 type GenericKey struct { | |
| 21 appID string | |
| 22 namespace string | |
| 23 toks []KeyTok | |
| 24 } | |
| 25 | |
| 26 var _ interface { | |
| 27 Key | |
| 28 json.Marshaler | |
| 29 json.Unmarshaler | |
| 30 } = (*GenericKey)(nil) | |
| 31 | |
| 32 // NewKeyToks creates a new GenericKey. It is the Key implementation | |
| 33 // returned from the various PropertyMap serialization routines, as well as | |
| 34 // the native key implementation for the in-memory implementation of gae. | |
| 35 func NewKeyToks(appID, ns string, toks []KeyTok) *GenericKey { | |
| 36 newToks := make([]KeyTok, len(toks)) | |
| 37 copy(newToks, toks) | |
| 38 return &GenericKey{appID, ns, newToks} | |
| 39 } | |
| 40 | |
| 41 // NewKey is a wrapper around NewKeyToks which has an interface similar | |
| 42 // to NewKey in the SDK. | |
| 43 func NewKey(appID, ns, kind, stringID string, intID int64, parent Key) *GenericK
ey { | |
| 44 _, _, toks := KeySplit(parent) | |
| 45 newToks := make([]KeyTok, len(toks)) | |
| 46 copy(newToks, toks) | |
| 47 newToks = append(newToks, KeyTok{Kind: kind, StringID: stringID, IntID:
intID}) | |
| 48 return &GenericKey{appID, ns, newToks} | |
| 49 } | |
| 50 | |
| 51 // NewKeyFromEncoded decodes and returns a *GenericKey | |
| 52 func NewKeyFromEncoded(encoded string) (ret *GenericKey, err error) { | |
| 53 ret = &GenericKey{} | |
| 54 ret.appID, ret.namespace, ret.toks, err = KeyToksDecode(encoded) | |
| 55 return | |
| 56 } | |
| 57 | |
| 58 func (k *GenericKey) lastTok() (ret KeyTok) { | |
| 59 if len(k.toks) > 0 { | |
| 60 ret = k.toks[len(k.toks)-1] | |
| 61 } | |
| 62 return | |
| 63 } | |
| 64 | |
| 65 // AppID returns the application ID that this Key is for. | |
| 66 func (k *GenericKey) AppID() string { return k.appID } | |
| 67 | |
| 68 // Namespace returns the namespace that this Key is for. | |
| 69 func (k *GenericKey) Namespace() string { return k.namespace } | |
| 70 | |
| 71 // Kind returns the datastore kind of the entity. | |
| 72 func (k *GenericKey) Kind() string { return k.lastTok().Kind } | |
| 73 | |
| 74 // StringID returns the string ID of the entity (if defined, otherwise ""). | |
| 75 func (k *GenericKey) StringID() string { return k.lastTok().StringID } | |
| 76 | |
| 77 // IntID returns the int64 ID of the entity (if defined, otherwise 0). | |
| 78 func (k *GenericKey) IntID() int64 { return k.lastTok().IntID } | |
| 79 | |
| 80 // String returns a human-readable version of this Key. | |
| 81 func (k *GenericKey) String() string { return KeyString(k) } | |
| 82 | |
| 83 // Parent returns the parent Key of this *GenericKey, or nil. The parent | |
| 84 // will always have the concrete type of *GenericKey. | |
| 85 func (k *GenericKey) Parent() Key { | |
| 86 if len(k.toks) <= 1 { | |
| 87 return nil | |
| 88 } | |
| 89 return &GenericKey{k.appID, k.namespace, k.toks[:len(k.toks)-1]} | |
| 90 } | |
| 91 | |
| 92 // MarshalJSON allows this key to be automatically marshaled by encoding/json. | |
| 93 func (k *GenericKey) MarshalJSON() ([]byte, error) { | |
| 94 return KeyMarshalJSON(k) | |
| 95 } | |
| 96 | |
| 97 // UnmarshalJSON allows this key to be automatically unmarshaled by encoding/jso
n. | |
| 98 func (k *GenericKey) UnmarshalJSON(buf []byte) error { | |
| 99 appID, namespace, toks, err := KeyUnmarshalJSON(buf) | |
| 100 if err != nil { | |
| 101 return err | |
| 102 } | |
| 103 *k = *NewKeyToks(appID, namespace, toks) | |
| 104 return nil | |
| 105 } | |
| OLD | NEW |