英文:
GAE Go — How to use GetMulti with non-existent entity keys?
问题
我发现自己需要使用一个键数组进行GetMulti
操作,其中一些实体存在,但有些不存在。
我的当前代码如下,返回一个错误(datastore: no such entity
)。
err := datastore.GetMulti(c, keys, infos)
那么我该如何做呢?我会使用一个“获取或插入”方法,但是没有这样的方法。
英文:
I've found myself needing to do a GetMulti
operation with an array of keys for which some entities exist, but some do not.
My current code, below, returns an error (datastore: no such entity
).
err := datastore.GetMulti(c, keys, infos)
So how can I do this? I'd use a "get or insert" method, but there isn't one.
答案1
得分: 19
GetMulti在这种情况下可以返回一个appengine.MultiError
。循环遍历它并查找datastore.ErrNoSuchEntity
。例如:
if err := datastore.GetMulti(c, keys, dst); err != nil {
if me, ok := err.(appengine.MultiError); ok {
for i, merr := range me {
if merr == datastore.ErrNoSuchEntity {
// keys[i] 不存在
}
}
} else {
return err
}
}
英文:
GetMulti can return a appengine.MultiError
in this case. Loop through that and look for datastore.ErrNoSuchEntity
. For example:
if err := datastore.GetMulti(c, keys, dst); err != nil {
if me, ok := err.(appengine.MultiError); ok {
for i, merr := range me {
if merr == datastore.ErrNoSuchEntity {
// keys[i] is missing
}
}
} else {
return err
}
}
答案2
得分: 2
我知道这个话题已经讨论了几天,但我想发布一个替代方案,使用类型开关。
if err := datastore.GetMulti(c, keys, dst); err != nil {
switch errt := err.(type) {
case appengine.MultiError:
for ix, e := range errt {
if e == datastore.ErrNoSuchEntity {
// keys[ix] 未找到
} else if e != nil {
// keys[ix] 有错误“e”
}
}
default:
// datastore 返回了一个不是多错误的错误
}
}
英文:
I know this topic is up for more than a few days, but I like to post an alternative, using type switch.
if err := datastore.GetMulti(c, keys, dst); err != nil {
switch errt := err.(type) {
case appengine.MultiError:
for ix, e := range errt {
if e == datastore.ErrNoSuchEntity {
// keys[ix] not found
} else if e != nil {
// keys[ix] have error "e"
}
}
default:
// datastore returned an error that is not a multi-error
}
}
答案3
得分: 0
Thought I'd throw my answer in to display another usecase. The following will take in any number of keys and return all the valid keys only.
// 验证键
var validKeys []*ds.Key
if err := c.DB.GetMulti(ctx, tempKeys, dst); err != nil {
if me, ok := err.(ds.MultiError); ok {
for i, merr := range me {
if merr == ds.ErrNoSuchEntity {
continue
}
validKeys = append(validKeys, tempKeys[i])
}
} else {
return "", err
}
} else {
// 所有的 tempKeys 都是有效的
validKeys = append(validKeys, tempKeys...)
}
英文:
Thought I'd throw my answer in to display another usecase. The following will take in any number of keys and return all the valid keys only.
// Validate keys
var validKeys []*ds.Key
if err := c.DB.GetMulti(ctx, tempKeys, dst); err != nil {
if me, ok := err.(ds.MultiError); ok {
for i, merr := range me {
if merr == ds.ErrNoSuchEntity {
continue
}
validKeys = append(validKeys, tempKeys[i])
}
} else {
return "", err
}
} else {
// All tempKeys are valid
validKeys = append(validKeys, tempKeys...)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论