Skip to content

Commit

Permalink
execute.c: support pick(last)
Browse files Browse the repository at this point in the history
Support [0] | setpath([-1]; _)
Add tests for pick/1
  • Loading branch information
pkoppstein committed Jul 27, 2023
1 parent 668607e commit 6d1e5fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,14 @@ jv jq_next(jq_state *jq) {
set_error(jq, jv_invalid_with_msg(msg));
goto do_backtrack;
}
// $array | .[-1]
if (jv_get_kind(k) == JV_KIND_NUMBER && jv_get_kind(t) == JV_KIND_ARRAY) {
int idx = jv_number_value(k);
if (idx < 0) {
jv_free(k);
k = jv_number(jv_array_length(jv_copy(t)) + idx);
}
}
jv v = jv_get(t, jv_copy(k));
if (jv_is_valid(v)) {
path_append(jq, k, jv_copy(v));
Expand Down
23 changes: 23 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,28 @@ del(.[1], .[-6], .[2], .[-3:9])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 3, 5, 6, 9]

# negative index
setpath([-1]; 1)
[0]
[1]

pick(.a.b.c)
null
{"a":{"b":{"c":null}}}

pick(first)
[1,2]
[1]

pick(first|first)
[[10,20],30]
[[10]]

# negative indices in path expressions (since last/1 is .[-1])
pick(last)
[[10,20],30]
[null,30]

#
# Assignment
#
Expand Down Expand Up @@ -1873,6 +1895,7 @@ try input catch .
null
"break"

# debug should emit its input
debug
1
1
Expand Down

0 comments on commit 6d1e5fb

Please sign in to comment.