google-app-engine - Go 中使用 GAE 数据存储时,指向 slice 的指针 "relationships"是否正常?

标签 google-app-engine go google-cloud-datastore

根据Ancestor Queries从 App Engine 文档中,我可以执行以下操作:

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)

但是...如果我想让 Team 包含一个指向玩家 slice 的指针,那么我会将其保存为 nil,当我查询它时,我会分配它,有点像这样:

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)

这是模拟关系的好方法吗?
对于我的应用程序,树结构将是完美适合我手指的戒指。
或者也许...您还有其他建议吗?

最佳答案

我认为指针不是数据存储的有效类型!
从这里: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.

或者也许...您还有其他建议吗?
播放器 slice 可能有效(未经测试)
或者也许您应该搜索 "The PropertyLoadSaver Interface"在同一个 URL 中。这就像您选择将内容插入到数据存储中的内容和方式,而不是让它自动插入。

关于google-app-engine - Go 中使用 GAE 数据存储时,指向 slice 的指针 "relationships"是否正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867351/

相关文章:

go - Guava Multimaps.index 等同于 golang?

iphone - 使用 Go 为 iPhone 应用程序编写库

java - Google App Engine 存储动态 java 对象

java - 在 Objectify 4 中保存时忽略一些实体字段

java - 灵活环境中的 App Engine 服务?

bash - 使用远程数据制作 Cobra 标志 bash 完成

java - Google Cloud Datastore - Java - GQLQuery 游标

google-app-engine - 子类化 db.TextProperty 以将 python dict 存储为 JSON 并将默认编码设置为 ASCII 以外的任何编码

python - 我可以使用 IronPython 为 Google App Engine 开发 GUI 吗?

java - GWT + GAE/J,通过线路发送 JDO 对象,但是如何呢?