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; }