Search code examples
google-app-enginegodatastore

Putting many PropertyList's into Google App Engine datastore (in Go) and loading them again with Query.GetAll


I am putting entities (as a datastore.PropertyList) into the datastore like this:

// save one
var plist datastore.PropertyList = make(datastore.PropertyList, 3)
plist = append(plist, datastore.Property { "name", "Mat", false, false })
plist = append(plist, datastore.Property { "age", "29", false, false })
plist = append(plist, datastore.Property { "location", "London", false, false })
key := datastore.NewIncompleteKey(context, "Record", nil)
datastore.Put(context, key, &plist)

// save another one
var plist datastore.PropertyList = make(datastore.PropertyList, 3)
plist = append(plist, datastore.Property { "name", "Laurie", false, false })
plist = append(plist, datastore.Property { "age", "27", false, false })
plist = append(plist, datastore.Property { "location", "London", false, false })
key := datastore.NewIncompleteKey(context, "Record", nil)
datastore.Put(context, key, &plist)

That all works fine (although the code above is more like pseudo code for now). I am able to load them individually, and the datastore.PropertyList comes out with each field as its own datastore.Property.

However, when I try to retrieve many of them using a Query, it fails:

query := datastore.NewQuery("Record")

plists := make(datastore.PropertyList, 0, 10)
keys, err := query.GetAll(context, &plists)

I get the following error:

datastore: cannot load field "age" into a "datastore.Property": no such struct field

It seems that it doesn't complain about Name because that happens to be a valid property of datastore.Property, so how do I get it to load the items as intended, with each item in plists being a datastore.PropertyList instead of datastore.Property?


Solution

  • I changed the implementation to use the PropertyLoadSaver interface - you can see it working nicely in our new Active Record style wrapper for datastore: http://github.com/matryer/gae-records (see the record.go type's Load and Save methods)