为什么 GAE 数据存储不支持简单的结构字段类型?

huangapple go评论62阅读模式
英文:

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

问题

我的单元测试在报错,错误信息如下:

>&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),这样做是为了确保 decrement 和 increment 方法能够更新调用它们的版本,而不仅仅是复制一个副本。我不确定为什么这样做不起作用,可能是因为它是一个匿名字段?或者可能是因为它是一个指向 int 或结构体的指针,而不是实际的 int 或结构体?

英文:

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?

答案1

得分: 6

datastore包不允许使用所有类型。特别是,它只允许使用以下类型:

  • 有符号整数(int、int8、int16、int32和int64)
  • 布尔值
  • 字符串
  • float32和float64
  • []byte(长度最多为1兆字节)
  • 任何底层类型为上述预声明类型之一的类型
  • ByteString
  • *Key
  • time.Time(以微秒精度存储)
  • appengine.BlobKey
  • appengine.GeoPoint
  • 所有字段都是有效值类型的结构体
  • 上述任何类型的切片

请注意,这不包括"任何接口类型"。

英文:

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

<pre>

  • 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.
    </pre>

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

huangapple
  • 本文由 发表于 2015年6月22日 03:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/30968270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定