Embedded (anonymous) structs not serializable to datastore

569 views
Skip to first unread message

Ben Harper

unread,
Nov 1, 2011, 7:05:11 PM11/1/11
to google-ap...@googlegroups.com
I've got:
type Base struct {...}
type A struct { Base, ... }
type B struct { Base, ... }

i.e, an embedded struct Base inside struct A (and B, etc).

If I try to serialize this to the datastore, I get:

Error: datastore: cannot store field named "Base" from a "A": unsupported datastore value type: appname.Base

Is there any way to accomplish what I'm trying to achieve here, or do I need to write my own reflection stuff?

Andrew Gerrand

unread,
Nov 2, 2011, 2:24:29 AM11/2/11
to google-ap...@googlegroups.com

You'll need to handle this case manually right now. It's something
that the datastore package should probably do.

Regardless of embedding, people want to do things like:

type Pair struct{ X, Y int }
type Foo struct {
 XY Pair
 Z int
}

and we eventually want to support this.

Although if we made datastore to do this with embedding, I think what
you'd end up with is properties named "Base.Field", not "Field". If
you wanted just "Field", then that might need more than what the
reflect package can
give. Embedded fields are just syntax; the A type doesn't have an
Field field, it has a Base field and Base has an Field field.

Andrew

Ben Harper

unread,
Nov 2, 2011, 2:59:07 AM11/2/11
to google-ap...@googlegroups.com
Thanks - I didn't realize that was how embedded fields worked.

Although for this particular project of mine, writing explicit serialization doesn't feel like much of a burden, having the language capable of building a decent ORM without resorting to scripted code-gen seems like a reasonable litmus test.

Ben

Ben Harper

unread,
Dec 9, 2011, 4:51:38 PM12/9/11
to google-ap...@googlegroups.com
I was wondering whether I could use the recently introduced struct tags to accomplish my original question.

However, this doesn't work:

type base struct {...}
type A struct {
  base    `datastore:"-"`
  Other   int64
}

I still get datastore: unsupported struct field type: mypackage.base

Is there any way to achieve my original goal using struct tags?

Thanks,
Ben

David Symonds

unread,
Dec 9, 2011, 6:42:10 PM12/9/11
to google-ap...@googlegroups.com

That seems like a bug. File an issue?

Ben Harper

unread,
Dec 10, 2011, 3:18:17 AM12/10/11
to google-ap...@googlegroups.com
I opened an issue: http://code.google.com/p/googleappengine/issues/detail?id=6516

I'll see if I get the energy to write a patch.

gar...@gmail.com

unread,
May 24, 2012, 9:49:16 AM5/24/12
to google-ap...@googlegroups.com
I've currently run into the same problem, and am a bit disappointed that there's no straightforward way to handle this in Go. It looks like I should make specific "database structs" for storage, and convert them to real structs manually for everything I want to store. I suppose this could be good practice in general, but so far, I've been attempting to save work by having the structs I use be the same ones I store -- and I found this approach forced me to structure my app around the limitations of the datastore, which has been unpleasant -- and doesn't let me use the really cool parts of Go.

Just a +1, I guess.

Jeff Huter

unread,
May 25, 2012, 2:32:46 PM5/25/12
to google-ap...@googlegroups.com
On Thursday, May 24, 2012 9:49:16 AM UTC-4, gar...@gmail.com wrote:
I've currently run into the same problem, and am a bit disappointed that there's no straightforward way to handle this in Go. It looks like I should make specific "database structs" for storage, and convert them to real structs manually for everything I want to store. I suppose this could be good practice in general, but so far, I've been attempting to save work by having the structs I use be the same ones I store -- and I found this approach forced me to structure my app around the limitations of the datastore, which has been unpleasant -- and doesn't let me use the really cool parts of Go.

Just a +1, I guess.

I sort of worked around this using the PropertyLoadSaver interface.  Basically, create Load and Save methods that direct the datastore fields to the proper locales in the Entity struct and visa versa.  Not exactly straight forward, but doable.  The big bonus is being able to create your structs in a manner that fits the problem domain at the cost of more complicated Load and Save method.

Though +1 for having this work out of the box without using the PropertyLoadSaver interface. 
Reply all
Reply to author
Forward
0 new messages