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 306 matching lines...) Loading... |
317 if err != nil { | 317 if err != nil { |
318 return nil, false | 318 return nil, false |
319 } | 319 } |
320 return prop.value, true | 320 return prop.value, true |
321 } | 321 } |
322 | 322 |
323 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { | 323 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { |
324 val = f.Interface() | 324 val = f.Interface() |
325 if bf, ok := val.(Toggle); ok { | 325 if bf, ok := val.(Toggle); ok { |
326 val = bf == On // true if On, otherwise false | 326 val = bf == On // true if On, otherwise false |
| 327 } else { |
| 328 val = UpconvertUnderlyingType(val) |
327 } | 329 } |
328 } | 330 } |
329 } | 331 } |
330 return val, true | 332 return val, true |
331 } | 333 } |
332 | 334 |
333 func (p *structPLS) GetAllMeta() PropertyMap { | 335 func (p *structPLS) GetAllMeta() PropertyMap { |
334 needKind := true | 336 needKind := true |
335 ret := make(PropertyMap, len(p.c.byMeta)+1) | 337 ret := make(PropertyMap, len(p.c.byMeta)+1) |
336 for k, idx := range p.c.byMeta { | 338 for k, idx := range p.c.byMeta { |
(...skipping 39 matching lines...) Loading... |
376 if b { | 378 if b { |
377 val = On | 379 val = On |
378 } else { | 380 } else { |
379 val = Off | 381 val = Off |
380 } | 382 } |
381 } | 383 } |
382 f := p.o.Field(idx) | 384 f := p.o.Field(idx) |
383 if val == nil { | 385 if val == nil { |
384 f.Set(reflect.Zero(f.Type())) | 386 f.Set(reflect.Zero(f.Type())) |
385 } else { | 387 } else { |
386 » » f.Set(reflect.ValueOf(val)) | 388 » » value := reflect.ValueOf(val) |
| 389 » » f.Set(value.Convert(f.Type())) |
387 } | 390 } |
388 return nil | 391 return nil |
389 } | 392 } |
390 | 393 |
391 func (p *structPLS) Problem() error { return p.c.problem } | 394 func (p *structPLS) Problem() error { return p.c.problem } |
392 | 395 |
393 var ( | 396 var ( |
394 // The RWMutex is chosen intentionally, as the majority of access to the | 397 // The RWMutex is chosen intentionally, as the majority of access to the |
395 // structCodecs map will be in parallel and will be to read an existing
codec. | 398 // structCodecs map will be in parallel and will be to read an existing
codec. |
396 // There's no reason to serialize goroutines on every | 399 // There's no reason to serialize goroutines on every |
(...skipping 180 matching lines...) Loading... |
577 st.idxSetting = NoIndex | 580 st.idxSetting = NoIndex |
578 } | 581 } |
579 } | 582 } |
580 if c.problem == errRecursiveStruct { | 583 if c.problem == errRecursiveStruct { |
581 c.problem = nil | 584 c.problem = nil |
582 } | 585 } |
583 return | 586 return |
584 } | 587 } |
585 | 588 |
586 func convertMeta(val string, t reflect.Type) (interface{}, error) { | 589 func convertMeta(val string, t reflect.Type) (interface{}, error) { |
| 590 switch t.Kind() { |
| 591 case reflect.String: |
| 592 return val, nil |
| 593 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.In
t64: |
| 594 if val == "" { |
| 595 return int64(0), nil |
| 596 } |
| 597 return strconv.ParseInt(val, 10, 64) |
| 598 } |
587 switch t { | 599 switch t { |
588 case typeOfString: | |
589 return val, nil | |
590 case typeOfKey: | 600 case typeOfKey: |
591 if val != "" { | 601 if val != "" { |
592 return nil, fmt.Errorf("key field is not allowed to have
a default: %q", val) | 602 return nil, fmt.Errorf("key field is not allowed to have
a default: %q", val) |
593 } | 603 } |
594 return nil, nil | 604 return nil, nil |
595 case typeOfInt64: | |
596 if val == "" { | |
597 return int64(0), nil | |
598 } | |
599 return strconv.ParseInt(val, 10, 64) | |
600 case typeOfToggle: | 605 case typeOfToggle: |
601 switch val { | 606 switch val { |
602 case "on", "On", "true": | 607 case "on", "On", "true": |
603 return true, nil | 608 return true, nil |
604 case "off", "Off", "false": | 609 case "off", "Off", "false": |
605 return false, nil | 610 return false, nil |
606 } | 611 } |
607 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) | 612 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
608 } | 613 } |
609 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) | 614 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
610 } | 615 } |
OLD | NEW |