| 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 package datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "fmt" | 9 "fmt" |
| 10 "math" | 10 "math" |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 } | 424 } |
| 425 | 425 |
| 426 // Save implements PropertyLoadSaver.Save by returning a copy of the | 426 // Save implements PropertyLoadSaver.Save by returning a copy of the |
| 427 // current map data. | 427 // current map data. |
| 428 func (pm PropertyMap) Save(withMeta bool) (PropertyMap, error) { | 428 func (pm PropertyMap) Save(withMeta bool) (PropertyMap, error) { |
| 429 if len(pm) == 0 { | 429 if len(pm) == 0 { |
| 430 return PropertyMap{}, nil | 430 return PropertyMap{}, nil |
| 431 } | 431 } |
| 432 ret := make(PropertyMap, len(pm)) | 432 ret := make(PropertyMap, len(pm)) |
| 433 for k, v := range pm { | 433 for k, v := range pm { |
| 434 » » if withMeta || len(k) == 0 || k[0] != '$' { | 434 » » if withMeta || !isMetaKey(k) { |
| 435 ret[k] = append(ret[k], v...) | 435 ret[k] = append(ret[k], v...) |
| 436 } | 436 } |
| 437 } | 437 } |
| 438 return ret, nil | 438 return ret, nil |
| 439 } | 439 } |
| 440 | 440 |
| 441 // GetMeta implements PropertyLoadSaver.GetMeta, and returns the current value | 441 // GetMeta implements PropertyLoadSaver.GetMeta, and returns the current value |
| 442 // associated with the metadata key. It may return ErrMetaFieldUnset if the | 442 // associated with the metadata key. It may return ErrMetaFieldUnset if the |
| 443 // key doesn't exist. | 443 // key doesn't exist. |
| 444 func (pm PropertyMap) GetMeta(key string) (interface{}, error) { | 444 func (pm PropertyMap) GetMeta(key string) (interface{}, error) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 465 } | 465 } |
| 466 pm["$"+key] = []Property{prop} | 466 pm["$"+key] = []Property{prop} |
| 467 return nil | 467 return nil |
| 468 } | 468 } |
| 469 | 469 |
| 470 // Problem implements PropertyLoadSaver.Problem. It ALWAYS returns nil. | 470 // Problem implements PropertyLoadSaver.Problem. It ALWAYS returns nil. |
| 471 func (pm PropertyMap) Problem() error { | 471 func (pm PropertyMap) Problem() error { |
| 472 return nil | 472 return nil |
| 473 } | 473 } |
| 474 | 474 |
| 475 func isMetaKey(k string) bool { |
| 476 // empty counts as a metakey since it's not a valid data key, but it's |
| 477 // not really a valid metakey either. |
| 478 return k == "" || k[0] == '$' |
| 479 } |
| 480 |
| 475 // GetMetaDefaultImpl is the implementation of PropertyLoadSaver.GetMetaDefault. | 481 // GetMetaDefaultImpl is the implementation of PropertyLoadSaver.GetMetaDefault. |
| 476 // | 482 // |
| 477 // It takes the normal GetMeta function, the key and the default, and returns | 483 // It takes the normal GetMeta function, the key and the default, and returns |
| 478 // the value according to PropertyLoadSaver.GetMetaDefault. | 484 // the value according to PropertyLoadSaver.GetMetaDefault. |
| 479 func GetMetaDefaultImpl(gm func(string) (interface{}, error), key string, dflt i
nterface{}) interface{} { | 485 func GetMetaDefaultImpl(gm func(string) (interface{}, error), key string, dflt i
nterface{}) interface{} { |
| 480 dflt = UpconvertUnderlyingType(dflt) | 486 dflt = UpconvertUnderlyingType(dflt) |
| 481 cur, err := gm(key) | 487 cur, err := gm(key) |
| 482 if err != nil { | 488 if err != nil { |
| 483 return dflt | 489 return dflt |
| 484 } | 490 } |
| 485 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { | 491 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { |
| 486 return dflt | 492 return dflt |
| 487 } | 493 } |
| 488 return cur | 494 return cur |
| 489 } | 495 } |
| OLD | NEW |