dongwu8064 2019-07-27 02:59
浏览 57
已采纳

如何使用结构体数组实现Google Datastore PropertyLoadsaver

How do you implement Load() and Save() for googles datastore when you have an array of structs? It is clearly possible, but how?

Firstly, when you allow the datastore itself to serialise a Person with a list of Phone objects, you can use reflection to see it internally creates a list of *datastore.Entity objects:

package main

import (
    "fmt"
    "reflect"

    "cloud.google.com/go/datastore"
)

type Phone struct {
    Type   string
    Number string
}

type Person struct {
    Name  string
    Phone []Phone
}

func main() {
    person := Person{Name: "Bob", Phone: []Phone{Phone{"a", "b"}, Phone{"c", "d"}}}

    // save here

    }
}

Here is my work so far, this saves the name field, but causes an error on the *datastore.Entity objects. here is my attempt:

func (p *Person) Save() ([]datastore.Property, error) {
    props := []datastore.Property{
        {
            Name:  "Name",
            Value: p.Name,
        },
    }

    var n []*datastore.Entity
    for _, x := range p.Phone {
        i1 := datastore.Property{Name: "Type", Value: x.Type}
        i2 := datastore.Property{Name: "Number", Value: x.Number}
        e := &datastore.Entity{Properties: []datastore.Property{i1, i2}}
        n = append(n, e)
    }
    props = append(props, datastore.Property{Name:"Phone",Value:n})

    return props, nil
}

Datastore itself complains with the following error:

invalid Value type []*datastore.Entity for a Property with Name "Phone"

I wonder if anyone can elucidate where I am going wrong? How do I save an array of structs in datastore in the same way datastore itself does?

Sample code above is on go playground: https://play.golang.org/p/AP1oFnlo1jm

  • 写回答

1条回答 默认 最新

  • dongxie8906 2019-07-27 07:09
    关注

    After some amount of experimentation it turns out here is how to implement a Save() method for an object that has structs. The datastore.Property must store an array of []interface{} holding []*datastore.Entity rather than a pure []*datastore.Entity:

    func (p *Person) Save() ([]datastore.Property, error) {
        props := []datastore.Property{
            {
                Name:  "Name",
                Value: p.Name,
            },
        }
    
        var n []interface{}
        for _, x := range p.Phone {
            i1 := datastore.Property{Name: "Type", Value: x.Type}
            i2 := datastore.Property{Name: "Number", Value: x.Number}
            e := &datastore.Entity{Properties: []datastore.Property{i1, i2}}
            n = append(n, e)
        }
        props = append(props, datastore.Property{Name:"Phone",Value:n})
    
        return props, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。