Daniel Robinson
Daniel Robinson

Reputation: 14898

why is a simple struct field type not supported in GAE datastore?

my unit test is failing with message:

&errors.errorString{s:"datastore: unsupported struct field type: sus.Version"}

I have a test struct type that I am trying to save to GAE datastore:

type foo struct{
    sus.Version
}

where sus.Version is the interface:

type Version interface{
    GetVersion() int
    getVersion() int
    incrementVersion() 
    decrementVersion() 
}

I have tried running my test with two Version implementations, first where it is just an alias for an int:

type version int

and secondly as a struct:

type version struct{
    val int
}

where the Version interface methods are given receiver type (v *version), it needs to be a pointer so decrement and increment actually update the version they are called on and not just a copy. I'm not sure why this isn't working, potentially because it's an anonymous field? or perhaps because it's a pointer to an int or struct rather than an actual int or struct?

Upvotes: 2

Views: 1571

Answers (1)

user1804599
user1804599

Reputation:

The datastore package doesn't allow all types to be used. In particular, it only allows the following types to be used:

- signed integers (int, int8, int16, int32 and int64),
- bool,
- string,
- float32 and float64,
- []byte (up to 1 megabyte in length),
- any type whose underlying type is one of the above predeclared types,
- ByteString,
- *Key,
- time.Time (stored with microsecond precision),
- appengine.BlobKey,
- appengine.GeoPoint,
- structs whose fields are all valid value types,
- slices of any of the above.

Note that this doesn't include "any interface type".

Upvotes: 6

Related Questions