python - 应用引擎 : convert ndb model to go lang struct

标签 python google-app-engine go

我在 App Engine 上有一个 python 模块和一个 go 模块。 go 模块相当简单,只是为由 python 模块填充的数据存储提供一个只读搜索接口(interface)。

如何将以下 ndb 模型转换为 go 结构:

class Course(ndb.Model):
    name = ndb.StringProperty()
    neat_name = ndb.StringProperty(required=True)
    country = ndb.KeyProperty(kind=Country, required=True)
    university = ndb.KeyProperty(kind=University, required=True)
    faculty = ndb.KeyProperty(kind=Faculty, required=True)
    department = ndb.KeyProperty(kind=Department, required=True)
    stage = ndb.KeyProperty(kind=Stage, required=True)

    legacy_id = ndb.StringProperty()
    course_title = ndb.StringProperty(required=True, indexed=False)
    course_description = ndb.TextProperty(required=True)
    course_link = ndb.StringProperty(required=True, indexed=False)

    #0-5 or None or not has attribute.
    course_rating_ = ndb.FloatProperty()
    course_review_count_ = ndb.IntegerProperty()

首先我要:

type Course struct {
    Name string `datastore:"name"`
    NeatName `datastore:"neat_name"`
    ...
}

对于 ndb.KeyProperty 属性 - 我是否只在我的 struct 中使用 string? & 我必须解析该字符串 - 是不是直截了当?

我也可以忽略 required=Trueindexed=False 选项吗?显然因为我没有做任何写入?

最佳答案

根据 https://cloud.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types , String(最多500个字符的短字符串,默认索引)映射到Go stringText(最大 1MB 的长字符串,未编入索引)也为 Go string 但始终使用 noindex;对于数据存储 Key,有 *datastore.Key,请参阅 https://cloud.google.com/appengine/docs/go/datastore/reference#Key ;对于 Integerint64;对于 Floatfloat64(您可以使用更短的整数和 float ,但数据存储区无论如何都使用 64 位,所以您也可以:-)。

Also can I just ignore the required=True & indexed=False options?

对于 required 是的,但我相信,使用 https://cloud.google.com/appengine/docs/go/datastore/reference ,您必须为 Text 使用选项 noindex,因为它必须指示长度超过 512 (unicode) 字符的字符串。

不确定 go 和它的 datastore 包的哪个版本强制执行此约束,但即使当前版本没有执行此约束,无论如何还是尊重它更安全——否则您的应用程序可能会因简单的 Go 版本升级而崩溃!-)

关于python - 应用引擎 : convert ndb model to go lang struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842167/

相关文章:

java - PyQt/Qt 是否有类似于 MigLayout 的布局管理器?

java - gradlew appengineEndpointsInstallClientLibs 有错误 Execution failed for task ':compileJava' 。 > 无效源版本 : 1. 7

python - 在 GAE 登录重定向期间保留 POST 变量?

go - 如何区分哪些依赖项是可下载的?

python - 根据第二个数组过滤numpy数组中的行

python - 实时捕获 python-background-processes 输出

python - 如果 LSTM 单元由同一个函数创建,它们会共享权重吗?

java - 异常获取服务器实例 : No valid instance id for this instance

c - Go 系统调用 vs. C 系统调用

mongodb - 如何制定正确的mongo查询以获取正确格式的记录