Skip to content

Commit

Permalink
Merge pull request #378 from fjtirado/Fix_#221
Browse files Browse the repository at this point in the history
[Fix_#221] Add Supplier<JsonNode> to scope
  • Loading branch information
eiiches authored Nov 14, 2024
2 parents 70dc5d4 + fe9723f commit 8254579
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 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 @@ -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<JsonNode> valueSupplier;

public ValueSupplierImpl(final Supplier<JsonNode> 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;
}
}

Expand Down Expand Up @@ -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<JsonNode> 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<JsonNode> 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) {
Expand Down

0 comments on commit 8254579

Please sign in to comment.