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

Unified Diff: service/datastore/pls.go

Issue 1414043006: Allow metadata fields to be PropertyConverters for symmetry. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix nits 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
« no previous file with comments | « no previous file | 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 282f0834fb6336983f21edcebc7107760f4f9742..f1f54e683fc975bd293f31d8550c27708cec67d8 100644
--- a/service/datastore/pls.go
+++ b/service/datastore/pls.go
@@ -125,6 +125,36 @@ import (
// func (s *Special) Problem() error {
// return GetPLS(s).Problem()
// }
+//
+// Additionally, any field ptr-to-type may implement the PropertyConverter
+// interface to allow a single field to, for example, implement some alternate
+// encoding (json, gzip), or even just serialize to/from a simple string field.
+// This applies to normal fields, as well as metadata fields. It can be useful
+// for storing struct '$id's which have multi-field meanings. For example, the
+// Person struct below could be initialized in go as `&Person{Name{"Jane",
+// "Doe"}}`, retaining Jane's name as manipulable Go fields. However, in the
+// datastore, it would have a key of `/Person,"Jane|Doe"`, and loading the
+// struct from the datastore as part of a Query, for example, would correctly
+// populate Person.Name.First and Person.Name.Last.
+//
+// type Name struct {
+// First string
+// Last string
+// }
+//
+// func (n *Name) ToProperty() (Property, error) {
+// return fmt.Sprintf("%s|%s", n.First, n.Last)
+// }
+//
+// func (n *Name) FromProperty(p Property) error {
+// // check p to be a PTString
+// // split on "|"
+// // assign to n.First, n.Last
+// }
+//
+// type Person struct {
+// Name `gae:"$id"`
+// }
func GetPLS(obj interface{}) interface {
PropertyLoadSaver
MetaGetterSetter
« no previous file with comments | « no previous file | service/datastore/pls_impl.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698