Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-preview.20210610' into master/1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
eiiches committed Jun 9, 2021
2 parents 4585bc7 + 2a0a4e7 commit d373805
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 60 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Usage

First, you need Java 8 or later.

If you use Maven, add the following snippet to the `<dependencies>` section of your POM. For instructions for other build tools (Gradle, etc.), visit [jackson-jq](https://search.maven.org/artifact/net.thisptr/jackson-jq/1.0.0-preview.20201123/jar) on search.maven.org.
If you use Maven, add the following snippet to the `<dependencies>` section of your POM. For instructions for other build tools (Gradle, etc.), visit [jackson-jq](https://search.maven.org/artifact/net.thisptr/jackson-jq/1.0.0-preview.20210610/jar) on search.maven.org.

```xml
<dependency>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</dependency>
```

Expand All @@ -30,29 +30,29 @@ Using a jackson-jq command line tool
To test a query quickly, we provide jackson-jq CLI.

```sh
$ curl -LO https://repo1.maven.org/maven2/net/thisptr/jackson-jq-cli/1.0.0-preview.20201123/jackson-jq-cli-1.0.0-preview.20201123.jar
$ curl -LO https://repo1.maven.org/maven2/net/thisptr/jackson-jq-cli/1.0.0-preview.20210610/jackson-jq-cli-1.0.0-preview.20210610.jar

$ java -jar jackson-jq-cli-1.0.0-preview.20201123.jar --help
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar --help
usage: jackson-jq [OPTIONS...] QUERY
-c,--compact compact instead of pretty-printed output
-h,--help print this message
--jq <arg> specify jq version
-n,--null-input use `null` as the single input value
-r,--raw output raw strings, not JSON texts

$ java -jar jackson-jq-cli-1.0.0-preview.20201123.jar '.foo'
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar '.foo'
{"foo": 42}
42
```

To test a query with a specific jq version,

```sh
$ java -jar jackson-jq-cli-1.0.0-preview.20201123.jar --jq 1.5 'join("-")'
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar --jq 1.5 'join("-")'
["1", 2]
jq: error: string ("-") and number (2) cannot be added

$ java -jar jackson-jq-cli-1.0.0-preview.20201123.jar --jq 1.6 'join("-")' # jq-1.6 can join any values, not only strings
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar --jq 1.6 'join("-")' # jq-1.6 can join any values, not only strings
["1", 2]
"1-2"
```
Expand Down Expand Up @@ -235,7 +235,7 @@ Using jackson-jq-extra module
<dependency>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq-extra</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions jackson-jq-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<parent>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq-parent</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</parent>

<dependencies>
<dependency>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq-extra</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down
4 changes: 2 additions & 2 deletions jackson-jq-extra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<parent>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq-parent</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</parent>

<dependencies>
<dependency>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</dependency>
</dependencies>

Expand Down
4 changes: 2 additions & 2 deletions jackson-jq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>net.thisptr</groupId>
<artifactId>jackson-jq-parent</artifactId>
<version>1.0.0-preview.20201123</version>
<version>1.0.0-preview.20210610</version>
</parent>

<dependencies>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<version>30.1.1-jre</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,36 @@ public static <T, U> List<U> _2(final List<Pair<T, U>> items) {
public String toString() {
return String.format("(%s, %s)", _1, _2);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_1 == null) ? 0 : _1.hashCode());
result = prime * result + ((_2 == null) ? 0 : _2.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("rawtypes")
final Pair other = (Pair) obj;
if (_1 == null) {
if (other._1 != null)
return false;
} else if (!_1.equals(other._1))
return false;
if (_2 == null) {
if (other._2 != null)
return false;
} else if (!_2.equals(other._2))
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void apply(final Scope scope, final JsonNode in, final Path ipath, final
accumulators[0] = newaccumulator;
accumulatorPaths[0] = newaccumulatorPath;
}, extractExpr != null ? false : requirePath);
}, stack, true);
}, stack);
}, requirePath);
}, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void pathRecursive(final Scope scope, final JsonNode in, final Pa
childScope.setValueWithPath(var.name, var.value, var.path);
}
pathRecursive(childScope, in, path, output, tail, requirePath);
}, accumulate, true);
}, accumulate);
});
} else if (head instanceof TransformPipeComponent) {
((TransformPipeComponent) head).expr.apply(scope, in, path, (pobj, ppath) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void apply(final Scope scope, final JsonNode in, final Path ipath, final
final List<JsonNode> reduceResult = new ArrayList<>();
reduceExpr.apply(childScope, accumulators[0], reduceResult::add);
accumulators[0] = reduceResult.isEmpty() ? NullNode.getInstance() : reduceResult.get(reduceResult.size() - 1);
}, stack, true);
}, stack);
});

output.emit(accumulators[0], null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import net.thisptr.jackson.jq.path.Path;

public interface PatternMatcher {

void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, final boolean emit) throws JsonQueryException;
void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate) throws JsonQueryException;

public static class MatchWithPath {
public final String name;
Expand All @@ -31,5 +30,5 @@ public interface MatchOutput {
void emit(List<MatchWithPath> vars) throws JsonQueryException;
}

void matchWithPath(Scope scope, JsonNode in, Path path, MatchOutput output, Stack<MatchWithPath> accumulate, boolean emit) throws JsonQueryException;
void matchWithPath(Scope scope, JsonNode in, Path path, MatchOutput output, Stack<MatchWithPath> accumulate) throws JsonQueryException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,47 @@ public ArrayMatcher(final List<PatternMatcher> matchers) {
this.matchers = matchers;
}

private void recursive(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, int index) throws JsonQueryException {
if (index >= matchers.size()) {
out.accept(accumulate);
return;
}

final PatternMatcher matcher = matchers.get(index);
final JsonNode value = in.get(index);

matcher.match(scope, value != null ? value : NullNode.getInstance(), (match) -> {
recursive(scope, in, out, accumulate, index + 1);
}, accumulate);
}

@Override
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, final boolean emit) throws JsonQueryException {
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate) throws JsonQueryException {
if (!in.isArray() && !in.isNull())
throw new JsonQueryTypeException("Cannot index %s with number", in.getNodeType());
for (int i = 0; i < matchers.size(); ++i) {
final PatternMatcher matcher = matchers.get(i);
final JsonNode item = in.get(i);
matcher.match(scope, item != null ? item : NullNode.getInstance(), out, accumulate, emit && i == matchers.size() - 1);
recursive(scope, in, out, accumulate, 0);
}

private void recursiveWithPath(final Scope scope, final JsonNode in, final Path path, final MatchOutput out, final Stack<MatchWithPath> accumulate, int index) throws JsonQueryException {
if (index >= matchers.size()) {
out.emit(accumulate);
return;
}

final PatternMatcher matcher = matchers.get(index);
final JsonNode value = in.get(index);
final ArrayIndexPath valuePath = ArrayIndexPath.chainIfNotNull(path, index);

matcher.matchWithPath(scope, value != null ? value : NullNode.getInstance(), valuePath, (match) -> {
recursiveWithPath(scope, in, path, out, accumulate, index + 1);
}, accumulate);
}

@Override
public void matchWithPath(final Scope scope, final JsonNode in, final Path path, final MatchOutput output, final Stack<MatchWithPath> accumulate, final boolean emit) throws JsonQueryException {
public void matchWithPath(final Scope scope, final JsonNode in, final Path path, final MatchOutput out, final Stack<MatchWithPath> accumulate) throws JsonQueryException {
if (!in.isArray() && !in.isNull())
throw new JsonQueryTypeException("Cannot index %s with number", in.getNodeType());
for (int i = 0; i < matchers.size(); ++i) {
final PatternMatcher matcher = matchers.get(i);
final JsonNode item = in.get(i);
matcher.matchWithPath(scope, item != null ? item : NullNode.getInstance(), ArrayIndexPath.chainIfNotNull(path, i), output, accumulate, emit && i == matchers.size() - 1);
}
recursiveWithPath(scope, in, path, out, accumulate, 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public ObjectMatcher(final List<Pair<Expression, PatternMatcher>> matchers) {
this.matchers = matchers;
}

private void recursive(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, final boolean emit, int index) throws JsonQueryException {
if (index >= matchers.size())
private void recursive(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, int index) throws JsonQueryException {
if (index >= matchers.size()) {
out.accept(accumulate);
return;
}

final Pair<Expression, PatternMatcher> kvexpr = matchers.get(index);
final Expression keyexpr = kvexpr._1;
Expand All @@ -38,15 +40,18 @@ private void recursive(final Scope scope, final JsonNode in, final Functional.Co
final JsonNode value = in.get(key.asText());

final int size = accumulate.size();
matcher.match(scope, value != null ? value : NullNode.getInstance(), out, accumulate, emit && index == matchers.size() - 1);
recursive(scope, in, out, accumulate, emit, index + 1);
matcher.match(scope, value != null ? value : NullNode.getInstance(), (match) -> {
recursive(scope, in, out, accumulate, index + 1);
}, accumulate);
accumulate.setSize(size);
});
}

private void recursiveWithPath(final Scope scope, final JsonNode in, final Path inpath, final MatchOutput output, final Stack<MatchWithPath> accumulate, final boolean emit, int index) throws JsonQueryException {
if (index >= matchers.size())
private void recursiveWithPath(final Scope scope, final JsonNode in, final Path inpath, final MatchOutput output, final Stack<MatchWithPath> accumulate, int index) throws JsonQueryException {
if (index >= matchers.size()) {
output.emit(accumulate);
return;
}

final Pair<Expression, PatternMatcher> kvexpr = matchers.get(index);
final Expression keyexpr = kvexpr._1;
Expand All @@ -60,26 +65,27 @@ private void recursiveWithPath(final Scope scope, final JsonNode in, final Path
final Path valuepath = ObjectFieldPath.chainIfNotNull(inpath, key.asText());

final int size = accumulate.size();
matcher.matchWithPath(scope, value != null ? value : NullNode.getInstance(), valuepath, output, accumulate, emit && index == matchers.size() - 1);
recursiveWithPath(scope, in, inpath, output, accumulate, emit, index + 1);
matcher.matchWithPath(scope, value != null ? value : NullNode.getInstance(), valuepath, (match) -> {
recursiveWithPath(scope, in, inpath, output, accumulate, index + 1);
}, accumulate);
accumulate.setSize(size);
});
}

@Override
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, final boolean emit) throws JsonQueryException {
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate) throws JsonQueryException {
if (!in.isObject() && !in.isNull())
throw new JsonQueryTypeException("Cannot index %s with string", in.getNodeType());

recursive(scope, in, out, accumulate, emit, 0);
recursive(scope, in, out, accumulate, 0);
}

@Override
public void matchWithPath(Scope scope, JsonNode in, Path path, MatchOutput output, Stack<MatchWithPath> accumulate, boolean emit) throws JsonQueryException {
public void matchWithPath(Scope scope, JsonNode in, Path path, MatchOutput output, Stack<MatchWithPath> accumulate) throws JsonQueryException {
if (!in.isObject() && !in.isNull())
throw new JsonQueryTypeException("Cannot index %s with string", in.getNodeType());

recursiveWithPath(scope, in, path, output, accumulate, emit, 0);
recursiveWithPath(scope, in, path, output, accumulate, 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ public ValueMatcher(final String name) {
}

@Override
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate, final boolean emit) throws JsonQueryException {
public void match(final Scope scope, final JsonNode in, final Functional.Consumer<List<Pair<String, JsonNode>>> out, final Stack<Pair<String, JsonNode>> accumulate) throws JsonQueryException {
accumulate.push(Pair.of(name, in));
if (emit)
out.accept(accumulate);
out.accept(accumulate);
}

@Override
public void matchWithPath(final Scope scope, final JsonNode in, final Path path, final MatchOutput output, final Stack<MatchWithPath> accumulate, final boolean emit) throws JsonQueryException {
public void matchWithPath(final Scope scope, final JsonNode in, final Path path, final MatchOutput output, final Stack<MatchWithPath> accumulate) throws JsonQueryException {
accumulate.push(new MatchWithPath(name, in, path));
if (emit)
output.emit(accumulate);
output.emit(accumulate);
}

@Override
Expand Down
Loading

0 comments on commit d373805

Please sign in to comment.