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

Unified Diff: service/datastore/pls_test.go

Issue 1414043006: Allow metadata fields to be PropertyConverters for symmetry. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 1 month 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
« service/datastore/pls_impl.go ('K') | « service/datastore/pls_impl.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/datastore/pls_test.go
diff --git a/service/datastore/pls_test.go b/service/datastore/pls_test.go
index bc8499ebf46915f52bb4af1749080b66a2335c5d..322107e8521bf212169cbd88a78a6aa8fec797de 100644
--- a/service/datastore/pls_test.go
+++ b/service/datastore/pls_test.go
@@ -641,6 +641,39 @@ func (i *KindOverride) SetMeta(key string, value interface{}) error {
return ErrMetaFieldUnset
}
+type EmbeddedID struct {
+ Thing string
+ Val int
+}
+
+var _ PropertyConverter = (*EmbeddedID)(nil)
+
+func (e *EmbeddedID) ToProperty() (ret Property, err error) {
+ return mpNI(fmt.Sprintf("%s|%d", e.Thing, e.Val)), nil
+}
+
+func (e *EmbeddedID) FromProperty(val Property) error {
+ if val.Type() != PTString {
+ return fmt.Errorf("gotta have a string")
+ }
+ toks := strings.SplitN(val.Value().(string), "|", 2)
+ if len(toks) != 2 {
+ return fmt.Errorf("gotta have two parts")
+ }
+ v, err := strconv.Atoi(toks[1])
+ if err != nil {
+ return err
+ }
+
+ e.Thing = toks[0]
+ e.Val = v
+ return nil
+}
+
+type IDEmbedder struct {
+ EmbeddedID `gae:"$id"`
+}
+
type Simple struct{}
type testCase struct {
@@ -1910,5 +1943,21 @@ func TestMeta(t *testing.T) {
"$kind": {mpNI("wut")},
})
})
+
+ Convey("Embeddable Metadata structs", func() {
+ ide := &IDEmbedder{EmbeddedID{"hello", 10}}
+ pls := GetPLS(ide)
+ val, err := pls.GetMeta("id")
+ So(err, ShouldBeNil)
+ So(val, ShouldEqual, "hello|10")
+
+ So(pls.SetMeta("id", "sup|1337"), ShouldBeNil)
+ So(ide.EmbeddedID, ShouldResemble, EmbeddedID{"sup", 1337})
+
+ So(pls.GetAllMeta(), ShouldResembleV, PropertyMap{
+ "$id": {mpNI("sup|1337")},
+ "$kind": {mpNI("IDEmbedder")},
+ })
+ })
})
}
« service/datastore/pls_impl.go ('K') | « service/datastore/pls_impl.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698