Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1928)

Side by Side Diff: go/src/infra/gae/libs/gae/helper/generic_key.go

Issue 1230303003: Revert "Refactor current GAE abstraction library to be free of the SDK*" (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "infra/gae/libs/gae"
10 )
11
12 // GenericDSKey is an implementation of gae.DSKey which doesn't rely on the
13 // SDK's implementation. It differs slightly in that it's not recursive (and
14 // thus cannot express some of the invalid Key's that the SDK implementation
15 // can). In particular, it's not possible to have a GenericDSKey in a namespace
16 // whose Parent() is in a different namespace.
17 //
18 // GenericDSKey also implements json.Marshaler and json.Unmarshaler so it's
19 // suitable for use in structs which need to serialize both to json and to
20 // datastore.
21 type GenericDSKey struct {
22 appID string
23 namespace string
24 toks []gae.DSKeyTok
25 }
26
27 var _ interface {
28 gae.DSKey
29 json.Marshaler
30 json.Unmarshaler
31 } = (*GenericDSKey)(nil)
32
33 // NewDSKeyToks creates a new GenericDSKey. It is the DSKey implementation
34 // returned from the various DSPropertyMap serialization routines, as well as
35 // the native key implementation for the in-memory implementation of gae.
36 func NewDSKeyToks(appID, ns string, toks []gae.DSKeyTok) *GenericDSKey {
37 newToks := make([]gae.DSKeyTok, len(toks))
38 copy(newToks, toks)
39 return &GenericDSKey{appID, ns, newToks}
40 }
41
42 // NewDSKey is a wrapper around NewDSKeyToks which has an interface similar
43 // to NewKey in the SDK.
44 func NewDSKey(appID, ns, kind, stringID string, intID int64, parent gae.DSKey) * GenericDSKey {
45 _, _, toks := DSKeySplit(parent)
46 newToks := make([]gae.DSKeyTok, len(toks))
47 copy(newToks, toks)
48 newToks = append(newToks, gae.DSKeyTok{Kind: kind, StringID: stringID, I ntID: intID})
49 return &GenericDSKey{appID, ns, newToks}
50 }
51
52 func (k *GenericDSKey) lastTok() (ret gae.DSKeyTok) {
53 if len(k.toks) > 0 {
54 ret = k.toks[len(k.toks)-1]
55 }
56 return
57 }
58
59 func (k *GenericDSKey) AppID() string { return k.appID }
60 func (k *GenericDSKey) Namespace() string { return k.namespace }
61 func (k *GenericDSKey) Kind() string { return k.lastTok().Kind }
62 func (k *GenericDSKey) StringID() string { return k.lastTok().StringID }
63 func (k *GenericDSKey) IntID() int64 { return k.lastTok().IntID }
64 func (k *GenericDSKey) String() string { return DSKeyString(k) }
65
66 func (k *GenericDSKey) MarshalJSON() ([]byte, error) {
67 return DSKeyMarshalJSON(k)
68 }
69
70 func (k *GenericDSKey) UnmarshalJSON(buf []byte) error {
71 appID, namespace, toks, err := DSKeyUnmarshalJSON(buf)
72 if err != nil {
73 return err
74 }
75 *k = *NewDSKeyToks(appID, namespace, toks)
76 return nil
77 }
78
79 func (k *GenericDSKey) Parent() gae.DSKey {
80 if len(k.toks) <= 1 {
81 return nil
82 }
83 return &GenericDSKey{k.appID, k.namespace, k.toks[:len(k.toks)-1]}
84 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/libs/gae/helper/datastore_test.go ('k') | go/src/infra/gae/libs/gae/helper/helper.infra_testing » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698