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 // HEAVILY adapted from github.com/golang/appengine/datastore | 5 // HEAVILY adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package datastore | 7 package datastore |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 val, err := p.GetMeta(k) | 243 val, err := p.GetMeta(k) |
244 if err != nil { | 244 if err != nil { |
245 return nil, err // TODO(riannucci): should these
be ignored? | 245 return nil, err // TODO(riannucci): should these
be ignored? |
246 } | 246 } |
247 p := Property{} | 247 p := Property{} |
248 if err = p.SetValue(val, NoIndex); err != nil { | 248 if err = p.SetValue(val, NoIndex); err != nil { |
249 return nil, err | 249 return nil, err |
250 } | 250 } |
251 ret["$"+k] = []Property{p} | 251 ret["$"+k] = []Property{p} |
252 } | 252 } |
| 253 if _, ok := p.c.byMeta["kind"]; !ok { |
| 254 ret["$kind"] = []Property{MkProperty(p.getDefaultKind())
} |
| 255 } |
253 } | 256 } |
254 return ret, nil | 257 return ret, nil |
255 } | 258 } |
256 | 259 |
| 260 func (p *structPLS) getDefaultKind() string { |
| 261 return p.o.Type().Name() |
| 262 } |
| 263 |
257 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { | 264 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { |
258 if err = p.Problem(); err != nil { | 265 if err = p.Problem(); err != nil { |
259 return | 266 return |
260 } | 267 } |
261 | 268 |
262 saveProp := func(name string, si IndexSetting, v reflect.Value, st *stru
ctTag) (err error) { | 269 saveProp := func(name string, si IndexSetting, v reflect.Value, st *stru
ctTag) (err error) { |
263 if st.substructCodec != nil { | 270 if st.substructCodec != nil { |
264 count, err := (&structPLS{v, st.substructCodec}).save(pr
opMap, name, si) | 271 count, err := (&structPLS{v, st.substructCodec}).save(pr
opMap, name, si) |
265 if err == nil { | 272 if err == nil { |
266 idxCount += count | 273 idxCount += count |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } | 324 } |
318 return | 325 return |
319 } | 326 } |
320 | 327 |
321 func (p *structPLS) GetMeta(key string) (interface{}, error) { | 328 func (p *structPLS) GetMeta(key string) (interface{}, error) { |
322 if err := p.Problem(); err != nil { | 329 if err := p.Problem(); err != nil { |
323 return nil, err | 330 return nil, err |
324 } | 331 } |
325 idx, ok := p.c.byMeta[key] | 332 idx, ok := p.c.byMeta[key] |
326 if !ok { | 333 if !ok { |
| 334 if key == "kind" { |
| 335 return p.getDefaultKind(), nil |
| 336 } |
327 return nil, ErrMetaFieldUnset | 337 return nil, ErrMetaFieldUnset |
328 } | 338 } |
329 st := p.c.byIndex[idx] | 339 st := p.c.byIndex[idx] |
330 val := st.metaVal | 340 val := st.metaVal |
331 f := p.o.Field(idx) | 341 f := p.o.Field(idx) |
332 if st.canSet { | 342 if st.canSet { |
333 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { | 343 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { |
334 val = f.Interface() | 344 val = f.Interface() |
335 if bf, ok := val.(Toggle); ok { | 345 if bf, ok := val.(Toggle); ok { |
336 val = bf == On // true if On, otherwise false | 346 val = bf == On // true if On, otherwise false |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 switch val { | 595 switch val { |
586 case "on", "On", "true": | 596 case "on", "On", "true": |
587 return true, nil | 597 return true, nil |
588 case "off", "Off", "false": | 598 case "off", "Off", "false": |
589 return false, nil | 599 return false, nil |
590 } | 600 } |
591 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) | 601 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
592 } | 602 } |
593 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) | 603 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
594 } | 604 } |
OLD | NEW |