som*_*ne1 4 python google-app-engine go app-engine-ndb
出于性能原因,我正在Go的Google AppEngine项目中编写模块,但需要能够读取我在数据存储区中拥有的某些实体。我写了Go代码以能够读取我在Python中构建的实体,但出现以下错误:
datastore: flattening nested structs leads to a slice of slices: field "Messages"
Python中的模型定义:
class ModelB(ndb.Model):
msg_id = ndb.StringProperty(indexed=False)
cat_ids = ndb.StringProperty(repeated=True, indexed=False)
list_ids = ndb.StringProperty(repeated=True, indexed=False)
default_list_id_index = ndb.IntegerProperty(indexed=False)
class ModelA(ndb.Model):
date_join = ndb.DateTimeProperty(auto_now_add=True)
name = ndb.StringProperty()
owner_salutation = ndb.StringProperty(indexed=False)
owner_email_address = ndb.StringProperty()
logo_url = ndb.StringProperty(indexed=False)
...
messages = ndb.LocalStructuredProperty(ModelB, name='bm', repeated=True)
Run Code Online (Sandbox Code Playgroud)
在Go中:
type ModelB struct {
MessageID string `datastore:"msg_id,noindex"`
CategoryIDs []string `datastore:"cat_ids,noindex"`
ListIDs []string `datastore:"list_ids,noindex"`
DefaultListIDIndex int `datastore:"default_list_id_index,noindex"`
}
type ModelA struct {
DateJoin time.Time `datastore:"date_join,"`
Name string `datastore:"name,"`
OwnerSalutation string `datastore:"owner_salutation,noindex"`
OwnerEmailAddress string `datastore:"owner_email_address,"`
LogoURL string `datastore:"logo_url,noindex"`
Messages []ModelB `datastore:"bm,"`
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错什么了吗?Go与Python模型定义之间的功能仅仅是不兼容吗?
尝试解码ModelB
重新定义ModelA如下:
import pb "appengine_internal/datastore"
import proto "code.google.com/p/goprotobuf/proto"
type ModelA struct {
DateJoin time.Time `datastore:"date_join,"`
Name string `datastore:"name,"`
OwnerSalutation string `datastore:"owner_salutation,noindex"`
OwnerEmailAddress string `datastore:"owner_email_address,"`
LogoURL string `datastore:"logo_url,noindex"`
Messages []ModelB `datastore:"-"`
}
// Load is implemented for the PropertyLoaderSaver interface.
func (seller *ModelA) Load(c <-chan datastore.Property) error {
f := make(chan datastore.Property, 100)
for p := range c {
if p.Name == "bm" {
var val pb.EntityProto
err := proto.Unmarshal([]byte(p.Value.(string)), &val)
if err != nil {
return err
}
//TODO: Store result as a new ModelB
} else {
f <- p
}
}
close(f)
return datastore.LoadStruct(seller, f)
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
proto: required field "{Unknown}" not set
Go数据存储区包不支持像这样的两层切片。您可以使用[]ModelB,只要ModelB不包含任何切片。或者,您可以ModelB在中使用ModelA,并且ModelB可以在其中包含切片。但你不能同时拥有[]ModelB和ModelB具有片。请参阅代码以了解错误情况。您的选择:
| 归档时间: |
|
| 查看次数: |
1390 次 |
| 最近记录: |