Closed itchyny closed 2 years ago
Hmmm seems to be parsed as expected, so it's either a compiler's issue or a VM's issue.
Note that the optional operator applies only to the last (index, slice and iterator) suffix. Here's
❯ jq -n '0 | .x.y?'
jq: error (at <unknown>): Cannot index number with string "x"
❯ xq -n '0 | .x.y?'
Here, .x.y?
is semantically equivalent to .x | try .y
, not try .x.y
. Likewise, 0 | .[].x?
is 0 | .[] | try .x
, not 0 | try .[].x
.
But while I was investigating the gojq implementation again, I noticed that gojq also has a bug. The query 0 | .x[]?
should emit indexing error because it is equivalent to 0 | .x | try .[]
, not 0 | try .x[]
. xq has this issue too.
Using optional object indexing as term suffix seems to have different semantics.
Interestingly, adding
|
will fix the behavior.