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 » kind := pls.GetMetaDefault("kind", "").(string) |
238 if kind == "" { | 238 if kind == "" { |
239 » » return nil, fmt.Errorf("unable to extract $kind from %v", src) | 239 » » return nil, fmt.Errorf("unable to extract $kind from %T", src) |
240 } | 240 } |
241 | 241 |
242 // get id - allow both to be default for default keys | 242 // get id - allow both to be default for default keys |
243 sid := pls.GetMetaDefault("id", "").(string) | 243 sid := pls.GetMetaDefault("id", "").(string) |
244 iid := pls.GetMetaDefault("id", 0).(int64) | 244 iid := pls.GetMetaDefault("id", 0).(int64) |
245 | 245 |
246 // get parent | 246 // get parent |
247 par, _ := pls.GetMetaDefault("parent", nil).(Key) | 247 par, _ := pls.GetMetaDefault("parent", nil).(Key) |
248 | 248 |
249 return nk(kind, sid, iid, par), nil | 249 return nk(kind, sid, iid, par), nil |
250 } | 250 } |
251 | 251 |
252 func setKey(src interface{}, key Key) { | 252 func setKey(src interface{}, key Key) { |
253 » pls, _ := mkPLSName(src) | 253 » pls := mkPLS(src) |
254 if pls.SetMeta("key", key) == ErrMetaFieldUnset { | 254 if pls.SetMeta("key", key) == ErrMetaFieldUnset { |
255 if key.StringID() != "" { | 255 if key.StringID() != "" { |
256 pls.SetMeta("id", key.StringID()) | 256 pls.SetMeta("id", key.StringID()) |
257 } else { | 257 } else { |
258 pls.SetMeta("id", key.IntID()) | 258 pls.SetMeta("id", key.IntID()) |
259 } | 259 } |
260 pls.SetMeta("kind", key.Kind()) | 260 pls.SetMeta("kind", key.Kind()) |
261 pls.SetMeta("parent", key.Parent()) | 261 pls.SetMeta("parent", key.Parent()) |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 func mkPLSName(o interface{}) (PropertyLoadSaver, string) { | 265 func mkPLS(o interface{}) PropertyLoadSaver { |
266 » if pls, ok := o.(*structPLS); ok { | 266 » if pls, ok := o.(PropertyLoadSaver); ok { |
267 » » return pls, pls.o.Type().Name() | 267 » » return pls |
268 } | 268 } |
269 » if pls, ok := o.(PropertyLoadSaver); ok { | 269 » return GetPLS(o) |
270 » » return pls, "" | |
271 » } | |
272 » pls := GetPLS(o) | |
273 » name := pls.(*structPLS).o.Type().Name() | |
274 » return pls, name | |
275 } | 270 } |
OLD | NEW |