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