doucai1901 2012-01-20 21:20
浏览 39
已采纳

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

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条回答 默认 最新

  • dongyanfeng0546 2012-01-21 00:11
    关注

    According to the following post the go datastore module doesn't support PropertyList yet.

    Use a pointer to a slice of datastore.Map instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了