Put a lot of PropertyList in the Google App Engine data store (in Go) and load them again using Query.GetAll

I put objects (like datastore.PropertyList ) in the data store as follows:

 // 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) 

Everything works fine (although the code above is more like pseudo code). I can load them individually, and datastore.PropertyList comes out with each field as its own datastore.Property .

However, when I try to extract many of them using 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 he does not complain about Name , because this is a valid property of datastore.Property , so how do I load it as expected, with each element in plists being a datastore.PropertyList instead of datastore.Property ?

+4
source share
3 answers

I changed the implementation to use the PropertyLoadSaver interface - you can see that it works great in our new Active Record style wrapper for the data store: http://github.com/matryer/gae-records (see the record.go methods of type Load and Save )

+3
source

GetAll launches the request in this context and returns all the keys corresponding to this request, and also adds values ​​to dst. Dst should be a pointer to a slice of structures, pointers to a structure, or maps. If q is a key-only request, GetAll ignores dst and returns the keys.

And according to the following post , the go datastore module does not yet support the PropertyList.

Use a pointer to a piece of data storage. Instead of him.

Also note that you need to make([]T, n) call to make a fragment of T , not make(T, n)

+2
source

Source: https://habr.com/ru/post/1391898/


All Articles