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 datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 | 10 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 // multiArgTypeInterface == []I | 233 // multiArgTypeInterface == []I |
234 func multiArgTypeInterface() multiArgType { | 234 func multiArgTypeInterface() multiArgType { |
235 return multiArgType{ | 235 return multiArgType{ |
236 valid: true, | 236 valid: true, |
237 | 237 |
238 getKey: func(aid, ns string, slot reflect.Value) (*Key, error) { | 238 getKey: func(aid, ns string, slot reflect.Value) (*Key, error) { |
239 return newKeyObjErr(aid, ns, slot.Elem().Interface()) | 239 return newKeyObjErr(aid, ns, slot.Elem().Interface()) |
240 }, | 240 }, |
241 getPM: func(slot reflect.Value) (PropertyMap, error) { | 241 getPM: func(slot reflect.Value) (PropertyMap, error) { |
242 » » » pls := mkPLS(slot.Elem().Interface()) | 242 » » » return mkPLS(slot.Elem().Interface()).Save(true) |
243 » » » return pls.Save(true) | |
244 }, | 243 }, |
245 getMetaPM: func(slot reflect.Value) PropertyMap { | 244 getMetaPM: func(slot reflect.Value) PropertyMap { |
246 » » » pls := getMGS(slot.Elem().Interface()) | 245 » » » return getMGS(slot.Elem().Interface()).GetAllMeta() |
247 » » » return pls.GetAllMeta() | |
248 }, | 246 }, |
249 setPM: func(slot reflect.Value, pm PropertyMap) error { | 247 setPM: func(slot reflect.Value, pm PropertyMap) error { |
250 » » » pls := mkPLS(slot.Elem().Interface()) | 248 » » » return mkPLS(slot.Elem().Interface()).Load(pm) |
251 » » » return pls.Load(pm) | |
252 }, | 249 }, |
253 setKey: func(slot reflect.Value, k *Key) { | 250 setKey: func(slot reflect.Value, k *Key) { |
254 setKey(slot.Elem().Interface(), k) | 251 setKey(slot.Elem().Interface(), k) |
255 }, | 252 }, |
256 } | 253 } |
257 } | 254 } |
258 | 255 |
259 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) { | 256 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) { |
260 pls := getMGS(src) | 257 pls := getMGS(src) |
261 » if key, _ := pls.GetMetaDefault("key", nil).(*Key); key != nil { | 258 » if key, _ := GetMetaDefault(pls, "key", nil).(*Key); key != nil { |
262 return key, nil | 259 return key, nil |
263 } | 260 } |
264 | 261 |
265 // get kind | 262 // get kind |
266 » kind := pls.GetMetaDefault("kind", "").(string) | 263 » kind := GetMetaDefault(pls, "kind", "").(string) |
267 if kind == "" { | 264 if kind == "" { |
268 return nil, fmt.Errorf("unable to extract $kind from %T", src) | 265 return nil, fmt.Errorf("unable to extract $kind from %T", src) |
269 } | 266 } |
270 | 267 |
271 // get id - allow both to be default for default keys | 268 // get id - allow both to be default for default keys |
272 » sid := pls.GetMetaDefault("id", "").(string) | 269 » sid := GetMetaDefault(pls, "id", "").(string) |
273 » iid := pls.GetMetaDefault("id", 0).(int64) | 270 » iid := GetMetaDefault(pls, "id", 0).(int64) |
274 | 271 |
275 // get parent | 272 // get parent |
276 » par, _ := pls.GetMetaDefault("parent", nil).(*Key) | 273 » par, _ := GetMetaDefault(pls, "parent", nil).(*Key) |
277 | 274 |
278 return NewKey(aid, ns, kind, sid, iid, par), nil | 275 return NewKey(aid, ns, kind, sid, iid, par), nil |
279 } | 276 } |
280 | 277 |
281 func setKey(src interface{}, key *Key) { | 278 func setKey(src interface{}, key *Key) { |
282 pls := getMGS(src) | 279 pls := getMGS(src) |
283 » if pls.SetMeta("key", key) == ErrMetaFieldUnset { | 280 » if !pls.SetMeta("key", key) { |
284 lst := key.LastTok() | 281 lst := key.LastTok() |
285 if lst.StringID != "" { | 282 if lst.StringID != "" { |
286 » » » _ = pls.SetMeta("id", lst.StringID) | 283 » » » pls.SetMeta("id", lst.StringID) |
287 } else { | 284 } else { |
288 » » » _ = pls.SetMeta("id", lst.IntID) | 285 » » » pls.SetMeta("id", lst.IntID) |
289 } | 286 } |
290 » » _ = pls.SetMeta("kind", lst.Kind) | 287 » » pls.SetMeta("kind", lst.Kind) |
291 » » _ = pls.SetMeta("parent", key.Parent()) | 288 » » pls.SetMeta("parent", key.Parent()) |
292 } | 289 } |
293 } | 290 } |
294 | 291 |
295 func mkPLS(o interface{}) PropertyLoadSaver { | 292 func mkPLS(o interface{}) PropertyLoadSaver { |
296 if pls, ok := o.(PropertyLoadSaver); ok { | 293 if pls, ok := o.(PropertyLoadSaver); ok { |
297 return pls | 294 return pls |
298 } | 295 } |
299 return GetPLS(o) | 296 return GetPLS(o) |
300 } | 297 } |
OLD | NEW |