From 098381408849e1273eca0ad59752bc55210f75ae Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 21:58:32 +0000 Subject: [PATCH 1/5] [maven-release-plugin] prepare for next development iteration --- jackson-jq-cli/pom.xml | 4 ++-- jackson-jq-extra/pom.xml | 4 ++-- jackson-jq/pom.xml | 2 +- pom.xml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/jackson-jq-cli/pom.xml b/jackson-jq-cli/pom.xml index 3c03756..a37c324 100644 --- a/jackson-jq-cli/pom.xml +++ b/jackson-jq-cli/pom.xml @@ -8,14 +8,14 @@ net.thisptr jackson-jq-parent - 1.0.1 + 1.0.2-SNAPSHOT net.thisptr jackson-jq-extra - 1.0.1 + 1.0.2-SNAPSHOT commons-cli diff --git a/jackson-jq-extra/pom.xml b/jackson-jq-extra/pom.xml index f15f3ca..4f27129 100644 --- a/jackson-jq-extra/pom.xml +++ b/jackson-jq-extra/pom.xml @@ -8,14 +8,14 @@ net.thisptr jackson-jq-parent - 1.0.1 + 1.0.2-SNAPSHOT net.thisptr jackson-jq - 1.0.1 + 1.0.2-SNAPSHOT diff --git a/jackson-jq/pom.xml b/jackson-jq/pom.xml index 5260902..85f2b90 100644 --- a/jackson-jq/pom.xml +++ b/jackson-jq/pom.xml @@ -8,7 +8,7 @@ net.thisptr jackson-jq-parent - 1.0.1 + 1.0.2-SNAPSHOT diff --git a/pom.xml b/pom.xml index ff60c84..ef99a97 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net.thisptr jackson-jq-parent pom - 1.0.1 + 1.0.2-SNAPSHOT ${project.groupId}:${project.artifactId} jq for Jackson JSON Processor https://github.com/eiiches/jackson-jq @@ -28,7 +28,7 @@ scm:git:git@github.com:eiiches/jackson-jq.git scm:git:git@github.com:eiiches/jackson-jq.git git@github.com:juven/git-demo.git - 1.0.1 + HEAD From b5e7365c5cf72a633a29e29ea7a4f395bb04167e Mon Sep 17 00:00:00 2001 From: sullis Date: Tue, 15 Oct 2024 13:38:37 -0700 Subject: [PATCH 2/5] add end-to-end test for custom function --- .../jackson/jq/CustomFunctionTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 jackson-jq/src/test/java/net/thisptr/jackson/jq/CustomFunctionTest.java diff --git a/jackson-jq/src/test/java/net/thisptr/jackson/jq/CustomFunctionTest.java b/jackson-jq/src/test/java/net/thisptr/jackson/jq/CustomFunctionTest.java new file mode 100644 index 0000000..b394eb7 --- /dev/null +++ b/jackson-jq/src/test/java/net/thisptr/jackson/jq/CustomFunctionTest.java @@ -0,0 +1,51 @@ +package net.thisptr.jackson.jq; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.IntNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import net.thisptr.jackson.jq.exception.JsonQueryException; +import net.thisptr.jackson.jq.path.Path; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +// end-to-end test of custom function +public class CustomFunctionTest { + + @Test + public void testCustomFunction() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + Version version = Versions.JQ_1_6; + + Scope rootScope = Scope.newEmptyScope(); + + BuiltinFunctionLoader.getInstance().loadFunctions(version, rootScope); + + rootScope.addFunction("times100", 1, new Function() { + @Override + public void apply(Scope scope, List args, JsonNode in, Path path, PathOutput output, Version version) throws JsonQueryException { + args.get(0).apply(scope, in, (numberNode) -> { + assert (numberNode.isIntegralNumber()); + output.emit(new IntNode(numberNode.asInt() * 100), null); + }); + } + }); + + String input = "{ \"a\": 5 }"; + + Scope childScope = Scope.newChildScope(rootScope); + + JsonQuery query = JsonQuery.compile("{ \"a\": times100(.a) }", version); + + final List out = new ArrayList<>(); + query.apply(childScope, mapper.readTree(input), out::add); + assertThat(out).hasSize(1); + assertThat(out.get(0)).isInstanceOf(ObjectNode.class); + assertThat(out.get(0).toString()).isEqualTo("{\"a\":500}"); + } +} From 8271586d395749766fad8487a43fc3e4a5c929ff Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 13 Nov 2024 19:33:37 +0100 Subject: [PATCH 3/5] [Fix_#221] Add Supplier to scope This allows lazy initialization of json variables. --- .../java/net/thisptr/jackson/jq/Scope.java | 19 ++++++++++++++----- .../jq/internal/tree/VariableAccess.java | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java index 46e8e90..edc7989 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; +import java.util.function.Supplier; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -48,25 +49,25 @@ private Map debugFunctions() { private ModuleLoader moduleLoader; public interface ValueWithPath { - JsonNode value(); + Supplier value(); Path path(); } private static class ValueWithPathImpl implements ValueWithPath { @JsonProperty("value") - private final JsonNode value; + private final Supplier value; @JsonProperty("path") private final Path path; - public ValueWithPathImpl(final JsonNode value, final Path path) { + public ValueWithPathImpl(final Supplier value, final Path path) { this.value = value; this.path = path; } @Override - public JsonNode value() { + public Supplier value() { return value; } @@ -139,8 +140,16 @@ private Function getFunctionRecursive(final String name) { public void setValue(final String name, final JsonNode value) { setValueWithPath(name, value, null); } + + public void setValue (final String name, Supplier supplier) { + setValueWithPath (name, supplier, null); + } public void setValueWithPath(final String name, final JsonNode value, final Path path) { + setValueWithPath(name, () -> value, path); + } + + public void setValueWithPath(final String name, final Supplier value, final Path path) { if (values == null) values = new HashMap<>(); values.put(name, new ValueWithPathImpl(value, path)); @@ -161,7 +170,7 @@ public JsonNode getValue(final String name) { final ValueWithPath value = getValueWithPath(name); if (value == null) return null; - return value.value(); + return value.value().get(); } @JsonIgnore diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java index 95f627e..5968011 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java @@ -30,7 +30,7 @@ public void apply(final Scope scope, final JsonNode in, final Path path, final P } else { final ValueWithPath value = scope.getValueWithPath(name); if (value != null) { - output.emit(value.value(), null); + output.emit(value.value().get(), null); return; } From fe9723fe11fb383ba14ced24aff39ac7ae30f416 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Thu, 14 Nov 2024 11:49:58 +0100 Subject: [PATCH 4/5] [Fix_#221] Backward compatible approach --- .../java/net/thisptr/jackson/jq/Scope.java | 54 +++++++++++++------ .../jq/internal/tree/VariableAccess.java | 2 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java index edc7989..d8a5879 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java @@ -49,31 +49,51 @@ private Map debugFunctions() { private ModuleLoader moduleLoader; public interface ValueWithPath { - Supplier value(); + JsonNode value(); Path path(); } - private static class ValueWithPathImpl implements ValueWithPath { - @JsonProperty("value") - private final Supplier value; - - @JsonProperty("path") + private static abstract class AbstractValueWithPath implements ValueWithPath { private final Path path; - - public ValueWithPathImpl(final Supplier value, final Path path) { - this.value = value; + + public AbstractValueWithPath (Path path) { this.path = path; } + + @Override + public Path path() { + return path; + } + } + + private static class ValueSupplierImpl extends AbstractValueWithPath { + private final Supplier valueSupplier; + + public ValueSupplierImpl(final Supplier valueSupplier, final Path path) { + super(path); + this.valueSupplier = valueSupplier; + } @Override - public Supplier value() { - return value; + public JsonNode value() { + return valueSupplier.get(); + } + } + + private static class ValueWithPathImpl extends AbstractValueWithPath { + @JsonProperty("value") + private final JsonNode value; + + public ValueWithPathImpl(final JsonNode value, final Path path) { + super(path); + this.value = value; + } @Override - public Path path() { - return path; + public JsonNode value() { + return value; } } @@ -146,13 +166,15 @@ public void setValue (final String name, Supplier supplier) { } public void setValueWithPath(final String name, final JsonNode value, final Path path) { - setValueWithPath(name, () -> value, path); + if (values == null) + values = new HashMap<>(); + values.put(name, new ValueWithPathImpl(value, path)); } public void setValueWithPath(final String name, final Supplier value, final Path path) { if (values == null) values = new HashMap<>(); - values.put(name, new ValueWithPathImpl(value, path)); + values.put(name, new ValueSupplierImpl(value, path)); } public ValueWithPath getValueWithPath(final String name) { @@ -170,7 +192,7 @@ public JsonNode getValue(final String name) { final ValueWithPath value = getValueWithPath(name); if (value == null) return null; - return value.value().get(); + return value.value(); } @JsonIgnore diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java index 5968011..95f627e 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/VariableAccess.java @@ -30,7 +30,7 @@ public void apply(final Scope scope, final JsonNode in, final Path path, final P } else { final ValueWithPath value = scope.getValueWithPath(name); if (value != null) { - output.emit(value.value().get(), null); + output.emit(value.value(), null); return; } From bb00f814a074b2914c2bb086a50fda4d4a03ee11 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:59:27 +0000 Subject: [PATCH 5/5] [maven-release-plugin] prepare release 1.1.0 --- README.md | 44 ++++++++++++++++++++-------------------- jackson-jq-cli/pom.xml | 4 ++-- jackson-jq-extra/pom.xml | 4 ++-- jackson-jq/pom.xml | 2 +- pom.xml | 4 ++-- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index bd6e8f9..e44b309 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ Usage First, you need Java 8 or later. -If you use Maven, add the following snippet to the `` 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.1/jar) on search.maven.org. +If you use Maven, add the following snippet to the `` 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.1.0/jar) on search.maven.org. ```xml net.thisptr jackson-jq - 1.0.1 + 1.1.0 ``` @@ -32,9 +32,9 @@ To test a query quickly, we provide jackson-jq CLI. *Please note that jackson-jq is a Java library and the CLI is provided solely for debugging/testing purpose (and not for production). The command-line options might change without notice.* ```sh -$ curl -LO https://repo1.maven.org/maven2/net/thisptr/jackson-jq-cli/1.0.1/jackson-jq-cli-1.0.1.jar +$ curl -LO https://repo1.maven.org/maven2/net/thisptr/jackson-jq-cli/1.1.0/jackson-jq-cli-1.1.0.jar -$ java -jar jackson-jq-cli-1.0.1.jar --help +$ java -jar jackson-jq-cli-1.1.0.jar --help usage: jackson-jq [OPTIONS...] QUERY -c,--compact compact instead of pretty-printed output -h,--help print this message @@ -42,7 +42,7 @@ usage: jackson-jq [OPTIONS...] QUERY -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.1.jar '.foo' +$ java -jar jackson-jq-cli-1.1.0.jar '.foo' {"foo": 42} 42 ``` @@ -50,11 +50,11 @@ $ java -jar jackson-jq-cli-1.0.1.jar '.foo' To test a query with a specific jq version, ```sh -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.5 'join("-")' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.5 'join("-")' ["1", 2] jq: error: string ("-") and number (2) cannot be added -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.6 'join("-")' # jq-1.6 can join any values, not only strings +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.6 'join("-")' # jq-1.6 can join any values, not only strings ["1", 2] "1-2" ``` @@ -236,9 +236,9 @@ $ jq -n '1 + 3 as $a | ($a * 2)' # interpreted as 1 + (3 as $a | ($a * 2)) whereas jackson-jq consistently interprets them as `(1 + 3)` whether `as $a` is used or not: ```console -$ java -jar jackson-jq-cli-1.0.1.jar -n '1 + 3 | (. * 2)' # interpreted as (1 + 3) | (. * 2) +$ java -jar jackson-jq-cli-1.1.0.jar -n '1 + 3 | (. * 2)' # interpreted as (1 + 3) | (. * 2) 8 -$ java -jar jackson-jq-cli-1.0.1.jar -n '1 + 3 as $a | ($a * 2)' # interpreted as (1 + 3) as $a | ($a * 2) +$ java -jar jackson-jq-cli-1.1.0.jar -n '1 + 3 as $a | ($a * 2)' # interpreted as (1 + 3) as $a | ($a * 2) 8 ``` @@ -247,7 +247,7 @@ $ java -jar jackson-jq-cli-1.0.1.jar -n '1 + 3 as $a | ($a * 2)' # interpreted a ```console $ jq -n '1 + 3 as $a | ($a * 2)' # interpreted as 1 + (3 as $a | ($a * 2)) 7 -$ java -jar jackson-jq-cli-1.0.1.jar -n '1 + 3 as $a | ($a * 2)' # interpreted as (1 + 3) as $a | ($a * 2) +$ java -jar jackson-jq-cli-1.1.0.jar -n '1 + 3 as $a | ($a * 2)' # interpreted as (1 + 3) as $a | ($a * 2) 8 ``` @@ -274,7 +274,7 @@ If the function with the same is defined more than once at the same scope, jacks ```console $ jq -n 'def f: 1; def g: f; def f: 2; g' 1 -$ java -jar jackson-jq-cli-1.0.1.jar -n 'def f: 1; def g: f; def f: 2; g' +$ java -jar jackson-jq-cli-1.1.0.jar -n 'def f: 1; def g: f; def f: 2; g' 2 ``` @@ -283,7 +283,7 @@ $ java -jar jackson-jq-cli-1.0.1.jar -n 'def f: 1; def g: f; def f: 2; g' Avoid using the duplicate function name. ```console -$ java -jar jackson-jq-cli-1.0.1.jar -n 'def f1: 1; def g: f1; def f2: 2; g' +$ java -jar jackson-jq-cli-1.1.0.jar -n 'def f1: 1; def g: f1; def f2: 2; g' 1 ``` @@ -353,7 +353,7 @@ jq: error: Division by zero? at , line 1: jq: 1 compile error $ jq '. / 0' <<< 0 jq: error (at :1): number (0) and number (0) cannot be divided because the divisor is zero -$ java -jar jackson-jq-cli-1.0.1.jar -n '0 / 0' +$ java -jar jackson-jq-cli-1.1.0.jar -n '0 / 0' jq: error: number (0) and number (0) cannot be divided because the divisor is zero ``` @@ -386,9 +386,9 @@ $ jq-1.2 -n '[1,2,3] | ((.[] | select(. > 1)) |= empty)' 2, 3 ] -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.6 -n '[1,2,3] | ((.[] | select(. > 1)) |= empty)' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.6 -n '[1,2,3] | ((.[] | select(. > 1)) |= empty)' jq: error: `|= empty` is undefined. See https://github.com/stedolan/jq/issues/897 -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.5 -n '[1,2,3] | ((.[] | select(. > 1)) |= empty)' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.5 -n '[1,2,3] | ((.[] | select(. > 1)) |= empty)' jq: error: `|= empty` is undefined. See https://github.com/stedolan/jq/issues/897 ``` @@ -397,9 +397,9 @@ jq: error: `|= empty` is undefined. See https://github.com/stedolan/jq/issues/89 You can use `_modify/2` if you really want to the original behavior. ```console -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.6 -n '[1,2,3] | _modify((.[] | select(. > 1)); empty)' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.6 -n '[1,2,3] | _modify((.[] | select(. > 1)); empty)' [ 1, 3 ] -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.5 -n '[1,2,3] | _modify((.[] | select(. > 1)); empty)' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.5 -n '[1,2,3] | _modify((.[] | select(. > 1)); empty)' null ``` @@ -419,7 +419,7 @@ jq 1.5 ```console $ jq-1.5 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' ["foo"] -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.5 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.5 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' jq: error: Invalid path expression with result 1 ``` @@ -428,7 +428,7 @@ jq 1.6 ```console $ jq-1.6 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' jq: error (at :1): Invalid path expression with result 1 -$ java -jar jackson-jq-cli-1.0.1.jar --jq 1.6 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' +$ java -jar jackson-jq-cli-1.1.0.jar --jq 1.6 -c 'path(.foo as $a | $a)' <<< '{"foo": 1}' jq: error: Invalid path expression with result 1 ``` @@ -452,7 +452,7 @@ $ jq -n 'label $a | label $b | try (break $b) catch .' { "__jq": 1 } -$ java -jar jackson-jq-cli-1.0.1.jar -n 'label $a | label $b | try (break $b) catch .' +$ java -jar jackson-jq-cli-1.1.0.jar -n 'label $a | label $b | try (break $b) catch .' { "__jq" : 0 } @@ -482,7 +482,7 @@ $ jq-1.6 -n '"x" | indices("")' # stuck in infinite loop ^C $ jq-1.6-83-gb52fc10 -n '"x" | indices("")' [] -$ java -jar jackson-jq-cli-1.0.1.jar -n '"x" | indices("")' +$ java -jar jackson-jq-cli-1.1.0.jar -n '"x" | indices("")' [ ] ``` @@ -499,7 +499,7 @@ To use this module, you need to add the following Maven dependency and set `Buil net.thisptr jackson-jq-extra - 1.0.1 + 1.1.0 ``` diff --git a/jackson-jq-cli/pom.xml b/jackson-jq-cli/pom.xml index a37c324..2abf87b 100644 --- a/jackson-jq-cli/pom.xml +++ b/jackson-jq-cli/pom.xml @@ -8,14 +8,14 @@ net.thisptr jackson-jq-parent - 1.0.2-SNAPSHOT + 1.1.0 net.thisptr jackson-jq-extra - 1.0.2-SNAPSHOT + 1.1.0 commons-cli diff --git a/jackson-jq-extra/pom.xml b/jackson-jq-extra/pom.xml index 4f27129..20589df 100644 --- a/jackson-jq-extra/pom.xml +++ b/jackson-jq-extra/pom.xml @@ -8,14 +8,14 @@ net.thisptr jackson-jq-parent - 1.0.2-SNAPSHOT + 1.1.0 net.thisptr jackson-jq - 1.0.2-SNAPSHOT + 1.1.0 diff --git a/jackson-jq/pom.xml b/jackson-jq/pom.xml index 85f2b90..046aed3 100644 --- a/jackson-jq/pom.xml +++ b/jackson-jq/pom.xml @@ -8,7 +8,7 @@ net.thisptr jackson-jq-parent - 1.0.2-SNAPSHOT + 1.1.0 diff --git a/pom.xml b/pom.xml index ef99a97..9a4459f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net.thisptr jackson-jq-parent pom - 1.0.2-SNAPSHOT + 1.1.0 ${project.groupId}:${project.artifactId} jq for Jackson JSON Processor https://github.com/eiiches/jackson-jq @@ -28,7 +28,7 @@ scm:git:git@github.com:eiiches/jackson-jq.git scm:git:git@github.com:eiiches/jackson-jq.git git@github.com:juven/git-demo.git - HEAD + 1.1.0