net.thisptr.jackson.jq.internal.tree.binaryop.assignment.Assignment Maven / Gradle / Ivy
package net.thisptr.jackson.jq.internal.tree.binaryop.assignment;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import net.thisptr.jackson.jq.Expression;
import net.thisptr.jackson.jq.PathOutput;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.misc.JsonNodeComparator;
import net.thisptr.jackson.jq.internal.misc.JsonNodeUtils;
import net.thisptr.jackson.jq.internal.tree.binaryop.BinaryOperatorExpression;
import net.thisptr.jackson.jq.path.Path;
import net.thisptr.jackson.jq.path.RootPath;
public class Assignment extends BinaryOperatorExpression {
public Assignment(final Expression lhs, final Expression rhs) {
super(lhs, rhs, "=");
}
@Override
public void apply(final Scope scope, final JsonNode in, final Path ipath, final PathOutput output, final boolean requirePath) throws JsonQueryException {
rhs.apply(scope, in, (rval) -> {
final List lpaths = new ArrayList<>();
lhs.apply(scope, in, RootPath.getInstance(), (lval, lpath) -> {
// `VALUE | path(VALUE) => []`
if (lpath == null && in.isValueNode() && JsonNodeComparator.getInstance().compare(in, lval) == 0)
lpath = RootPath.getInstance();
if (lpath == null)
throw new JsonQueryException("Invalid path expression with result %s", JsonNodeUtils.toString(lval));
lpaths.add(lpath);
}, true);
JsonNode out = in;
for (final Path lpath : lpaths)
out = lpath.mutate(out, (lval_) -> rval);
output.emit(out, null);
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy