如何忽略datastore.Query.GetAll()中的错误?

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

How to ignore errors in datastore.Query.GetAll()?

问题

我刚刚开始使用Go运行时开发一个GAE应用程序,到目前为止一切顺利。然而,我遇到了以下问题:

我正在利用数据存储提供的灵活性,通过使用具有不同属性的多个不同结构体保存相同实体名称(“Item”)。Go语言数据存储参考指出,“在Get和Put调用之间甚至在不同的App Engine请求之间,传递的实际类型不必匹配”,因为实体实际上只是一系列属性,因此可以存储在适当支持它们的容器类型中。

我需要查询存储在实体名称“Item”下的所有实体,并一次将它们编码为JSON。利用实体属性的灵活性,可以将查询到的实体存储在任意的datastore.PropertyList中,然而,当查询到的实体的属性无法正确表示(即不兼容的类型或缺失值)时,GetGetAll函数返回ErrFieldMismatch错误。我保存的所有这些结构体都是用户生成的,大多数值都是可选的,因此将空值保存到数据存储中没有问题。但是在检索它们时就会出现问题。

数据存储Go文档还指出,由于空值而返回的错误是可以忽略、恢复或致命的,这取决于Get方法的调用者。我想知道如何正确地处理这个问题,因为仅仅忽略错误是不够的,当查询结果出现这个错误时,我的查询目标结构体(datastore.PropertyList)根本没有填充。

提前感谢您的帮助,对于问题的冗长表示抱歉。

更新:这是一些代码

query := datastore.NewQuery("Item") // 这里我使用了一些Filter调用,以及Limit调用和Order调用
items := make([]datastore.PropertyList, 0)
_, err := query.GetAll(context, &items) // context在之前已经定义
if err != nil {
    // 处理错误的代码,对于我的情况,打印错误并将服务器状态设置为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:

[]

答案1

得分: 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.

huangapple
  • 本文由 发表于 2012年1月21日 05:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/8947997.html
匿名

发表评论

匿名网友

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

确定