Chromium Code Reviews| 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 | 207 |
| 208 // multiArgTypeInterface == []I | 208 // multiArgTypeInterface == []I |
| 209 func multiArgTypeInterface() multiArgType { | 209 func multiArgTypeInterface() multiArgType { |
| 210 return multiArgType{ | 210 return multiArgType{ |
| 211 valid: true, | 211 valid: true, |
| 212 | 212 |
| 213 getKey: func(nk newKeyFunc, slot reflect.Value) (Key, error) { | 213 getKey: func(nk newKeyFunc, slot reflect.Value) (Key, error) { |
| 214 return newKeyObjErr(nk, slot.Elem().Interface()) | 214 return newKeyObjErr(nk, slot.Elem().Interface()) |
| 215 }, | 215 }, |
| 216 getPM: func(slot reflect.Value) (PropertyMap, error) { | 216 getPM: func(slot reflect.Value) (PropertyMap, error) { |
| 217 » » » pls, _ := mkPLSName(slot.Elem().Interface()) | 217 » » » pls := mkPLS(slot.Elem().Interface()) |
| 218 return pls.Save(true) | 218 return pls.Save(true) |
| 219 }, | 219 }, |
| 220 setPM: func(slot reflect.Value, pm PropertyMap) error { | 220 setPM: func(slot reflect.Value, pm PropertyMap) error { |
| 221 » » » pls, _ := mkPLSName(slot.Elem().Interface()) | 221 » » » pls := mkPLS(slot.Elem().Interface()) |
| 222 return pls.Load(pm) | 222 return pls.Load(pm) |
| 223 }, | 223 }, |
| 224 setKey: func(slot reflect.Value, k Key) { | 224 setKey: func(slot reflect.Value, k Key) { |
| 225 setKey(slot.Elem().Interface(), k) | 225 setKey(slot.Elem().Interface(), k) |
| 226 }, | 226 }, |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 | 229 |
| 230 func newKeyObjErr(nk newKeyFunc, src interface{}) (Key, error) { | 230 func newKeyObjErr(nk newKeyFunc, src interface{}) (Key, error) { |
| 231 » pls, name := mkPLSName(src) | 231 » pls := mkPLS(src) |
| 232 if key, _ := pls.GetMetaDefault("key", nil).(Key); key != nil { | 232 if key, _ := pls.GetMetaDefault("key", nil).(Key); key != nil { |
| 233 return key, nil | 233 return key, nil |
| 234 } | 234 } |
| 235 | 235 |
| 236 // get kind | 236 // get kind |
| 237 » kind := pls.GetMetaDefault("kind", name).(string) | 237 » kindIface, err := pls.GetMeta("kind") |
|
iannucci
2015/08/15 21:50:54
I think you could GetMetaDefault("kind", "").(stri
dnj (Google)
2015/08/16 06:11:06
Done.
| |
| 238 » if kind == "" { | 238 » if err != nil { |
| 239 » » return nil, fmt.Errorf("unable to extract $kind from %v", src) | 239 » » return nil, fmt.Errorf("unable to extract $kind from %v: %s", sr c, err) |
| 240 » } | |
| 241 » kind, ok := kindIface.(string) | |
| 242 » if !ok { | |
| 243 » » return nil, fmt.Errorf("$kind is not a string: %T", kind) | |
| 240 } | 244 } |
| 241 | 245 |
| 242 // get id - allow both to be default for default keys | 246 // get id - allow both to be default for default keys |
| 243 sid := pls.GetMetaDefault("id", "").(string) | 247 sid := pls.GetMetaDefault("id", "").(string) |
| 244 iid := pls.GetMetaDefault("id", 0).(int64) | 248 iid := pls.GetMetaDefault("id", 0).(int64) |
| 245 | 249 |
| 246 // get parent | 250 // get parent |
| 247 par, _ := pls.GetMetaDefault("parent", nil).(Key) | 251 par, _ := pls.GetMetaDefault("parent", nil).(Key) |
| 248 | 252 |
| 249 return nk(kind, sid, iid, par), nil | 253 return nk(kind, sid, iid, par), nil |
| 250 } | 254 } |
| 251 | 255 |
| 252 func setKey(src interface{}, key Key) { | 256 func setKey(src interface{}, key Key) { |
| 253 » pls, _ := mkPLSName(src) | 257 » pls := mkPLS(src) |
| 254 if pls.SetMeta("key", key) == ErrMetaFieldUnset { | 258 if pls.SetMeta("key", key) == ErrMetaFieldUnset { |
| 255 if key.StringID() != "" { | 259 if key.StringID() != "" { |
| 256 pls.SetMeta("id", key.StringID()) | 260 pls.SetMeta("id", key.StringID()) |
| 257 } else { | 261 } else { |
| 258 pls.SetMeta("id", key.IntID()) | 262 pls.SetMeta("id", key.IntID()) |
| 259 } | 263 } |
| 260 pls.SetMeta("kind", key.Kind()) | 264 pls.SetMeta("kind", key.Kind()) |
| 261 pls.SetMeta("parent", key.Parent()) | 265 pls.SetMeta("parent", key.Parent()) |
| 262 } | 266 } |
| 263 } | 267 } |
| 264 | 268 |
| 265 func mkPLSName(o interface{}) (PropertyLoadSaver, string) { | 269 func mkPLS(o interface{}) PropertyLoadSaver { |
| 266 » if pls, ok := o.(*structPLS); ok { | 270 » if pls, ok := o.(PropertyLoadSaver); ok { |
| 267 » » return pls, pls.o.Type().Name() | 271 » » return pls |
| 268 } | 272 } |
| 269 » if pls, ok := o.(PropertyLoadSaver); ok { | 273 » return GetPLS(o) |
| 270 » » return pls, "" | |
| 271 » } | |
| 272 » pls := GetPLS(o) | |
| 273 » name := pls.(*structPLS).o.Type().Name() | |
| 274 » return pls, name | |
| 275 } | 274 } |
| OLD | NEW |