在GAE数据存储中使用Go的嵌套结构

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

Nested structs on GAE datastore using Go

问题

我正在尝试弄清楚如何在使用Go的GAE数据存储时使嵌套结构起作用。我知道数据存储不特别支持嵌套结构。当将帖子作为JSON发送给用户时,我需要找到一种简单的方法来获取用户信息。

我想到的一种方法是为用户设置两个字段。一个用于ID/键引用用户,另一个用于用户类型结构,在从数据存储加载帖子时将其添加到那里。额外的字段似乎很愚蠢,所以我希望有更好的解决方案。

有两种实体类型或结构:POST和USER

帖子需要包含有关发帖用户的信息。

我将为用户输出的JSON的结构如下:

  • POST
    • field1
    • field2
    • USER
      • user_field1
      • user_Field2
英文:

I'm trying to figure out how to get nested structs to work with GAE datastore using Go. I know the datastore doesn't specifically support nested structs. I need to find a simple way of getting user information to go with a post when it is sent out to a user as JSON.

One thing I thought of was to put two fields for the user. One for the ID/key referencing to user and another one for the user type struct which would be added there when the post is loaded from the datastore. Extra fields seem silly so I'm hoping there is a better solution for this.

There are two entity types or structs: POST and USER

Posts need to contain information about the user who made the post.

The structure for the JSON I'm going to output for users is as follows:

  • POST
    • field1
    • field2
    • USER
      • user_field1
      • user_Field2

答案1

得分: 9

Go的appengine datastore api提供了PropertyLoadSaver接口来处理这种情况:https://developers.google.com/appengine/docs/go/datastore/reference#PropertyLoadSaver

你可以按照自己的方式构建结构体,然后实现该接口的Load和Save方法来正确填充它。这意味着你需要自己编写序列化代码,但它给了你在数据结构上的完全自由。

这样你仍然可以对字段进行过滤,并且可以有嵌套的结构体。

英文:

Go's appengine datastore api provides the PropertyLoadSaver interface for this sort of thing: https://developers.google.com/appengine/docs/go/datastore/reference#PropertyLoadSaver

You structure your struct however you want and then implement the Load and Save methods of that interface to populate it correctly. It means you write the serialization code yourself but it gives you full freedom in how you structure your data.

This will allow you still filter over the fields and have a nested struct.

答案2

得分: 0

Python运行时有ndb库,支持像这样的嵌套结构。Go语言没有,所以我可以想到两种解决方案:

  1. 在POST种类中,有一个用户字段,它是一个键,引用了一个具有必要字段的USER种类。需要两次获取和往返。
  2. 在POST种类中创建一个用户字段,它是一个blob。blob是一个在go中[de]序列化的字符串。这意味着您不能搜索或过滤任何用户数据,但它也允许您将所有内容存储在一个实体中。

您应该根据您的应用需求使用这些。如果您需要用户成为一个真实的事物,请使用1。如果用户不是您需要处理的对象(即仅用于显示的数据),则可以使用2。

英文:

The python runtime has the ndb library which supports nested structures like this. Go does not, so I can think of two solutions:

  1. In the POST kind, have a user field that is a key, referencing a USER kind with the necessary fields. Requires two fetches and roundtrips.
  2. Make a user field in the POST kind that is a blob. The blob is a string that is [de]serialized in go. This means you can't search or filter on any of the user data, but it also allows you to store everything in one entity.

You should use these based on the needs of your app. If you need users to be a real thing, use 1. If users aren't objects you need to work with (i.e., just data to display), you can use 2.

huangapple
  • 本文由 发表于 2013年1月14日 05:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/14308530.html
匿名

发表评论

匿名网友

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

确定