我在 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=True
和 indexed=False
选项吗?显然因为我没有做任何写入?
最佳答案
根据 https://cloud.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types , String
(最多500个字符的短字符串,默认索引)映射到Go string
; Text
(最大 1MB 的长字符串,未编入索引)也为 Go string
但始终使用 noindex
;对于数据存储 Key
,有 *datastore.Key
,请参阅 https://cloud.google.com/appengine/docs/go/datastore/reference#Key ;对于 Integer
,int64
;对于 Float
、float64
(您可以使用更短的整数和 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/