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

Unified Diff: service/datastore/pls.go

Issue 1427933002: Decouple PLS from MGS. (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Cleaner code for test. 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
« no previous file with comments | « service/datastore/multiarg.go ('k') | service/datastore/pls_impl.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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]
« no previous file with comments | « service/datastore/multiarg.go ('k') | service/datastore/pls_impl.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698