Python appengine:将ndb模型转换为go lang结构
我在appengine上有一个python模块和一个go模块。go模块相当简单,只提供了一个到数据存储的只读搜索接口,该数据存储由python模块填充 如何将以下ndb模型转换为go结构:Python appengine:将ndb模型转换为go lang结构,python,google-app-engine,go,Python,Google App Engine,Go,我在appengine上有一个python模块和一个go模块。go模块相当简单,只提供了一个到数据存储的只读搜索接口,该数据存储由python模块填充 如何将以下ndb模型转换为go结构: class Course(ndb.Model): name = ndb.StringProperty() neat_name = ndb.StringProperty(required=True) country = ndb.KeyProperty(kind=Country, requ
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
选项吗?很明显,因为我没有写任何东西?Per,字符串(一个最多500个字符的短字符串,默认情况下索引)映射到Go字符串
Text
(一个最大1MB的长字符串,没有索引)也可以转到string
,但始终使用noindex
;对于数据存储Key
,有*数据存储。Key
,请参阅;对于Integer
,int64
;对于Float
,float64
(可以使用较短的int和Float,但数据存储使用64位,因此也可以:-)
我还可以忽略required=True
和indexed=False
选项吗
对于required
,是的,但我认为,使用时,对于Text
必须使用选项noindex
,因为有必要指示长度超过512(unicode)字符的字符串
不确定哪些版本的go
及其datastore
软件包强制执行此约束,但即使当前版本没有强制执行此约束,也应该更安全地遵守此约束,否则您的应用程序可能会因简单的go版本升级而崩溃!) 以下是代码-它在生产和本地也起作用:
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
}