在选择后获得空值的生成器不会产生错误。

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

Getting a generator on a null value after select gives no error

问题

考虑这个示例:

echo null | jq '[{"a": null}, {"a": [1]}][] | select(has("a")?) | .a[]'

对于第一个值,它不会出现错误,但对于第二个值,会出现错误:

echo null | jq '[{"a": null}, {"a": [1]}][] | .a[]'

看起来似乎不应该有任何变化,因为数组中的所有值都满足select表达式中的筛选条件。

这种行为的原因是什么?

英文:

Consider this example:

echo null | jq '[{"a": null}, {"a": [1]}][] | select(has("a")?) | .a[]'

It gives no error on the first value, but this one does:

echo null | jq '[{"a": null}, {"a": [1]}][] | .a[]'

It seems that nothing should change, because all values in the array satisfy the filter in the select expression.

What is the reason for this behavior?

答案1

得分: 3

如@OguzIsmail建议,这是一个错误。我相信已经报告了这个错误,地址是https://github.com/stedolan/jq/issues/1859

特别注意一下2019年4月ravron首次贡献的内容以及nicowilliams的后续内容。

记住,E?try E catch empty的简写,因此这个错误报告的标题是:“try/catch捕捉到了不应该捕捉的内容”。

就好像catch子句放错了地方:

try (select(has("a")) | .a[]) catch empty

在这种情况下,将结果与使用gojq获得的结果进行比较可能有帮助。

英文:

As @OguzIsmail suggested, it's a bug. I believe it's already been reported as https://github.com/stedolan/jq/issues/1859

See in particular the first contribution by ravron in April 2019 (!), and the followup by nicowilliams.

Recall that E? is shorthand for try E catch empty, hence the title of the bug report:
"try/catch catches more than it should"

It's as though the catch clause is misplaced:

try (select(has("a")) | .a[]) catch empty

In cases like this, comparing the results with those obtained using gojq can be helpful.

huangapple
  • 本文由 发表于 2023年3月7日 03:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655062.html
匿名

发表评论

匿名网友

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

确定