Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Unified Diff: service/datastore/pls.go

Issue 1427933002: Decouple PLS from MGS. (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Update dox w/ example. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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)
+// }
+//
+// func (s *Special) Save(withMeta bool) (PropertyMap, error) {
+// props, err := GetPLS(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]

Powered by Google App Engine
This is Rietveld 408576698