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 rawdatastore | 7 package rawdatastore |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 idx, ok := p.c.byMeta[key] | 325 idx, ok := p.c.byMeta[key] |
326 if !ok { | 326 if !ok { |
327 return nil, ErrMetaFieldUnset | 327 return nil, ErrMetaFieldUnset |
328 } | 328 } |
329 st := p.c.byIndex[idx] | 329 st := p.c.byIndex[idx] |
330 val := st.metaVal | 330 val := st.metaVal |
331 f := p.o.Field(idx) | 331 f := p.o.Field(idx) |
332 if st.canSet { | 332 if st.canSet { |
333 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { | 333 if !reflect.DeepEqual(reflect.Zero(f.Type()).Interface(), f.Inte
rface()) { |
334 val = f.Interface() | 334 val = f.Interface() |
| 335 if bf, ok := val.(Toggle); ok { |
| 336 val = bf == On // true if On, otherwise false |
| 337 } |
335 } | 338 } |
336 } | 339 } |
337 return val, nil | 340 return val, nil |
338 } | 341 } |
339 | 342 |
340 func (p *structPLS) SetMeta(key string, val interface{}) (err error) { | 343 func (p *structPLS) SetMeta(key string, val interface{}) (err error) { |
341 if err = p.Problem(); err != nil { | 344 if err = p.Problem(); err != nil { |
342 return | 345 return |
343 } | 346 } |
344 idx, ok := p.c.byMeta[key] | 347 idx, ok := p.c.byMeta[key] |
345 if !ok { | 348 if !ok { |
346 return ErrMetaFieldUnset | 349 return ErrMetaFieldUnset |
347 } | 350 } |
348 if !p.c.byIndex[idx].canSet { | 351 if !p.c.byIndex[idx].canSet { |
349 return fmt.Errorf("gae/helper: cannot set meta %q: unexported fi
eld", key) | 352 return fmt.Errorf("gae/helper: cannot set meta %q: unexported fi
eld", key) |
350 } | 353 } |
| 354 // setting a BoolField |
| 355 if b, ok := val.(bool); ok { |
| 356 if b { |
| 357 val = On |
| 358 } else { |
| 359 val = Off |
| 360 } |
| 361 } |
351 p.o.Field(idx).Set(reflect.ValueOf(val)) | 362 p.o.Field(idx).Set(reflect.ValueOf(val)) |
352 return nil | 363 return nil |
353 } | 364 } |
354 | 365 |
355 func (p *structPLS) Problem() error { return p.c.problem } | 366 func (p *structPLS) Problem() error { return p.c.problem } |
356 | 367 |
357 var ( | 368 var ( |
358 // The RWMutex is chosen intentionally, as the majority of access to the | 369 // The RWMutex is chosen intentionally, as the majority of access to the |
359 // structCodecs map will be in parallel and will be to read an existing
codec. | 370 // structCodecs map will be in parallel and will be to read an existing
codec. |
360 // There's no reason to serialize goroutines on every | 371 // There's no reason to serialize goroutines on every |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 561 |
551 func convertMeta(val string, t reflect.Type) (interface{}, error) { | 562 func convertMeta(val string, t reflect.Type) (interface{}, error) { |
552 switch t { | 563 switch t { |
553 case typeOfString: | 564 case typeOfString: |
554 return val, nil | 565 return val, nil |
555 case typeOfInt64: | 566 case typeOfInt64: |
556 if val == "" { | 567 if val == "" { |
557 return int64(0), nil | 568 return int64(0), nil |
558 } | 569 } |
559 return strconv.ParseInt(val, 10, 64) | 570 return strconv.ParseInt(val, 10, 64) |
| 571 case typeOfToggle: |
| 572 switch val { |
| 573 case "on", "On", "true": |
| 574 return true, nil |
| 575 case "off", "Off", "false": |
| 576 return false, nil |
| 577 } |
| 578 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
560 } | 579 } |
561 » return nil, fmt.Errorf("helper: meta field with bad type/value %s/%s", t
, val) | 580 » return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
562 } | 581 } |
OLD | NEW |