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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看