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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)