如何忽略 datastore.Query.GetAll() 中的错误?
我刚刚开始使用 Go 运行时开发 GAE 应用程序,到目前为止,我很高兴。但是,我遇到了以下挫折:
我利用数据存储提供的灵活性,通过使用相同的实体名称(“Item”)保存具有不同属性的多个不同结构。 Go 语言数据存储区参考指出“传递的实际类型不必在 Get 和 Put 调用之间甚至不同的 App Engine 请求之间进行匹配”,因为实体实际上只是一系列属性,因此可以存储在可以支持它们的适当容器类型中。
我需要查询存储在实体名称“Item”下的所有实体,并将它们一次性编码为 JSON。利用实体属性的灵活性对我有利,可以将查询的实体存储到任意 datastore.PropertyList
中,但是 Get
和 GetAll
当查询实体的属性无法正确表示时(也就是说,类型不兼容,或者只是缺少值),函数会返回 ErrFieldMismatch
作为错误。我保存的所有这些结构都是用户生成的,大多数值都是可选的,因此将空值保存到数据存储中。使用空值保存这些结构时没有问题(再次实现数据存储灵活性),但检索它们时会出现问题。
数据存储 Go 文档中还指出,由 Get
方法的调用者决定由于空值而返回的错误是可忽略的、可恢复的还是致命的。我想知道如何正确执行此操作,因为仅忽略错误是不够的,因为当查询结果为时,查询的目标结构(datastore.PropertyList
)根本没有填充这个错误。
预先感谢您,并对这个冗长的问题表示歉意。
更新:这是一些代码
query := datastore.NewQuery("Item") // here I use some Filter calls, as well as a Limit call and an Order call
items := make([]datastore.PropertyList, 0)
_, err := query.GetAll(context, &items) // context has been obviously defined before
if err != nil {
// something to handle the error, which in my case, it's printing it and setting the server status as 500
}
更新 2:这是一些输出
如果我使用 make([]datastore.PropertyList, 0)
,我会得到:
datastore: invalid entity type
如果我使用 make(datastore.PropertyList , 0)
,我得到这个:
datastore: cannot load field "Foo" into a "datastore.Property": no such struct field
在这两种情况下(我认为第一个可以被丢弃)在 items
中我得到这个:
[]
I just started developing a GAE app with the Go runtime, so far it's been a pleasure. However, I have encountered the following setback:
I am taking advantage of the flexibility that the datastore provides by having several different structs with different properties being saved with the same entity name ("Item"). The Go language datastore reference states that "the actual types passed do not have to match between Get and Put calls or even across different App Engine requests", since entities are actually just a series of properties, and can therefore be stored in an appropriate container type that can support them.
I need to query all of the entities stored under the entity name "Item" and encode them as JSON all at once. Using that entity property flexibility to my advantage, it is possible to store queried entities into an arbitrary datastore.PropertyList
, however, the Get
and GetAll
functions return ErrFieldMismatch
as an error when a property of the queried entities cannot be properly represented (that is to say, incompatible types, or simply a missing value). All of these structs I'm saving are user generated and most values are optional, therefore saving empty values into the datastore. There are no problems while saving these structs with empty values (datastore flexibility again), but there are when retrieving them.
It is also stated in the datastore Go documentation, that it is up to the caller of the Get
methods to decide if the errors returned due to empty values are ignorable, recoverable, or fatal. I would like to know how to properly do this, since just ignoring the errors won't suffice, as the destination structs (datastore.PropertyList
) of my queries are not filled at all when a query results in this error.
Thank you in advance, and sorry for the lengthy question.
Update: Here is some code
query := datastore.NewQuery("Item") // here I use some Filter calls, as well as a Limit call and an Order call
items := make([]datastore.PropertyList, 0)
_, err := query.GetAll(context, &items) // context has been obviously defined before
if err != nil {
// something to handle the error, which in my case, it's printing it and setting the server status as 500
}
Update 2: Here is some output
If I use make([]datastore.PropertyList, 0)
, I get this:
datastore: invalid entity type
And if I use make(datastore.PropertyList, 0)
, I get this:
datastore: cannot load field "Foo" into a "datastore.Property": no such struct field
And in both cases (the first one I assume can be discarded) in items
I get this:
[]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据以下帖子,go datastore 模块不会尚不支持 PropertyList。
请改用指向
datastore.Map
切片的指针。According to the following post the go datastore module doesn't support PropertyList yet.
Use a pointer to a slice of
datastore.Map
instead.