go - App Engine Datastore: How to set multiple values on a property using golang? -
i trying save multiple values single property in google's datastore using golang.
i have slice of int64 able store , retrieve. documentation can see there support implementing propertyloadsaver{} interface. cannot seem come correct implementation.
essentially, i'd accomplish:
type post struct { title string upvotes []int64 `json:"-" xml:"-" datastore:",multiple"` downvotes []int64 `json:"-" xml:"-" datastore:",multiple"` } c := appengine.newcontext(r) p := &post{ title: "name" upvotes: []int64{23, 45, 67, 89, 10} downvotes: []int64{90, 87, 65, 43, 21, 123} } k := datastore.newincompletekey(c, "post", nil) err := datastore.put(c, k, p) but without "datastore: invalid entity type" error.
multi-valued properties supported appengine default, don't need special make work. don't need implement propertyloadsaver interface, , don't need special tag value.
if struct field of slice type, automatically multi-valued property. code works:
type post struct { title string upvotes []int64 downvotes []int64 } c := appengine.newcontext(r) p := &post{ title: "name", upvotes: []int64{23, 45, 67, 89, 10}, downvotes: []int64{90, 87, 65, 43, 21, 123}, } k := datastore.newincompletekey(c, "post", nil) key, err := datastore.put(c, k, p) c.infof("result: key: %v, err: %v", key, err) of course if want can specify tag value json , xml:
type post struct { title string upvotes []int64 `json:"-" xml:"-"` downvotes []int64 `json:"-" xml:"-"` } notes:
but please note multi-valued properties not suitable store large number of values if property indexed. doing require many indices (many writes) store , modify entity, , potentially hit index limit entity (see index limits , exploding indexes more details).
so example can't use multi-valued property store hundreds of up- , downvotes post. should store votes separate/different entities linking post e.g. key of post or preferably intid.
Comments
Post a Comment