diff --git a/src/execute.c b/src/execute.c index 091ddbc355..198ad26663 100644 --- a/src/execute.c +++ b/src/execute.c @@ -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)); diff --git a/tests/jq.test b/tests/jq.test index f3804a7da2..177c4afd06 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -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 # @@ -1873,6 +1895,7 @@ try input catch . null "break" +# debug should emit its input debug 1 1