drl47263 2017-04-21 18:44
浏览 36
已采纳

无法将字节数组存储在Google的数据存储区中

I'm using Google's datastore in my Go app. I have a Song struct, which has a uuid.UUID field.

type Song struct {
    ID: uuid.UUID
    Title: string
    ...
}

This UUID is taken from github.com/satori/go.uuid and is defined as

type UUID [16]byte

It seems that datastore can't handle byte arrays but in this use case only byte slices or strings. In the json package I can use a tag to interpret it as a string

type Song struct {
    ID: uuid.UUID `json:"id,string"`
    ....
}

Is there a way of telling datastore to interpret the UUID as a slice/string or do I either have to give up "type"-safety and just store a string or use a custom PropertyLoadSaver?

  • 写回答

1条回答 默认 最新

  • dongtang2376 2017-04-21 19:50
    关注

    Per Google's Documentation:

    Valid value types are:

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

    So, you will have to use a byte slice or string. You could do some behind the scenes manipulation when you need to do your setting or getting like (Playground Example):

    type uuid [16]byte
    
    type song struct {
        u []byte
    }
    
    func main() {
        var b [16]byte
        copy(b[:], "0123456789012345")
    
        var u uuid = uuid(b) //this would represent when you get the uuid
    
        s := song{u: []byte(u[:])}
    
        copy(b[:], s.u)
        u = uuid(b)
        fmt.Println(u)
    }
    

    This could also be done through methods. (Playground example)

    Alternatively, you could have an entity specific to the datastore that carries the byte slice, and the transformers that go to and from that entity know how to do the conversion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题