arturovm
arturovm

Reputation: 1235

How to ignore errors in datastore.Query.GetAll()?

I just started developing a GAE app with the Go runtime, so far it's been a pleasure. However, I have encountered the following setback:

I am taking advantage of the flexibility that the datastore provides by having several different structs with different properties being saved with the same entity name ("Item"). The Go language datastore reference states that "the actual types passed do not have to match between Get and Put calls or even across different App Engine requests", since entities are actually just a series of properties, and can therefore be stored in an appropriate container type that can support them.

I need to query all of the entities stored under the entity name "Item" and encode them as JSON all at once. Using that entity property flexibility to my advantage, it is possible to store queried entities into an arbitrary datastore.PropertyList, however, the Get and GetAll functions return ErrFieldMismatch as an error when a property of the queried entities cannot be properly represented (that is to say, incompatible types, or simply a missing value). All of these structs I'm saving are user generated and most values are optional, therefore saving empty values into the datastore. There are no problems while saving these structs with empty values (datastore flexibility again), but there are when retrieving them.

It is also stated in the datastore Go documentation, that it is up to the caller of the Get methods to decide if the errors returned due to empty values are ignorable, recoverable, or fatal. I would like to know how to properly do this, since just ignoring the errors won't suffice, as the destination structs (datastore.PropertyList) of my queries are not filled at all when a query results in this error.

Thank you in advance, and sorry for the lengthy question.

Update: Here is some code

query := datastore.NewQuery("Item") // here I use some Filter calls, as well as a Limit call and an Order call
items := make([]datastore.PropertyList, 0)
_, err := query.GetAll(context, &items) // context has been obviously defined before
if err != nil {
    // something to handle the error, which in my case, it's printing it and setting the server status as 500
}

Update 2: Here is some output

If I use make([]datastore.PropertyList, 0), I get this:

datastore: invalid entity type

And if I use make(datastore.PropertyList, 0), I get this:

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

And in both cases (the first one I assume can be discarded) in items I get this:

[]

Upvotes: 2

Views: 1398

Answers (1)

proppy
proppy

Reputation: 10504

According to the following post the go datastore module doesn't support PropertyList yet.

Use a pointer to a slice of datastore.Map instead.

Upvotes: 1

Related Questions