golang / appengine

Go App Engine packages
http://google.golang.org/appengine
Apache License 2.0
668 stars 207 forks source link

datastore: Add option to disable only saving of a struct field #38

Closed bashtian closed 7 years ago

bashtian commented 7 years ago

Right now a struct field needs to have the tag datastore:"-" to disable saving in the datastore. The problem is that it also disables loading of the field, which makes migrations hard because now I always get the error "no such struct field".

A better way would be to only disable saving of field, e.g. with datastore:",nosave" or datastore:",deprecated". This would make migrations a lot easier because I don't have to check for the error everywhere I load the struct.

s-mang commented 7 years ago

We have a struct field tag which may solve your issue in the cloud.google.com/go/datastore package, datastore:",omitempty". If you can migrate you code over to using that package instead, this might work well for you. Otherwise, I suggest implementing the PropertyLoadSaver interface yourself. You could use the default implementations of Load and Save, and to fall back to your special implementation when you get the "no such struct field" error.

bashtian commented 7 years ago

datastore:",omitempty" would be perfect, unfortunately changing the package is not an option at the moment. Are there plans to port this to the appengine/datastore package?

fredr commented 7 years ago

just out of curiosity, is cloud.google.com/go/xxx going to replace these packages? aka. are they the preferred packages to use, even when working with appengine standard?

ajessup commented 7 years ago

@fredr While cloud.google.com/go may largely run on App Engine Standard, for best performance and compatibility we recommend using the libraries at google.golang.org/appengine. These libraries are, and will continue to be, fully supported on this environment.

s-mang commented 7 years ago

@bashtian we do not have plans to port datastore:",omitempty" to the appengine datastore package due to lack of engineering resources. However, if you have the time and want to submit a pull request yourself, we'd welcome it! Check out the CONTRIBUTING.md at the root of this project. For reference, the cloud.google.com/go/datastore package is almost identical in implementation to this package, and the relevant commit in cloud.google.com/go/datastore is https://github.com/GoogleCloudPlatform/google-cloud-go/commit/94f177402d59546c5639e015376f04a6a92b02