是否指向切片与Go与GAE数据存储区中的良好“关系”?

时间:2013-07-25 19:43:49

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

根据App Engine文档中的Ancestor Queries,我可以这样做:

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包含一个指向一片播放器的指针,那么我会将其保存为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)

这是模拟人际关系的好方法吗? 对于我的应用程序,树形结构将是完全适合我的手指的环 或许......你有其他建议吗?

1 个答案:

答案 0 :(得分:2)

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

  

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

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

或者......你还有其他建议吗?
对玩家的切片可以工作(未测试)
或者您可以在同一网址中搜索"The PropertyLoadSaver Interface"。它就像你选择什么以及如何将东西插入datasotre而不是让它自动。