OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package prod | 5 package prod |
6 | 6 |
7 import ( | 7 import ( |
8 » "github.com/luci/gae" | 8 » rds "github.com/luci/gae/service/rawdatastore" |
9 » "github.com/luci/gae/helper" | |
10 "google.golang.org/appengine/datastore" | 9 "google.golang.org/appengine/datastore" |
11 ) | 10 ) |
12 | 11 |
13 type dsKeyImpl struct { | 12 type dsKeyImpl struct { |
14 *datastore.Key | 13 *datastore.Key |
15 } | 14 } |
16 | 15 |
17 var _ gae.DSKey = dsKeyImpl{} | 16 var _ rds.Key = dsKeyImpl{} |
18 | 17 |
19 func (k dsKeyImpl) Parent() gae.DSKey { return dsR2F(k.Key.Parent()) } | 18 func (k dsKeyImpl) Parent() rds.Key { return dsR2F(k.Key.Parent()) } |
20 | 19 |
21 // dsR2F (DS real-to-fake) converts an SDK Key to a gae.DSKey | 20 // dsR2F (DS real-to-fake) converts an SDK Key to a rds.Key |
22 func dsR2F(k *datastore.Key) gae.DSKey { | 21 func dsR2F(k *datastore.Key) rds.Key { |
23 return dsKeyImpl{k} | 22 return dsKeyImpl{k} |
24 } | 23 } |
25 | 24 |
26 // dsR2FErr (DS real-to-fake with error) acts like dsR2F, but is for wrapping fu
nction | 25 // dsR2FErr (DS real-to-fake with error) acts like dsR2F, but is for wrapping fu
nction |
27 // invocations which return (*Key, error). e.g. | 26 // invocations which return (*Key, error). e.g. |
28 // | 27 // |
29 // return dsR2FErr(datastore.Put(...)) | 28 // return dsR2FErr(datastore.Put(...)) |
30 // | 29 // |
31 // instead of: | 30 // instead of: |
32 // | 31 // |
33 // k, err := datastore.Put(...) | 32 // k, err := datastore.Put(...) |
34 // if err != nil { | 33 // if err != nil { |
35 // return nil, err | 34 // return nil, err |
36 // } | 35 // } |
37 // return dsR2F(k), nil | 36 // return dsR2F(k), nil |
38 func dsR2FErr(k *datastore.Key, err error) (gae.DSKey, error) { | 37 func dsR2FErr(k *datastore.Key, err error) (rds.Key, error) { |
39 if err != nil { | 38 if err != nil { |
40 return nil, err | 39 return nil, err |
41 } | 40 } |
42 return dsR2F(k), nil | 41 return dsR2F(k), nil |
43 } | 42 } |
44 | 43 |
45 // dsF2R (DS fake-to-real) converts a DSKey back to an SDK *Key. | 44 // dsF2R (DS fake-to-real) converts a DSKey back to an SDK *Key. |
46 func dsF2R(k gae.DSKey) *datastore.Key { | 45 func dsF2R(k rds.Key) *datastore.Key { |
47 if rkey, ok := k.(dsKeyImpl); ok { | 46 if rkey, ok := k.(dsKeyImpl); ok { |
48 return rkey.Key | 47 return rkey.Key |
49 } | 48 } |
50 // we should always hit the fast case above, but just in case, safely ro
und | 49 // we should always hit the fast case above, but just in case, safely ro
und |
51 // trip through the proto encoding. | 50 // trip through the proto encoding. |
52 » rkey, err := datastore.DecodeKey(helper.DSKeyEncode(k)) | 51 » rkey, err := datastore.DecodeKey(rds.KeyEncode(k)) |
53 if err != nil { | 52 if err != nil { |
54 // should never happen in a good program, but it's not ignorable
, and | 53 // should never happen in a good program, but it's not ignorable
, and |
55 // passing an error back makes this function too cumbersome (and
it causes | 54 // passing an error back makes this function too cumbersome (and
it causes |
56 // this `if err != nil { panic(err) }` logic to show up in a bun
ch of | 55 // this `if err != nil { panic(err) }` logic to show up in a bun
ch of |
57 // places. Realistically, everything should hit the early exit c
lause above. | 56 // places. Realistically, everything should hit the early exit c
lause above. |
58 panic(err) | 57 panic(err) |
59 } | 58 } |
60 return rkey | 59 return rkey |
61 } | 60 } |
62 | 61 |
63 // dsMR2F (DS multi-real-to-fake) converts a slice of SDK keys to their wrapped | 62 // dsMR2F (DS multi-real-to-fake) converts a slice of SDK keys to their wrapped |
64 // types. | 63 // types. |
65 func dsMR2F(ks []*datastore.Key) []gae.DSKey { | 64 func dsMR2F(ks []*datastore.Key) []rds.Key { |
66 » ret := make([]gae.DSKey, len(ks)) | 65 » ret := make([]rds.Key, len(ks)) |
67 for i, k := range ks { | 66 for i, k := range ks { |
68 ret[i] = dsR2F(k) | 67 ret[i] = dsR2F(k) |
69 } | 68 } |
70 return ret | 69 return ret |
71 } | 70 } |
72 | 71 |
73 // dsMF2R (DS multi-fake-to-fake) converts a slice of wrapped keys to SDK keys. | 72 // dsMF2R (DS multi-fake-to-fake) converts a slice of wrapped keys to SDK keys. |
74 func dsMF2R(ks []gae.DSKey) []*datastore.Key { | 73 func dsMF2R(ks []rds.Key) []*datastore.Key { |
75 ret := make([]*datastore.Key, len(ks)) | 74 ret := make([]*datastore.Key, len(ks)) |
76 for i, k := range ks { | 75 for i, k := range ks { |
77 ret[i] = dsF2R(k) | 76 ret[i] = dsF2R(k) |
78 } | 77 } |
79 return ret | 78 return ret |
80 } | 79 } |
OLD | NEW |