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