指向切片的指针在使用GAE Datastore的Go语言中是否是良好的“关系”?

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

Are pointer to slices good "relationships" in Go with GAE Datastore?

问题

根据App Engine文档中的祖先查询,我可以这样做:

type Team struct {
    Name string
}

type Player struct {
    Name string
}

// 首先保存数据以供测试
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A"})
playerA := datastore.NewIncompleteKey(c, "Player", teamA)
playerA, _ = datastore.Put(c, playerA, Player{"Player A"})
playerB := datastore.NewIncompleteKey(c, "Player", teamA)
playerB , _ = datastore.Put(c, playerB, Player{"Player B"})

// 查询数据
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1).KeysOnly()
teams, _ := q.GetAll(c, nil)
q = datastore.NewQuery("Player").Ancestor(teams[0])
var players []Player
q.GetAll(c, &players)

然而...如果我想让Team包含一个指向Player切片的指针,我可以将其保存为nil,并在查询时进行赋值,类似于这样:

type Team struct {
    Name string
    Players *[]Player `datastore:"-"`
}

type Player struct {
    Name string
}

// 首先保存数据以供测试
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A", nil})

/* 保存player数据在这里 */

// 查询数据
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1)
var teams []Team
teamKeys, _ := q.GetAll(c, teams)
q = datastore.NewQuery("Player").Ancestor(teamKeys[0])
q.GetAll(c, teams[0].Players)

这样做是否是模拟关系的好方法?对于我的应用程序,树结构将是完美适合我的需求。或者...你有其他建议吗?

英文:

according to Ancestor Queries from the App Engine docs, I can do something like this:

type Team struct {
    Name string
}

type Player struct {
    Name string
}

// Save data first just for the test case
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A"})
playerA := datastore.NewIncompleteKey(c, "Player", teamA)
playerA, _ = datastore.Put(c, playerA, Player{"Player A"})
playerB := datastore.NewIncompleteKey(c, "Player", teamA)
playerB , _ = datastore.Put(c, playerB, Player{"Player B"})

// query data
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1).KeysOnly()
teams, _ := q.GetAll(c, nil)
q = datastore.NewQuery("Player").Ancestor(teams[0])
var players []Player
q.GetAll(c, &players)

However... What if I want to have Team to include a pointer to an slice of player, so I would save it as nil, and when I query it, I would assign it, sort of like this:

type Team struct {
    Name string
    Players *[]Player `datastore:-`
}

type Player {
    Name string
}

// Save data first just for the test case
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A", nil})

/* Saving player data goes here */

// query data
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1)
var teams []Team
teamKeys, _ := q.GetAll(c, teams)
q = datastore.NewQuery("Player").Ancestor(teamKeys[0])
q.GetAll(c, teams[0].Players)

Would that be a good approach for simulating relationships?
For my app, a tree structure would be the ring that perfectly fits my finger.
Or maybe... do you have another suggestion?

1: https://developers.google.com/appengine/docs/go/datastore/queries#Go_Ancestor_filters "Ancestor Queries"

答案1

得分: 2

我不认为指针是datastore的有效类型!
从这里:https://developers.google.com/appengine/docs/go/datastore/reference

一个实体的内容是从区分大小写的字段名到值的映射。有效的值类型有:

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

或者...你有其他建议吗?
一个Player的切片可能会起作用(未经测试)
或者你可以在相同的URL中搜索“PropertyLoadSaver接口”。它就像是你选择了什么和如何将事物插入到数据存储中,而不是让它自动完成。

英文:

I don't think a pointer is a valid type for datastore!
From here: https://developers.google.com/appengine/docs/go/datastore/reference
> An entity's contents are a mapping from case-sensitive field names to
> values. Valid value types are:

- signed integers (int, int8, int16, int32 and int64),
- bool,
- string,
- float32 and float64,
- any type whose underlying type is one of the above predeclared types,
- *Key,
- time.Time,
- appengine.BlobKey,
- []byte (up to 1 megabyte in length),
- structs whose fields are all valid value types,
- slices of any of the above.

Or maybe... do you have another suggestion?
A slice to Player may work (not tested)
Or maybe you should search for "The PropertyLoadSaver Interface" in the same URL. It just something like you choose what and how you insert thing to datasotre rather than let it auto.

huangapple
  • 本文由 发表于 2013年7月26日 03:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/17867351.html
匿名

发表评论

匿名网友

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

确定