Index: service/datastore/pls.go |
diff --git a/service/datastore/pls.go b/service/datastore/pls.go |
index e3bb3194fb8a8d57a85f045c4c125a02c9d21306..34115bfdaf6a9efcd748e23bb2561ba53160f494 100644 |
--- a/service/datastore/pls.go |
+++ b/service/datastore/pls.go |
@@ -8,7 +8,8 @@ import ( |
"reflect" |
) |
-// GetPLS resolves obj into a PropertyLoadSaver. |
+// GetPLS resolves obj into default struct PropertyLoadSaver and |
+// MetaGetterSetter implementation. |
// |
// obj must be a non-nil pointer to a struct of some sort. |
// |
@@ -93,7 +94,41 @@ import ( |
// methods. So if your GetMeta handles "kind", but you explicitly have a |
// $kind field, the $kind field will take precedence and your GetMeta |
// implementation will not be called for "kind". |
-func GetPLS(obj interface{}) PropertyLoadSaver { |
+// |
+// A struct overloading any of the PropertyLoadSaver or MetaGetterSetter |
+// interfaces may evoke the default struct behavior by using GetPLS on itself. |
+// For example: |
+// |
+// struct Special { |
+// Name string |
+// |
+// foo string |
+// } |
+// |
+// func (s *Special) Load(props PropertyMap) error { |
+// if foo, ok := props["foo"]; ok && len(foo) == 1 { |
+// s.foo = foo |
+// delete(props, "foo") |
+// } |
+// return GetPLS(props) |
iannucci
2015/10/30 22:02:54
I think this should be `GetPLS(s).Load(props)`
|
+// } |
+// |
+// func (s *Special) Save(withMeta bool) (PropertyMap, error) { |
+// props, err := GetPLS(withMeta) |
iannucci
2015/10/30 22:02:54
GetPLS(s).Save(withMeta)
|
+// if err != nil { |
+// return nil, err |
+// } |
+// props["foo"] = []Property{MkProperty(s.foo)} |
+// return props, nil |
+// } |
+// |
+// func (s *Special) Problem() error { |
+// return GetPLS(s).Problem() |
+// } |
+func GetPLS(obj interface{}) interface { |
+ PropertyLoadSaver |
+ MetaGetterSetter |
+} { |
v := reflect.ValueOf(obj) |
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
return &structPLS{c: &structCodec{problem: ErrInvalidEntityType}} |
@@ -106,6 +141,13 @@ func GetPLS(obj interface{}) PropertyLoadSaver { |
return &structPLS{v, c} |
} |
+func getMGS(obj interface{}) MetaGetterSetter { |
+ if mgs, ok := obj.(MetaGetterSetter); ok { |
+ return mgs |
+ } |
+ return GetPLS(obj) |
+} |
+ |
func getCodec(structType reflect.Type) *structCodec { |
structCodecsMutex.RLock() |
c, ok := structCodecs[structType] |