dongzhong5756 2015-03-03 21:24
浏览 8
已采纳

appengine:将ndb模型转换为lang结构

I've got a python module and a go module on app engine. The go module is fairly simple and just provides a readonly search interface to the datastore which is populated by the python module.

How do I convert the following ndb model into a go struct:

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()

To start with I'll have:

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

For the ndb.KeyProperty properties - Do I just use a string in my struct? & I'll have to parse that string - is that straight forward?

Also can I just ignore the required=True & indexed=False options? Obviously since i'm not doing any writes?

  • 写回答

2条回答 默认 最新

  • dongnong7524 2015-03-04 00:06
    关注

    Per https://cloud.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types , String (a short string of up to 500 characters, indexed by default) maps to Go string; Text (a long string up to 1MB, not indexed) also to Go string but always with noindex; for datastore Key there is *datastore.Key, see https://cloud.google.com/appengine/docs/go/datastore/reference#Key ; for Integer, int64; for Float, float64 (you could use shorter ints and floats but the datastore uses 64 bits for each anyway so you might as well:-).

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

    Yes for required, but I believe that, using https://cloud.google.com/appengine/docs/go/datastore/reference , you do have to use option noindex for Text because it's necessary to indicate strings that can be longer than 512 (unicode) characters.

    Not sure which versions of go and its datastore package enforce this constraint, but even if the present one doesn't it's safer to respect it anyway -- or else your app might break with a simple Go version upgrade!-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值