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..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 @@ -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; @@ -53,26 +54,46 @@ public interface ValueWithPath { Path path(); } - private static class ValueWithPathImpl implements ValueWithPath { - @JsonProperty("value") - private final JsonNode value; - - @JsonProperty("path") + private static abstract class AbstractValueWithPath implements ValueWithPath { private final Path path; - - public ValueWithPathImpl(final JsonNode 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 JsonNode value() { - return 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; } } @@ -139,12 +160,22 @@ 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) { 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 ValueSupplierImpl(value, path)); + } public ValueWithPath getValueWithPath(final String name) { if (values != null) {