使用Go在GAE数据存储上嵌套结构 [英] Nested structs on GAE datastore using Go

查看:109
本文介绍了使用Go在GAE数据存储上嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用Go来获得嵌套结构与GAE数据存储一起工作。我知道数据存储并不特别支持嵌套结构。我需要找到一种获取用户信息的简单方法,当用户信息以JSON的形式发送给用户时。



我想到的一件事就是把两个字段供用户使用。一个用于引用用户的ID /密钥,另一个用于用户类型结构,当从数据存储加载帖子时,该用户类型结构将添加到用户类型结构中。额外的领域看起来很愚蠢,所以我希望有更好的解决方案。



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



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

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


  • POST

    • field1

    • field2

    • USER

      • user_field1

      • user_Field2

      • appengine datastore api为这类事情提供PropertyLoadSaver接口: https://developers.google.com / appengine / docs / go / datastore / reference#PropertyLoadSaver

        您可以根据需要构建您的结构,然后将该接口的Load和Save方法实现为填充它正确。这意味着你自己编写序列化代码,但它可以让你完全自由地构造你的数据。



        这将允许你仍然过滤字段并且嵌套结构。


        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

        解决方案

        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.

        这篇关于使用Go在GAE数据存储上嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆