Skip to content

Commit

Permalink
Merge pull request #97 from eiiches/feature/20210807-fix-bracket-fiel…
Browse files Browse the repository at this point in the history
…d-access-iteration-order

core: fix bracket field access iteration order
  • Loading branch information
eiiches authored Aug 7, 2021
2 parents 5ee025b + 5083381 commit 5837272
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ public String toString() {

@Override
public void apply(final Scope scope, final JsonNode in, final Path path, final PathOutput output, final boolean requirePath) throws JsonQueryException {
target.apply(scope, in, path, (pobj, ppath) -> {
if (isRange) {
startExpr.apply(scope, in, (start) -> {
endExpr.apply(scope, in, (end) -> {
if (isRange) {
startExpr.apply(scope, in, (start) -> {
endExpr.apply(scope, in, (end) -> {
target.apply(scope, in, path, (pobj, ppath) -> {
if ((start.isNumber() || start.isNull()) && (end.isNumber() || end.isNull())) {
emitArrayRangeIndexPath(permissive, start, end, pobj, ppath, output, requirePath);
} else {
if (!permissive)
throw new JsonQueryTypeException("Start and end indices of an %s slice must be numbers", pobj.getNodeType());
}
});
}, requirePath);
});
} else { // isRange == false
startExpr.apply(scope, in, (accessor) -> {
});
} else { // isRange == false
startExpr.apply(scope, in, (accessor) -> {
target.apply(scope, in, path, (pobj, ppath) -> {
if (accessor.isNumber()) {
emitArrayIndexPath(permissive, accessor, pobj, ppath, output, requirePath);
} else if (accessor.isTextual()) {
Expand All @@ -61,8 +63,8 @@ public void apply(final Scope scope, final JsonNode in, final Path path, final P
if (!permissive)
throw new JsonQueryTypeException("Cannot index %s with %s", pobj.getNodeType(), accessor.getNodeType());
}
});
}
}, requirePath);
}, requirePath);
});
}
}
}
24 changes: 24 additions & 0 deletions jackson-jq/src/test/resources/tests/constructs/array_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,27 @@
- q: '[1,1,1,1,1] | .[-1.2] = 3'
out:
- [1, 1, 1, 1, 3]

- q: '.[]["a","b"]'
in: [{"a": 1, "b": 3}, {"a": 2, "b": 4}]
out: [1, 2, 3, 4]

- q: '.[][0, 1]'
in: [[1, 3],[2, 4]]
out: [1, 2, 3, 4]

- q: '.[][0,1:1,2]'
in: [[1, 3],[2, 4]]
out:
- [1]
- [2]
- [1, 3]
- [2, 4]
- []
- []
- [3]
- [4]

- q: '.[]."\("a","b")"'
in: [{"a": 1, "b": 3}, {"a": 2, "b": 4}]
out: [1, 2, 3, 4]

0 comments on commit 5837272

Please sign in to comment.