A serializer that works in conjonction with Google Cloud Datastore to serialize relationships defined as pointers. This is useful if you go the denormalization way.
go get github.com/marcgiovannoni/datastore-serializer
This serializer has been written so working with Google JSONAPI and Google Cloud Datastore is easier.
The use of jsonapi is not mandatory, it will work with simple relationships.
ID
attribute has to be an encoded datastore.Key
type Post struct {
ID string `datastore:"-" jsonapi:"primary,post" serializer:"primary,post"`
Text string `datastore:"text,noindex" jsonapi:"attr,text"`
Comments []*Comment `datastore:"-" jsonapi:"relation,comments" serializer:"relation,comments"`
}
type Comment struct {
ID string `datastore:"-" jsonapi:"primary,comment" serializer:"primary,comment"`
Text string `datastore:"text,noindex" jsonapi:"attr,text"`
}
func (me *Post) Load(ps []datastore.Property) error {
return serializer.LoadEntity(me, ps)
}
func (me *Post) Save() ([]datastore.Property, error) {
ps, err := serializer.SaveEntity(me)
if err != nil {
return nil, err
}
return ps, nil
}
func (me *Comment) Load(ps []datastore.Property) error {
return serializer.LoadEntity(me, ps)
}
func (me *Comment) Save() ([]datastore.Property, error) {
ps, err := serializer.SaveEntity(me)
if err != nil {
return nil, err
}
return ps, nil
}
This will be serialized in the datastore as:
text comments.id comments.text