Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: service/datastore/multiarg.go

Issue 1270113002: Re-add metadata passthrough on Get operations (Closed) Base URL: https://github.com/luci/gae.git@fix_other_interfaces
Patch Set: cleanup Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/pls_impl.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 datastore 5 package datastore
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "reflect" 9 "reflect"
10 10
11 "github.com/luci/luci-go/common/errors" 11 "github.com/luci/luci-go/common/errors"
12 ) 12 )
13 13
14 type multiArgType struct { 14 type multiArgType struct {
15 valid bool 15 valid bool
16 16
17 getKey func(nk newKeyFunc, slot reflect.Value) (Key, error) 17 getKey func(nk newKeyFunc, slot reflect.Value) (Key, error)
18 getPM func(slot reflect.Value) (PropertyMap, error) 18 getPM func(slot reflect.Value) (PropertyMap, error)
19 setPM func(slot reflect.Value, pm PropertyMap) error 19 setPM func(slot reflect.Value, pm PropertyMap) error
20 setKey func(slot reflect.Value, k Key) 20 setKey func(slot reflect.Value, k Key)
21 newElem func() reflect.Value 21 newElem func() reflect.Value
22 } 22 }
23 23
24 func (mat *multiArgType) GetKeys(nk newKeyFunc, slice reflect.Value) ([]Key, err or) { 24 func (mat *multiArgType) GetKeysPMs(nk newKeyFunc, slice reflect.Value) ([]Key, []PropertyMap, error) {
25 » ret := make([]Key, slice.Len()) 25 » retKey := make([]Key, slice.Len())
26 » lme := errors.LazyMultiError{Size: len(ret)} 26 » retPM := make([]PropertyMap, slice.Len())
27 » for i := range ret { 27 » lme := errors.LazyMultiError{Size: len(retKey)}
28 » for i := range retKey {
28 key, err := mat.getKey(nk, slice.Index(i)) 29 key, err := mat.getKey(nk, slice.Index(i))
29 » » lme.Assign(i, err) 30 » » if !lme.Assign(i, err) {
30 » » ret[i] = key 31 » » » retKey[i] = key
32 » » » pm, err := mat.getPM(slice.Index(i))
33 » » » if !lme.Assign(i, err) {
34 » » » » retPM[i] = pm
35 » » » }
36 » » }
31 } 37 }
32 » return ret, lme.Get() 38 » return retKey, retPM, lme.Get()
33 }
34
35 func (mat *multiArgType) GetPMs(slice reflect.Value) ([]PropertyMap, error) {
36 » ret := make([]PropertyMap, slice.Len())
37 » lme := errors.LazyMultiError{Size: len(ret)}
38 » for i := range ret {
39 » » key, err := mat.getPM(slice.Index(i))
40 » » lme.Assign(i, err)
41 » » ret[i] = key
42 » }
43 » return ret, lme.Get()
44 } 39 }
45 40
46 // parseMultiArg checks that v has type []S, []*S, []I, []P or []*P, for some 41 // parseMultiArg checks that v has type []S, []*S, []I, []P or []*P, for some
47 // struct type S, for some interface type I, or some non-interface non-pointer 42 // struct type S, for some interface type I, or some non-interface non-pointer
48 // type P such that P or *P implements PropertyLoadSaver. 43 // type P such that P or *P implements PropertyLoadSaver.
49 func parseMultiArg(e reflect.Type) multiArgType { 44 func parseMultiArg(e reflect.Type) multiArgType {
50 if e.Kind() != reflect.Slice { 45 if e.Kind() != reflect.Slice {
51 return multiArgTypeInvalid() 46 return multiArgTypeInvalid()
52 } 47 }
53 return parseArg(e.Elem()) 48 return parseArg(e.Elem())
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 return pls.Load(pm) 222 return pls.Load(pm)
228 }, 223 },
229 setKey: func(slot reflect.Value, k Key) { 224 setKey: func(slot reflect.Value, k Key) {
230 setKey(slot.Elem().Interface(), k) 225 setKey(slot.Elem().Interface(), k)
231 }, 226 },
232 } 227 }
233 } 228 }
234 229
235 func newKeyObjErr(nk newKeyFunc, src interface{}) (Key, error) { 230 func newKeyObjErr(nk newKeyFunc, src interface{}) (Key, error) {
236 pls, name := mkPLSName(src) 231 pls, name := mkPLSName(src)
237 » if key := getMetaKey(pls, "key"); key != nil { 232 » if key, _ := pls.GetMetaDefault("key", nil).(Key); key != nil {
238 return key, nil 233 return key, nil
239 } 234 }
240 235
241 // get kind 236 // get kind
242 » kind := getMetaString(pls, "kind", name) 237 » kind := pls.GetMetaDefault("kind", name).(string)
243 if kind == "" { 238 if kind == "" {
244 return nil, fmt.Errorf("unable to extract $kind from %v", src) 239 return nil, fmt.Errorf("unable to extract $kind from %v", src)
245 } 240 }
246 241
247 // get id - allow both to be default for default keys 242 // get id - allow both to be default for default keys
248 » sid := getMetaString(pls, "id", "") 243 » sid := pls.GetMetaDefault("id", "").(string)
249 » iid := getMetaInt64(pls, "id", 0) 244 » iid := pls.GetMetaDefault("id", 0).(int64)
250 245
251 // get parent 246 // get parent
252 » par := getMetaKey(pls, "parent") 247 » par, _ := pls.GetMetaDefault("parent", nil).(Key)
253 248
254 return nk(kind, sid, iid, par), nil 249 return nk(kind, sid, iid, par), nil
255 } 250 }
256 251
257 func setKey(src interface{}, key Key) { 252 func setKey(src interface{}, key Key) {
258 pls, _ := mkPLSName(src) 253 pls, _ := mkPLSName(src)
259 if pls.SetMeta("key", key) == ErrMetaFieldUnset { 254 if pls.SetMeta("key", key) == ErrMetaFieldUnset {
260 if key.StringID() != "" { 255 if key.StringID() != "" {
261 pls.SetMeta("id", key.StringID()) 256 pls.SetMeta("id", key.StringID())
262 } else { 257 } else {
263 pls.SetMeta("id", key.IntID()) 258 pls.SetMeta("id", key.IntID())
264 } 259 }
265 pls.SetMeta("kind", key.Kind()) 260 pls.SetMeta("kind", key.Kind())
266 pls.SetMeta("parent", key.Parent()) 261 pls.SetMeta("parent", key.Parent())
267 } 262 }
268 } 263 }
269 264
270 func mkPLSName(o interface{}) (PropertyLoadSaver, string) { 265 func mkPLSName(o interface{}) (PropertyLoadSaver, string) {
271 if pls, ok := o.(*structPLS); ok { 266 if pls, ok := o.(*structPLS); ok {
272 return pls, pls.o.Type().Name() 267 return pls, pls.o.Type().Name()
273 } 268 }
274 if pls, ok := o.(PropertyLoadSaver); ok { 269 if pls, ok := o.(PropertyLoadSaver); ok {
275 return pls, "" 270 return pls, ""
276 } 271 }
277 pls := GetPLS(o) 272 pls := GetPLS(o)
278 name := pls.(*structPLS).o.Type().Name() 273 name := pls.(*structPLS).o.Type().Name()
279 return pls, name 274 return pls, name
280 } 275 }
281
282 func getMetaString(pls PropertyLoadSaver, key, dflt string) string {
283 mstr, err := pls.GetMeta(key)
284 ret, ok := mstr.(string)
285 if err != nil || !ok {
286 return dflt
287 }
288 return ret
289 }
290
291 func getMetaInt64(pls PropertyLoadSaver, key string, dflt int64) int64 {
292 mint, err := pls.GetMeta(key)
293 ret, ok := mint.(int64)
294 if err != nil || !ok {
295 return dflt
296 }
297 return ret
298 }
299
300 func getMetaKey(pls PropertyLoadSaver, key string) Key {
301 mkey, _ := pls.GetMeta(key)
302 ret, _ := mkey.(Key)
303 return ret
304 }
OLDNEW
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/pls_impl.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698