| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be 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 24 matching lines...) Expand all Loading... |
| 35 byMeta map[string]int | 35 byMeta map[string]int |
| 36 byName map[string]int | 36 byName map[string]int |
| 37 bySpecial map[string]int | 37 bySpecial map[string]int |
| 38 | 38 |
| 39 byIndex []structTag | 39 byIndex []structTag |
| 40 hasSlice bool | 40 hasSlice bool |
| 41 problem error | 41 problem error |
| 42 } | 42 } |
| 43 | 43 |
| 44 type structPLS struct { | 44 type structPLS struct { |
| 45 » o reflect.Value | 45 » o reflect.Value |
| 46 » c *structCodec | 46 » c *structCodec |
| 47 » mgs MetaGetterSetter |
| 47 } | 48 } |
| 48 | 49 |
| 49 var _ PropertyLoadSaver = (*structPLS)(nil) | 50 var _ PropertyLoadSaver = (*structPLS)(nil) |
| 50 | 51 |
| 51 // typeMismatchReason returns a string explaining why the property p could not | 52 // typeMismatchReason returns a string explaining why the property p could not |
| 52 // be stored in an entity field of type v.Type(). | 53 // be stored in an entity field of type v.Type(). |
| 53 func typeMismatchReason(val interface{}, v reflect.Value) string { | 54 func typeMismatchReason(val interface{}, v reflect.Value) string { |
| 54 entityType := reflect.TypeOf(val) | 55 entityType := reflect.TypeOf(val) |
| 55 return fmt.Sprintf("type mismatch: %s versus %v", entityType, v.Type()) | 56 return fmt.Sprintf("type mismatch: %s versus %v", entityType, v.Type()) |
| 56 } | 57 } |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 226 } |
| 226 if slice.IsValid() { | 227 if slice.IsValid() { |
| 227 slice.Set(reflect.Append(slice, v)) | 228 slice.Set(reflect.Append(slice, v)) |
| 228 } | 229 } |
| 229 return "" | 230 return "" |
| 230 } | 231 } |
| 231 | 232 |
| 232 func (p *structPLS) Save(withMeta bool) (PropertyMap, error) { | 233 func (p *structPLS) Save(withMeta bool) (PropertyMap, error) { |
| 233 ret := PropertyMap(nil) | 234 ret := PropertyMap(nil) |
| 234 if withMeta { | 235 if withMeta { |
| 235 » » ret = getMGS(p.o.Addr().Interface()).GetAllMeta() | 236 » » if p.mgs != nil { |
| 237 » » » ret = p.mgs.GetAllMeta() |
| 238 » » } else { |
| 239 » » » ret = p.GetAllMeta() |
| 240 » » } |
| 236 } else { | 241 } else { |
| 237 ret = make(PropertyMap, len(p.c.byName)) | 242 ret = make(PropertyMap, len(p.c.byName)) |
| 238 } | 243 } |
| 239 if _, err := p.save(ret, "", ShouldIndex); err != nil { | 244 if _, err := p.save(ret, "", ShouldIndex); err != nil { |
| 240 return nil, err | 245 return nil, err |
| 241 } | 246 } |
| 242 return ret, nil | 247 return ret, nil |
| 243 } | 248 } |
| 244 | 249 |
| 245 func (p *structPLS) getDefaultKind() string { | 250 func (p *structPLS) getDefaultKind() string { |
| 246 if !p.o.IsValid() { | 251 if !p.o.IsValid() { |
| 247 return "" | 252 return "" |
| 248 } | 253 } |
| 249 return p.o.Type().Name() | 254 return p.o.Type().Name() |
| 250 } | 255 } |
| 251 | 256 |
| 252 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { | 257 func (p *structPLS) save(propMap PropertyMap, prefix string, is IndexSetting) (i
dxCount int, err error) { |
| 253 saveProp := func(name string, si IndexSetting, v reflect.Value, st *stru
ctTag) (err error) { | 258 saveProp := func(name string, si IndexSetting, v reflect.Value, st *stru
ctTag) (err error) { |
| 254 if st.substructCodec != nil { | 259 if st.substructCodec != nil { |
| 255 » » » count, err := (&structPLS{v, st.substructCodec}).save(pr
opMap, name, si) | 260 » » » count, err := (&structPLS{v, st.substructCodec, nil}).sa
ve(propMap, name, si) |
| 256 if err == nil { | 261 if err == nil { |
| 257 idxCount += count | 262 idxCount += count |
| 258 if idxCount > maxIndexedProperties { | 263 if idxCount > maxIndexedProperties { |
| 259 err = errors.New("gae: too many indexed
properties") | 264 err = errors.New("gae: too many indexed
properties") |
| 260 } | 265 } |
| 261 } | 266 } |
| 262 return err | 267 return err |
| 263 } | 268 } |
| 264 | 269 |
| 265 prop := Property{} | 270 prop := Property{} |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 switch val { | 677 switch val { |
| 673 case "on", "On", "true": | 678 case "on", "On", "true": |
| 674 return true, nil | 679 return true, nil |
| 675 case "off", "Off", "false": | 680 case "off", "Off", "false": |
| 676 return false, nil | 681 return false, nil |
| 677 } | 682 } |
| 678 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) | 683 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
| 679 } | 684 } |
| 680 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) | 685 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
| 681 } | 686 } |
| OLD | NEW |