appengine:将ndb模型转换为Go语言结构体

huangapple go评论104阅读模式
英文:

appengine: convert ndb model to go lang struct

问题

我有一个在应用引擎上的Python模块和Go模块。Go模块非常简单,只提供一个只读的搜索接口,该接口使用Python模块填充的数据存储库。

我该如何将以下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 string `datastore:"neat_name"`
    ...
}

对于ndb.KeyProperty属性,我只需在我的结构体中使用string类型吗?我需要解析该字符串,这是否很简单?

另外,我可以忽略required=Trueindexed=False选项吗?显然,因为我不进行任何写操作。

英文:

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?

答案1

得分: 2

根据https://cloud.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types,String(最多500个字符的短字符串,默认为索引)映射到Go的string类型;Text(最多1MB的长字符串,不进行索引)也映射到Go的string类型,但始终带有noindex;对于datastore的Key类型,可以使用*datastore.Key,参见https://cloud.google.com/appengine/docs/go/datastore/reference#Key;对于Integer类型,使用int64;对于Float类型,使用float64(你可以使用更短的整数和浮点数,但datastore对于每个值都使用64位,所以最好使用64位的类型)。

> 我可以忽略required=Trueindexed=False选项吗?

对于required来说是可以的,但是我认为,根据https://cloud.google.com/appengine/docs/go/datastore/reference,对于Text类型,你必须使用noindex选项,因为它必须指示可以超过512个(Unicode)字符的字符串。

不确定哪个版本的godatastore包会强制执行这个约束,但即使当前版本不强制执行,遵守这个约束也更安全,否则你的应用可能会在简单的Go版本升级时出现问题!-)

英文:

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

答案2

得分: 0

以下是代码的翻译:

这是代码 - 它在生产环境和本地环境中都可以工作:

type Course struct {
    Name              string         `datastore:"name"`
    NeatName          string         `datastore:"neat_name"`
    Country           *datastore.Key `datastore:"country"`
    University        *datastore.Key `datastore:"university"`
    Faculty           *datastore.Key `datastore:"faculty"`
    Department        *datastore.Key `datastore:"department"`
    Stage             *datastore.Key `datastore:"stage"`
    LegacyId          string         `datastore:"legacy_id"`
    CourseTitle       string         `datastore:"course_title,noindex"`
    CourseDescription string         `datastore:"course_description"`
    CourseLink        string         `datastore:"course_link,noindex"`
    CourseRating      float64        `datastore:"course_rating_"`
    CourseReviewCount int64          `datastore:"course_review_count_"`
}

func (ttt *EdSearchApi) Search(r *http.Request,
    req *SearchQuery, resp *SearchResults) error {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("Course").Limit(1)
    var courses []Course
    _, err := q.GetAll(c, &courses)
    c.Infof("err %v", err)
    c.Infof("courses 0: %v", courses[0])
    c.Infof("!!!")
    return nil
}

希望对你有帮助!

英文:

Here's the code - it's working in production & locally too:

type Course struct {
    Name              string         `datastore:"name"`
    NeatName          string         `datastore:"neat_name"`
    Country           *datastore.Key `datastore:"country"`
    University        *datastore.Key `datastore:"university"`
    Faculty           *datastore.Key `datastore:"faculty"`
    Department        *datastore.Key `datastore:"department"`
    Stage             *datastore.Key `datastore:"stage"`
    LegacyId          string         `datastore:"legacy_id"`
    CourseTitle       string         `datastore:"course_title,noindex"`
    CourseDescription string         `datastore:"course_description"`
    CourseLink        string         `datastore:"course_link,noindex"`
    CourseRating      float64        `datastore:"course_rating_"`
    CourseReviewCount int64          `datastore:"course_review_count_"`
}

and

func (ttt *EdSearchApi) Search(r *http.Request,
	req *SearchQuery, resp *SearchResults) error {
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Course").Limit(1)
	var courses []Course
	_, err := q.GetAll(c, &courses)
	c.Infof("err %v", err)
	c.Infof("courses 0: %v", courses[0])
	c.Infof("!!!")
	return nil
}

huangapple
  • 本文由 发表于 2015年3月4日 05:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/28842167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定