| 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 198 } |
| 199 set(pVal) | 199 set(pVal) |
| 200 } | 200 } |
| 201 if slice.IsValid() { | 201 if slice.IsValid() { |
| 202 slice.Set(reflect.Append(slice, v)) | 202 slice.Set(reflect.Append(slice, v)) |
| 203 } | 203 } |
| 204 return "" | 204 return "" |
| 205 } | 205 } |
| 206 | 206 |
| 207 func (p *structPLS) Save(withMeta bool) (PropertyMap, error) { | 207 func (p *structPLS) Save(withMeta bool) (PropertyMap, error) { |
| 208 » size := len(p.c.byName) | 208 » ret := PropertyMap(nil) |
| 209 if withMeta { | 209 if withMeta { |
| 210 » » size += len(p.c.byMeta) | 210 » » ret = p.GetAllMeta() |
| 211 » } else { |
| 212 » » ret = make(PropertyMap, len(p.c.byName)) |
| 211 } | 213 } |
| 212 ret := make(PropertyMap, size) | |
| 213 if _, err := p.save(ret, "", ShouldIndex); err != nil { | 214 if _, err := p.save(ret, "", ShouldIndex); err != nil { |
| 214 return nil, err | 215 return nil, err |
| 215 } | 216 } |
| 216 if withMeta { | |
| 217 for k := range p.c.byMeta { | |
| 218 val, err := p.GetMeta(k) | |
| 219 if err != nil { | |
| 220 return nil, err // TODO(riannucci): should these
be ignored? | |
| 221 } | |
| 222 p := Property{} | |
| 223 if err = p.SetValue(val, NoIndex); err != nil { | |
| 224 return nil, err | |
| 225 } | |
| 226 ret["$"+k] = []Property{p} | |
| 227 } | |
| 228 if _, ok := p.c.byMeta["kind"]; !ok { | |
| 229 ret["$kind"] = []Property{MkPropertyNI(p.getDefaultKind(
))} | |
| 230 } | |
| 231 } | |
| 232 return ret, nil | 217 return ret, nil |
| 233 } | 218 } |
| 234 | 219 |
| 235 func (p *structPLS) getDefaultKind() string { | 220 func (p *structPLS) getDefaultKind() string { |
| 236 return p.o.Type().Name() | 221 return p.o.Type().Name() |
| 237 } | 222 } |
| 238 | 223 |
| 239 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { | 224 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { |
| 240 if err = p.Problem(); err != nil { | 225 if err = p.Problem(); err != nil { |
| 241 return | 226 return |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 if err := p.Problem(); err != nil { | 289 if err := p.Problem(); err != nil { |
| 305 return nil, err | 290 return nil, err |
| 306 } | 291 } |
| 307 idx, ok := p.c.byMeta[key] | 292 idx, ok := p.c.byMeta[key] |
| 308 if !ok { | 293 if !ok { |
| 309 if key == "kind" { | 294 if key == "kind" { |
| 310 return p.getDefaultKind(), nil | 295 return p.getDefaultKind(), nil |
| 311 } | 296 } |
| 312 return nil, ErrMetaFieldUnset | 297 return nil, ErrMetaFieldUnset |
| 313 } | 298 } |
| 299 return p.getMetaFor(idx), nil |
| 300 } |
| 301 |
| 302 func (p *structPLS) getMetaFor(idx int) interface{} { |
| 314 st := p.c.byIndex[idx] | 303 st := p.c.byIndex[idx] |
| 315 val := st.metaVal | 304 val := st.metaVal |
| 316 f := p.o.Field(idx) | 305 f := p.o.Field(idx) |
| 317 if st.canSet { | 306 if st.canSet { |
| 318 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { | 307 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { |
| 319 val = f.Interface() | 308 val = f.Interface() |
| 320 if bf, ok := val.(Toggle); ok { | 309 if bf, ok := val.(Toggle); ok { |
| 321 val = bf == On // true if On, otherwise false | 310 val = bf == On // true if On, otherwise false |
| 322 } | 311 } |
| 323 } | 312 } |
| 324 } | 313 } |
| 325 » return val, nil | 314 » return val |
| 315 } |
| 316 |
| 317 func (p *structPLS) GetAllMeta() PropertyMap { |
| 318 » ret := make(PropertyMap, len(p.c.byMeta)+1) |
| 319 » for k, idx := range p.c.byMeta { |
| 320 » » val := p.getMetaFor(idx) |
| 321 » » p := Property{} |
| 322 » » if err := p.SetValue(val, NoIndex); err != nil { |
| 323 » » » continue |
| 324 » » } |
| 325 » » ret["$"+k] = []Property{p} |
| 326 » } |
| 327 » if _, ok := p.c.byMeta["kind"]; !ok { |
| 328 » » ret["$kind"] = []Property{MkPropertyNI(p.getDefaultKind())} |
| 329 » } |
| 330 » return ret |
| 326 } | 331 } |
| 327 | 332 |
| 328 func (p *structPLS) GetMetaDefault(key string, def interface{}) interface{} { | 333 func (p *structPLS) GetMetaDefault(key string, def interface{}) interface{} { |
| 329 return GetMetaDefaultImpl(p.GetMeta, key, def) | 334 return GetMetaDefaultImpl(p.GetMeta, key, def) |
| 330 } | 335 } |
| 331 | 336 |
| 332 func (p *structPLS) SetMeta(key string, val interface{}) (err error) { | 337 func (p *structPLS) SetMeta(key string, val interface{}) (err error) { |
| 333 if err = p.Problem(); err != nil { | 338 if err = p.Problem(); err != nil { |
| 334 return | 339 return |
| 335 } | 340 } |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 switch val { | 573 switch val { |
| 569 case "on", "On", "true": | 574 case "on", "On", "true": |
| 570 return true, nil | 575 return true, nil |
| 571 case "off", "Off", "false": | 576 case "off", "Off", "false": |
| 572 return false, nil | 577 return false, nil |
| 573 } | 578 } |
| 574 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) | 579 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
| 575 } | 580 } |
| 576 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) | 581 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
| 577 } | 582 } |
| OLD | NEW |