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