为什么 GAE 数据存储不支持简单的结构字段类型?
why is a simple struct field type not supported in GAE datastore?
my unit test 失败并显示消息:
&errors.errorString{s:"datastore: unsupported struct field type: sus.Version"}
我有一个正在尝试保存到 GAE 数据存储的测试结构类型:
type foo struct{
sus.Version
}
其中 sus.Version 是接口:
type Version interface{
GetVersion() int
getVersion() int
incrementVersion()
decrementVersion()
}
我已经尝试 运行 我的测试有两个 Version 实现,首先它只是一个 int 的别名:
type version int
其次作为结构:
type version struct{
val int
}
其中 Version 接口方法被赋予接收器类型 (v *version)
,它需要是一个指针,因此递减和递增实际上更新了它们被调用的版本,而不仅仅是一个副本。我不确定为什么这不起作用,可能是因为它是一个匿名字段?或者可能是因为它是一个指向 int 或 struct 而不是实际 int 或 struct 的指针?
datastore package 不允许使用所有类型。特别是,它只允许使用以下类型:
- 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.
请注意,这不包括 "any interface type"。
my unit test 失败并显示消息:
&errors.errorString{s:"datastore: unsupported struct field type: sus.Version"}
我有一个正在尝试保存到 GAE 数据存储的测试结构类型:
type foo struct{
sus.Version
}
其中 sus.Version 是接口:
type Version interface{
GetVersion() int
getVersion() int
incrementVersion()
decrementVersion()
}
我已经尝试 运行 我的测试有两个 Version 实现,首先它只是一个 int 的别名:
type version int
其次作为结构:
type version struct{
val int
}
其中 Version 接口方法被赋予接收器类型 (v *version)
,它需要是一个指针,因此递减和递增实际上更新了它们被调用的版本,而不仅仅是一个副本。我不确定为什么这不起作用,可能是因为它是一个匿名字段?或者可能是因为它是一个指向 int 或 struct 而不是实际 int 或 struct 的指针?
datastore package 不允许使用所有类型。特别是,它只允许使用以下类型:
- 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.
请注意,这不包括 "any interface type"。