omitempty fields don't clear struct if they are empty

Skip to first unread message

dir Ls

May 14, 2019, 10:25:50 PM5/14/19
to google-appengine-go
I have a few fields in my struct that have been set to omitempty for datastore tag. The save works as expected. However, if I have a struct with an existing value in any of these fields and I do a Get, it doesn't clear these fields that don't exist in the datastore. Is this the expected behavior? In most cases it is fine and not a worry but when you do RunInTransaction and the logic ends up getting called multiple times due to retry, any partially computed values will still exist even though we are re-reading the entities. This will break the idempotency requirement of the transaction code. Or am I missing something?

Prateek Malhotra

May 15, 2019, 11:45:20 AM5/15/19
to google-appengine-go
I believe omitempty only applies when writing to datastore, it plays no part when reading from datastore.

I think if you have a situation where you update these fields during a transaction, you can probably solve the problem you described by:
  • Ensuring whatever calculations you are doing are idempotent OR
  • Allocate the structs within the transaction, not outside of it
Reply all
Reply to author
0 new messages