mjibson/goon — pull request #4 by xStrom (2013-04-04 12:29:39+00:00)
Title: New feature, to update entity structs with datastore key info if requested

We're treading uncharted waters now, how exciting! This isn't a bug fix like my previous pull requests, but **a feature addition**.

In terms of GAE, I come from a Java background, and more specifically I used **Objectify** for datastore/caching. Objectify is heavily based on Python's NDB, which I belive is also your source of inspiration. Having used Objectify for years, I've become accustomed to doing things a certain way. The vanilla Go datastore API is lacking, I think we both agree on that. That brought me to goon, which is a big step up, but still falls short, e.g. it requires me to write way too much boilerplate. As I've noted previously, I intend to improve goon to better fit my convenience. However I think it would be more beneficial if I wouldn't run off with a complete fork. That said, I am unsure how compatible my ideas are with yours. To help gauge that, I'm opening this discussion with a relatively small feature addition that is fully API-compatible with your master branch.

So, about the feature itself. I find that I don't want to pass around the ```goon.Entity``` type in my application logic, however I do need access to the entity key info. Thus I have devised a voluntary way to have the key info updated in the entity struct.

Time for some code! Lets define our data structures.

type User struct {
Id int64 `datastore:"-" goon:"id"`
Name string
}

type UserPoem struct {
Title string `datastore:"-" goon:"id"`
UserKey *datastore.Key `datastore:"-" goon:"parent"`
Text string `datastore:",noindex"`
}

When structures are retrieved from goon, there will be a scan over the struct fields, searching for the goon tag. The goon tag is designed as a comma separated list of options, similarly to the datastore tag. When the "id" option is found, then goon will update the field value with the entity id. The int64/string decision is done automagically based on what data type the id field is. Additionally, there is a search for the "parent" option, and if found the field value is set to the entity key's parent key. On top of doing this tag searching on data retrieval, this struct update is also done on storing data, mainly to have the structs up-to-date with autogenerated ids.

You'll also notice that I tagged the id/parent fields to be ignored by the datastore API, because storing those twice would be redundant indeed.

This feature is a **small but fundamental step** towards achieving the convenience I am after. I am very interested to hear what your thoughts on this are.