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 "encoding/base64" | 8 "encoding/base64" |
9 "errors" | 9 "errors" |
10 "fmt" | 10 "fmt" |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 // objects. | 673 // objects. |
674 Load(PropertyMap) error | 674 Load(PropertyMap) error |
675 | 675 |
676 // Save returns the current property as a PropertyMap. if withMeta is tr
ue, | 676 // Save returns the current property as a PropertyMap. if withMeta is tr
ue, |
677 // then the PropertyMap contains all the metadata (e.g. '$meta' fields) | 677 // then the PropertyMap contains all the metadata (e.g. '$meta' fields) |
678 // which was held by this PropertyLoadSaver. | 678 // which was held by this PropertyLoadSaver. |
679 Save(withMeta bool) (PropertyMap, error) | 679 Save(withMeta bool) (PropertyMap, error) |
680 | 680 |
681 MetaGetter | 681 MetaGetter |
682 | 682 |
| 683 // GetAllMeta returns a PropertyMap with all of the metadata in this |
| 684 // PropertyLoadSaver. If a metadata field has an error during serializat
ion, |
| 685 // it is skipped. |
| 686 GetAllMeta() PropertyMap |
| 687 |
683 // SetMeta allows you to set the current value of the meta-keyed field. | 688 // SetMeta allows you to set the current value of the meta-keyed field. |
684 SetMeta(key string, val interface{}) error | 689 SetMeta(key string, val interface{}) error |
685 | 690 |
686 // Problem indicates that this PLS has a fatal problem. Usually this is | 691 // Problem indicates that this PLS has a fatal problem. Usually this is |
687 // set when the underlying struct has recursion, invalid field types, ne
sted | 692 // set when the underlying struct has recursion, invalid field types, ne
sted |
688 // slices, etc. | 693 // slices, etc. |
689 Problem() error | 694 Problem() error |
690 } | 695 } |
691 | 696 |
692 // PropertyMap represents the contents of a datastore entity in a generic way. | 697 // PropertyMap represents the contents of a datastore entity in a generic way. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 v, ok := pm["$"+key] | 751 v, ok := pm["$"+key] |
747 if !ok || len(v) == 0 { | 752 if !ok || len(v) == 0 { |
748 return nil, ErrMetaFieldUnset | 753 return nil, ErrMetaFieldUnset |
749 } | 754 } |
750 if len(v) > 1 { | 755 if len(v) > 1 { |
751 return nil, errors.New("gae: too many values for Meta key") | 756 return nil, errors.New("gae: too many values for Meta key") |
752 } | 757 } |
753 return v[0].Value(), nil | 758 return v[0].Value(), nil |
754 } | 759 } |
755 | 760 |
| 761 // GetAllMeta implements PropertyLoadSaver.GetAllMeta. |
| 762 func (pm PropertyMap) GetAllMeta() PropertyMap { |
| 763 ret := make(PropertyMap, 8) |
| 764 for k, v := range pm { |
| 765 if isMetaKey(k) { |
| 766 newV := make([]Property, len(v)) |
| 767 copy(newV, v) |
| 768 ret[k] = newV |
| 769 } |
| 770 } |
| 771 return ret |
| 772 } |
| 773 |
756 // GetMetaDefault is the implementation of PropertyLoadSaver.GetMetaDefault. | 774 // GetMetaDefault is the implementation of PropertyLoadSaver.GetMetaDefault. |
757 func (pm PropertyMap) GetMetaDefault(key string, dflt interface{}) interface{} { | 775 func (pm PropertyMap) GetMetaDefault(key string, dflt interface{}) interface{} { |
758 return GetMetaDefaultImpl(pm.GetMeta, key, dflt) | 776 return GetMetaDefaultImpl(pm.GetMeta, key, dflt) |
759 } | 777 } |
760 | 778 |
761 // SetMeta implements PropertyLoadSaver.SetMeta. It will only return an error | 779 // SetMeta implements PropertyLoadSaver.SetMeta. It will only return an error |
762 // if `val` has an invalid type (e.g. not one supported by Property). | 780 // if `val` has an invalid type (e.g. not one supported by Property). |
763 func (pm PropertyMap) SetMeta(key string, val interface{}) error { | 781 func (pm PropertyMap) SetMeta(key string, val interface{}) error { |
764 prop := Property{} | 782 prop := Property{} |
765 if err := prop.SetValue(val, NoIndex); err != nil { | 783 if err := prop.SetValue(val, NoIndex); err != nil { |
(...skipping 22 matching lines...) Expand all Loading... |
788 dflt = UpconvertUnderlyingType(dflt) | 806 dflt = UpconvertUnderlyingType(dflt) |
789 cur, err := gm(key) | 807 cur, err := gm(key) |
790 if err != nil { | 808 if err != nil { |
791 return dflt | 809 return dflt |
792 } | 810 } |
793 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { | 811 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { |
794 return dflt | 812 return dflt |
795 } | 813 } |
796 return cur | 814 return cur |
797 } | 815 } |
OLD | NEW |