Skip to content

Commit

Permalink
[Fix_#221] Add Supplier<JsonNode> to scope
Browse files Browse the repository at this point in the history
This allows lazy initialization of json variables.
  • Loading branch information
fjtirado committed Nov 13, 2024
1 parent 70dc5d4 commit 8271586
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,25 +49,25 @@ private Map<String, String> debugFunctions() {
private ModuleLoader moduleLoader;

public interface ValueWithPath {
JsonNode value();
Supplier<JsonNode> value();

Path path();
}

private static class ValueWithPathImpl implements ValueWithPath {
@JsonProperty("value")
private final JsonNode value;
private final Supplier<JsonNode> value;

@JsonProperty("path")
private final Path path;

public ValueWithPathImpl(final JsonNode value, final Path path) {
public ValueWithPathImpl(final Supplier<JsonNode> value, final Path path) {
this.value = value;
this.path = path;
}

@Override
public JsonNode value() {
public Supplier<JsonNode> value() {
return value;
}

Expand Down Expand Up @@ -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<JsonNode> 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<JsonNode> value, final Path path) {
if (values == null)
values = new HashMap<>();
values.put(name, new ValueWithPathImpl(value, path));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 8271586

Please sign in to comment.