英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论