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

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

Issue 1516173002: Fix error message from KeyForObj when passing an invalid struct. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Fix GetMetaDefault silliness Created 5 years 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
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
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 return pls.Load(pm) 251 return pls.Load(pm)
252 }, 252 },
253 setKey: func(slot reflect.Value, k *Key) { 253 setKey: func(slot reflect.Value, k *Key) {
254 setKey(slot.Elem().Interface(), k) 254 setKey(slot.Elem().Interface(), k)
255 }, 255 },
256 } 256 }
257 } 257 }
258 258
259 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) { 259 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) {
260 pls := getMGS(src) 260 pls := getMGS(src)
261 » if key, _ := pls.GetMetaDefault("key", nil).(*Key); key != nil { 261 » keyI, err := GetMetaDefault(pls, "key", nil)
262 » if err != nil {
263 » » return nil, err
264 » }
265 » if key, _ := keyI.(*Key); key != nil {
262 return key, nil 266 return key, nil
263 } 267 }
264 268
265 // get kind 269 // get kind
266 » kind := pls.GetMetaDefault("kind", "").(string) 270 » kindI, err := GetMetaDefault(pls, "kind", "")
271 » if err != nil {
272 » » return nil, err
273 » }
274
275 » kind, _ := kindI.(string)
267 if kind == "" { 276 if kind == "" {
268 return nil, fmt.Errorf("unable to extract $kind from %T", src) 277 return nil, fmt.Errorf("unable to extract $kind from %T", src)
269 } 278 }
270 279
271 // get id - allow both to be default for default keys 280 // get id - allow both to be default for default keys
272 » sid := pls.GetMetaDefault("id", "").(string) 281 » sidI, err := GetMetaDefault(pls, "id", "")
273 » iid := pls.GetMetaDefault("id", 0).(int64) 282 » if err != nil {
283 » » return nil, err
284 » }
285
286 » iidI, err := GetMetaDefault(pls, "id", 0)
287 » if err != nil {
288 » » return nil, err
289 » }
274 290
275 // get parent 291 // get parent
276 » par, _ := pls.GetMetaDefault("parent", nil).(*Key) 292 » parI, err := GetMetaDefault(pls, "parent", nil)
293 » if err != nil {
294 » » return nil, err
295 » }
277 296
297 iid, _ := iidI.(int64)
298 sid, _ := sidI.(string)
299 par, _ := parI.(*Key)
278 return NewKey(aid, ns, kind, sid, iid, par), nil 300 return NewKey(aid, ns, kind, sid, iid, par), nil
279 } 301 }
280 302
281 func setKey(src interface{}, key *Key) { 303 func setKey(src interface{}, key *Key) {
282 pls := getMGS(src) 304 pls := getMGS(src)
283 if pls.SetMeta("key", key) == ErrMetaFieldUnset { 305 if pls.SetMeta("key", key) == ErrMetaFieldUnset {
284 lst := key.LastTok() 306 lst := key.LastTok()
285 if lst.StringID != "" { 307 if lst.StringID != "" {
286 _ = pls.SetMeta("id", lst.StringID) 308 _ = pls.SetMeta("id", lst.StringID)
287 } else { 309 } else {
288 _ = pls.SetMeta("id", lst.IntID) 310 _ = pls.SetMeta("id", lst.IntID)
289 } 311 }
290 _ = pls.SetMeta("kind", lst.Kind) 312 _ = pls.SetMeta("kind", lst.Kind)
291 _ = pls.SetMeta("parent", key.Parent()) 313 _ = pls.SetMeta("parent", key.Parent())
292 } 314 }
293 } 315 }
294 316
295 func mkPLS(o interface{}) PropertyLoadSaver { 317 func mkPLS(o interface{}) PropertyLoadSaver {
296 if pls, ok := o.(PropertyLoadSaver); ok { 318 if pls, ok := o.(PropertyLoadSaver); ok {
297 return pls 319 return pls
298 } 320 }
299 return GetPLS(o) 321 return GetPLS(o)
300 } 322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698