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 ds "github.com/luci/gae/service/datastore" | 8 ds "github.com/luci/gae/service/datastore" |
9 "github.com/luci/gae/service/datastore/dskey" | 9 "github.com/luci/gae/service/datastore/dskey" |
10 "google.golang.org/appengine/datastore" | 10 "google.golang.org/appengine/datastore" |
11 ) | 11 ) |
12 | 12 |
13 type dsKeyImpl struct { | 13 type dsKeyImpl struct { |
14 *datastore.Key | 14 *datastore.Key |
15 } | 15 } |
16 | 16 |
17 var _ ds.Key = dsKeyImpl{} | 17 var _ ds.Key = dsKeyImpl{} |
18 | 18 |
19 func (k dsKeyImpl) Parent() ds.Key { return dsR2F(k.Key.Parent()) } | 19 func (k dsKeyImpl) Parent() ds.Key { return dsR2F(k.Key.Parent()) } |
20 func (k dsKeyImpl) Incomplete() bool { return k.Key.Incomplete() } | 20 func (k dsKeyImpl) Incomplete() bool { return k.Key.Incomplete() } |
21 func (k dsKeyImpl) Valid(allowSpecial bool, aid, ns string) bool { | 21 func (k dsKeyImpl) Valid(allowSpecial bool, aid, ns string) bool { |
22 return dskey.Valid(k, allowSpecial, aid, ns) | 22 return dskey.Valid(k, allowSpecial, aid, ns) |
23 } | 23 } |
24 func (k dsKeyImpl) PartialValid(aid, ns string) bool { | 24 func (k dsKeyImpl) PartialValid(aid, ns string) bool { |
25 return dskey.PartialValid(k, aid, ns) | 25 return dskey.PartialValid(k, aid, ns) |
26 } | 26 } |
27 | 27 |
28 // dsR2F (DS real-to-fake) converts an SDK Key to a ds.Key | 28 // dsR2F (DS real-to-fake) converts an SDK Key to a ds.Key |
29 func dsR2F(k *datastore.Key) ds.Key { | 29 func dsR2F(k *datastore.Key) ds.Key { |
| 30 if k == nil { |
| 31 return nil |
| 32 } |
30 return dsKeyImpl{k} | 33 return dsKeyImpl{k} |
31 } | 34 } |
32 | 35 |
33 // dsF2R (DS fake-to-real) converts a DSKey back to an SDK *Key. | 36 // dsF2R (DS fake-to-real) converts a DSKey back to an SDK *Key. |
34 func dsF2R(k ds.Key) *datastore.Key { | 37 func dsF2R(k ds.Key) *datastore.Key { |
| 38 if k == nil { |
| 39 return nil |
| 40 } |
35 if rkey, ok := k.(dsKeyImpl); ok { | 41 if rkey, ok := k.(dsKeyImpl); ok { |
36 return rkey.Key | 42 return rkey.Key |
37 } | 43 } |
38 // we should always hit the fast case above, but just in case, safely ro
und | 44 // we should always hit the fast case above, but just in case, safely ro
und |
39 // trip through the proto encoding. | 45 // trip through the proto encoding. |
40 rkey, err := datastore.DecodeKey(dskey.Encode(k)) | 46 rkey, err := datastore.DecodeKey(dskey.Encode(k)) |
41 if err != nil { | 47 if err != nil { |
42 // should never happen in a good program, but it's not ignorable
, and | 48 // should never happen in a good program, but it's not ignorable
, and |
43 // passing an error back makes this function too cumbersome (and
it causes | 49 // passing an error back makes this function too cumbersome (and
it causes |
44 // this `if err != nil { panic(err) }` logic to show up in a bun
ch of | 50 // this `if err != nil { panic(err) }` logic to show up in a bun
ch of |
45 // places. Realistically, everything should hit the early exit c
lause above. | 51 // places. Realistically, everything should hit the early exit c
lause above. |
46 panic(err) | 52 panic(err) |
47 } | 53 } |
48 return rkey | 54 return rkey |
49 } | 55 } |
50 | 56 |
51 // dsMF2R (DS multi-fake-to-fake) converts a slice of wrapped keys to SDK keys. | 57 // dsMF2R (DS multi-fake-to-fake) converts a slice of wrapped keys to SDK keys. |
52 func dsMF2R(ks []ds.Key) []*datastore.Key { | 58 func dsMF2R(ks []ds.Key) []*datastore.Key { |
53 ret := make([]*datastore.Key, len(ks)) | 59 ret := make([]*datastore.Key, len(ks)) |
54 for i, k := range ks { | 60 for i, k := range ks { |
55 ret[i] = dsF2R(k) | 61 ret[i] = dsF2R(k) |
56 } | 62 } |
57 return ret | 63 return ret |
58 } | 64 } |
OLD | NEW |