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 // error occurs, it's returned via error. If non-fatal conversion errors | 673 // error occurs, it's returned via error. If non-fatal conversion errors |
674 // occur, error will be a MultiError containing one or more ErrFieldMism
atch | 674 // occur, error will be a MultiError containing one or more ErrFieldMism
atch |
675 // objects. | 675 // objects. |
676 Load(PropertyMap) error | 676 Load(PropertyMap) error |
677 | 677 |
678 // Save returns the current property as a PropertyMap. if withMeta is tr
ue, | 678 // Save returns the current property as a PropertyMap. if withMeta is tr
ue, |
679 // then the PropertyMap contains all the metadata (e.g. '$meta' fields) | 679 // then the PropertyMap contains all the metadata (e.g. '$meta' fields) |
680 // which was held by this PropertyLoadSaver. | 680 // which was held by this PropertyLoadSaver. |
681 Save(withMeta bool) (PropertyMap, error) | 681 Save(withMeta bool) (PropertyMap, error) |
682 | 682 |
683 » MetaGetter | 683 » MetaGetterSetter |
684 | |
685 » // GetAllMeta returns a PropertyMap with all of the metadata in this | |
686 » // PropertyLoadSaver. If a metadata field has an error during serializat
ion, | |
687 » // it is skipped. | |
688 » GetAllMeta() PropertyMap | |
689 | |
690 » // SetMeta allows you to set the current value of the meta-keyed field. | |
691 » SetMeta(key string, val interface{}) error | |
692 | 684 |
693 // Problem indicates that this PLS has a fatal problem. Usually this is | 685 // Problem indicates that this PLS has a fatal problem. Usually this is |
694 // set when the underlying struct has recursion, invalid field types, ne
sted | 686 // set when the underlying struct has recursion, invalid field types, ne
sted |
695 // slices, etc. | 687 // slices, etc. |
696 Problem() error | 688 Problem() error |
697 } | 689 } |
698 | 690 |
| 691 // MetaGetterSetter is the subset of PropertyLoadSaver which pertains to |
| 692 // getting and saving metadata. |
| 693 // |
| 694 // A *struct may implement this interface to provide metadata which is |
| 695 // supplimental to the variety described by GetPLS. For example, this could be |
| 696 // used to implement a parsed-out $kind or $id. |
| 697 type MetaGetterSetter interface { |
| 698 MetaGetter |
| 699 |
| 700 // GetAllMeta returns a PropertyMap with all of the metadata in this |
| 701 // MetaGetterSetter. If a metadata field has an error during serializati
on, |
| 702 // it is skipped. |
| 703 // |
| 704 // If a *struct is implementing this, then it only needs to return the |
| 705 // metadata fields which would be returned by its GetMeta implementation
, and |
| 706 // the `GetPLS` implementation will add any statically-defined metadata |
| 707 // fields. So if GetMeta provides $id, but there's a simple tagged field
for |
| 708 // $kind, this method is only expected to return a PropertyMap with "$id
". |
| 709 GetAllMeta() PropertyMap |
| 710 |
| 711 // SetMeta allows you to set the current value of the meta-keyed field. |
| 712 SetMeta(key string, val interface{}) error |
| 713 } |
| 714 |
699 // PropertyMap represents the contents of a datastore entity in a generic way. | 715 // PropertyMap represents the contents of a datastore entity in a generic way. |
700 // It maps from property name to a list of property values which correspond to | 716 // It maps from property name to a list of property values which correspond to |
701 // that property name. It is the spiritual successor to PropertyList from the | 717 // that property name. It is the spiritual successor to PropertyList from the |
702 // original SDK. | 718 // original SDK. |
703 // | 719 // |
704 // PropertyMap may contain "meta" values, which are keyed with a '$' prefix. | 720 // PropertyMap may contain "meta" values, which are keyed with a '$' prefix. |
705 // Technically the datastore allows arbitrary property names, but all of the | 721 // Technically the datastore allows arbitrary property names, but all of the |
706 // SDKs go out of their way to try to make all property names valid programming | 722 // SDKs go out of their way to try to make all property names valid programming |
707 // language tokens. Special values must correspond to a single Property... | 723 // language tokens. Special values must correspond to a single Property... |
708 // corresponding to 0 is equivalent to unset, and corresponding to >1 is an | 724 // corresponding to 0 is equivalent to unset, and corresponding to >1 is an |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 dflt = UpconvertUnderlyingType(dflt) | 843 dflt = UpconvertUnderlyingType(dflt) |
828 cur, err := gm(key) | 844 cur, err := gm(key) |
829 if err != nil { | 845 if err != nil { |
830 return dflt | 846 return dflt |
831 } | 847 } |
832 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { | 848 if dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt) { |
833 return dflt | 849 return dflt |
834 } | 850 } |
835 return cur | 851 return cur |
836 } | 852 } |
OLD | NEW |