Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix_#221] Add Supplier<JsonNode> to scope #378

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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