| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be 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 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 func (c metaMultiArgConstraints) keyOperationsOnly() bool { | 34 func (c metaMultiArgConstraints) keyOperationsOnly() bool { |
| 35 return c >= mmaKeysOnly | 35 return c >= mmaKeysOnly |
| 36 } | 36 } |
| 37 | 37 |
| 38 type multiArgType struct { | 38 type multiArgType struct { |
| 39 getMGS func(slot reflect.Value) MetaGetterSetter | 39 getMGS func(slot reflect.Value) MetaGetterSetter |
| 40 getPLS func(slot reflect.Value) PropertyLoadSaver | 40 getPLS func(slot reflect.Value) PropertyLoadSaver |
| 41 newElem func() reflect.Value | 41 newElem func() reflect.Value |
| 42 } | 42 } |
| 43 | 43 |
| 44 func (mat *multiArgType) getKey(aid, ns string, slot reflect.Value) (*Key, error
) { | 44 func (mat *multiArgType) getKey(kc KeyContext, slot reflect.Value) (*Key, error)
{ |
| 45 » return newKeyObjErr(aid, ns, mat.getMGS(slot)) | 45 » return newKeyObjErr(kc, mat.getMGS(slot)) |
| 46 } | 46 } |
| 47 | 47 |
| 48 func (mat *multiArgType) getPM(slot reflect.Value) (PropertyMap, error) { | 48 func (mat *multiArgType) getPM(slot reflect.Value) (PropertyMap, error) { |
| 49 return mat.getPLS(slot).Save(true) | 49 return mat.getPLS(slot).Save(true) |
| 50 } | 50 } |
| 51 | 51 |
| 52 func (mat *multiArgType) getMetaPM(slot reflect.Value) PropertyMap { | 52 func (mat *multiArgType) getMetaPM(slot reflect.Value) PropertyMap { |
| 53 return mat.getMGS(slot).GetAllMeta() | 53 return mat.getMGS(slot).GetAllMeta() |
| 54 } | 54 } |
| 55 | 55 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return mustParseArg(et.Elem(), true) | 230 return mustParseArg(et.Elem(), true) |
| 231 } | 231 } |
| 232 | 232 |
| 233 func mustParseArg(et reflect.Type, sliceArg bool) *multiArgType { | 233 func mustParseArg(et reflect.Type, sliceArg bool) *multiArgType { |
| 234 if mat := parseArg(et, false); mat != nil { | 234 if mat := parseArg(et, false); mat != nil { |
| 235 return mat | 235 return mat |
| 236 } | 236 } |
| 237 panic(fmt.Errorf("invalid argument type: %s is not a PLS or pointer-to-s
truct", et)) | 237 panic(fmt.Errorf("invalid argument type: %s is not a PLS or pointer-to-s
truct", et)) |
| 238 } | 238 } |
| 239 | 239 |
| 240 func newKeyObjErr(aid, ns string, mgs MetaGetterSetter) (*Key, error) { | 240 func newKeyObjErr(kc KeyContext, mgs MetaGetterSetter) (*Key, error) { |
| 241 if key, _ := GetMetaDefault(mgs, "key", nil).(*Key); key != nil { | 241 if key, _ := GetMetaDefault(mgs, "key", nil).(*Key); key != nil { |
| 242 return key, nil | 242 return key, nil |
| 243 } | 243 } |
| 244 | 244 |
| 245 // get kind | 245 // get kind |
| 246 kind := GetMetaDefault(mgs, "kind", "").(string) | 246 kind := GetMetaDefault(mgs, "kind", "").(string) |
| 247 if kind == "" { | 247 if kind == "" { |
| 248 return nil, errors.New("unable to extract $kind") | 248 return nil, errors.New("unable to extract $kind") |
| 249 } | 249 } |
| 250 | 250 |
| 251 // get id - allow both to be default for default keys | 251 // get id - allow both to be default for default keys |
| 252 sid := GetMetaDefault(mgs, "id", "").(string) | 252 sid := GetMetaDefault(mgs, "id", "").(string) |
| 253 iid := GetMetaDefault(mgs, "id", 0).(int64) | 253 iid := GetMetaDefault(mgs, "id", 0).(int64) |
| 254 | 254 |
| 255 // get parent | 255 // get parent |
| 256 par, _ := GetMetaDefault(mgs, "parent", nil).(*Key) | 256 par, _ := GetMetaDefault(mgs, "parent", nil).(*Key) |
| 257 | 257 |
| 258 » return NewKey(aid, ns, kind, sid, iid, par), nil | 258 » return kc.NewKey(kind, sid, iid, par), nil |
| 259 } | 259 } |
| 260 | 260 |
| 261 func isOKSingleType(t reflect.Type, allowKey bool) error { | 261 func isOKSingleType(t reflect.Type, allowKey bool) error { |
| 262 switch { | 262 switch { |
| 263 case t == nil: | 263 case t == nil: |
| 264 return errors.New("no type information") | 264 return errors.New("no type information") |
| 265 case t.Implements(typeOfPropertyLoadSaver): | 265 case t.Implements(typeOfPropertyLoadSaver): |
| 266 return nil | 266 return nil |
| 267 case !allowKey && t == typeOfKey: | 267 case !allowKey && t == typeOfKey: |
| 268 return errors.New("not user datatype") | 268 return errors.New("not user datatype") |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 return &mma, nil | 394 return &mma, nil |
| 395 } | 395 } |
| 396 | 396 |
| 397 func (mma *metaMultiArg) iterator(cb metaMultiArgIteratorCallback) *metaMultiArg
Iterator { | 397 func (mma *metaMultiArg) iterator(cb metaMultiArgIteratorCallback) *metaMultiArg
Iterator { |
| 398 return &metaMultiArgIterator{ | 398 return &metaMultiArgIterator{ |
| 399 metaMultiArg: mma, | 399 metaMultiArg: mma, |
| 400 cb: cb, | 400 cb: cb, |
| 401 } | 401 } |
| 402 } | 402 } |
| 403 | 403 |
| 404 // getKeysPMs returns the | 404 // getKeysPMs returns the keys and PropertyMap for the supplied argument items. |
| 405 func (mma *metaMultiArg) getKeysPMs(aid, ns string, meta bool) ([]*Key, []Proper
tyMap, error) { | 405 func (mma *metaMultiArg) getKeysPMs(kc KeyContext, meta bool) ([]*Key, []Propert
yMap, error) { |
| 406 var et errorTracker | 406 var et errorTracker |
| 407 it := mma.iterator(et.init(mma)) | 407 it := mma.iterator(et.init(mma)) |
| 408 | 408 |
| 409 // Determine our flattened keys and property maps. | 409 // Determine our flattened keys and property maps. |
| 410 retKey := make([]*Key, mma.count) | 410 retKey := make([]*Key, mma.count) |
| 411 var retPM []PropertyMap | 411 var retPM []PropertyMap |
| 412 if !mma.keysOnly { | 412 if !mma.keysOnly { |
| 413 retPM = make([]PropertyMap, mma.count) | 413 retPM = make([]PropertyMap, mma.count) |
| 414 } | 414 } |
| 415 | 415 |
| 416 for i := 0; i < mma.count; i++ { | 416 for i := 0; i < mma.count; i++ { |
| 417 it.next(func(mat *multiArgType, slot reflect.Value) error { | 417 it.next(func(mat *multiArgType, slot reflect.Value) error { |
| 418 » » » key, err := mat.getKey(aid, ns, slot) | 418 » » » key, err := mat.getKey(kc, slot) |
| 419 if err != nil { | 419 if err != nil { |
| 420 return err | 420 return err |
| 421 } | 421 } |
| 422 retKey[i] = key | 422 retKey[i] = key |
| 423 | 423 |
| 424 if !mma.keysOnly { | 424 if !mma.keysOnly { |
| 425 var pm PropertyMap | 425 var pm PropertyMap |
| 426 if meta { | 426 if meta { |
| 427 pm = mat.getMetaPM(slot) | 427 pm = mat.getMetaPM(slot) |
| 428 } else { | 428 } else { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 default: | 565 default: |
| 566 // Pass through to track as MultiError. | 566 // Pass through to track as MultiError. |
| 567 bt.errorTracker.trackError(it, err) | 567 bt.errorTracker.trackError(it, err) |
| 568 } | 568 } |
| 569 } | 569 } |
| 570 | 570 |
| 571 func (bt *boolTracker) result() *ExistsResult { | 571 func (bt *boolTracker) result() *ExistsResult { |
| 572 bt.res.updateSlices() | 572 bt.res.updateSlices() |
| 573 return &bt.res | 573 return &bt.res |
| 574 } | 574 } |
| OLD | NEW |