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