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