dongmeng1875 2013-07-25 19:43
浏览 42
已采纳

在带有GAE数据存储的Go中,指向切片的指针是否具有良好的“关系”?

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条回答 默认 最新

  • doubian0284 2013-08-20 08:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程