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 1/2] [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 46e8e903..edc79898 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 95f627e8..5968011c 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 2/2] [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 edc79898..d8a58790 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 5968011c..95f627e8 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; }