duandong7980 2015-02-21 07:15
浏览 24

AppEngine数据存储区使用Go编码切片

I have a need to store a slice of slices in that Datastore. Since the Datastore doesn't support two levels of slices, I have encode the child slice as JSON and store it as a []byte. I'm using the PropertyLoadSaver to accomplish this. The saving and loading works, but I've noticed that the saved entity has a empty []byte for every parent element. After some investigation I discovered this is caused by the Variants.OptionsJSON Property being referenced in the Save() method and also an attribute of the struct.

So my question are:

  • Is this a bug or am I doing something wrong?
  • Is there a better way to accomplish this?

The one constraint I have is that I need to be able to query the entity by one of the "Variant" attributes, but not the "Variant.Options".

Thanks!

Example:

Given JSON

{
  "variants": [{
    "options": [
      {
        "name": "One",
        "value": "one"
      },{
        "name": "Two",
        "value": "two"
      }]
    }, {
    "options": [
        {
        "name": "Three",
        "value": "three"
        },{
        "name": "Four",
        "value": "four"
        }]
    }]
  }]
}

How it's stored in the datastore, notice the two empty '', '' attributes at the end:

Entity Kind: Products
Entity Key: ahBkZXZ-c3RvcmVza2ktY29tchULEghQcm9kdWN0cxiAgICAgNDHCww
ID: 6507459568992256
Variants.OptionsJSON (list): `['[{"name":"One","value":"one"},{"name":"Two","value":"two"}]', '[{"name":"Three","value":"three"},{"name":"Four","value":"four"}]', '', '']`

model.go

import (
    "appengine/datastore"
    "encoding/json"
)

type Option struct {
    Name  string `json:"name"`
    Value string `json:"value"`
}

type Variant struct {
    OptionsJSON []byte   `json:"-" datastore:"OptionsJSON"`
    Options     []Option `json:"options" datastore:"-"`
}

type Product struct {
    Variants []Variant `json:"variants"`
}

func (x *Product) Load(c <-chan datastore.Property) error {
    if err := datastore.LoadStruct(x, c); err != nil {
        return err
    }
    for i, v := range x.Variants {
        var opts []Option
        json.Unmarshal(v.OptionsJSON, &opts)
        x.Variants[i].Options = opts
    }
    return nil
}

func (x *Product) Save(c chan<- datastore.Property) error {
    for _, v := range x.Variants {
        b, _ := json.Marshal(v.Options)
        c <- datastore.Property{
            Name:     "Variants.OptionsJSON",
            Value:    b,
            NoIndex:  true,
            Multiple: true,
        }
    }
    return datastore.SaveStruct(x, c)
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据