All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.groupbyinc.common.jackson.jq.internal.JsonQueryFunction Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package net.thisptr.jackson.jq.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.misc.Preconditions;

import com.fasterxml.jackson.databind.JsonNode;

public class JsonQueryFunction implements Function {
	private JsonQuery body;
	private List params;
	private String name;
	private Scope closure;

	public JsonQueryFunction(final String name, final List params, final JsonQuery body, final Scope closure) {
		this.name = name;
		this.params = params;
		this.body = body;
		this.closure = closure;
	}

	@Override
	public List apply(final Scope scope, final List args, final JsonNode in) throws JsonQueryException {
		Preconditions.checkArgumentCount(name, args, params.size());

		final Scope fnScope = Scope.newChildScope(closure);
		fnScope.addFunction(name, params.size(), this);

		final List out = new ArrayList<>();
		applyRecursive(out, fnScope, scope, args, in, 0);
		return out;
	}

	private void applyRecursive(final List out, final Scope fnScope, final Scope scope, final List args, final JsonNode in, final int i) throws JsonQueryException {
		if (i == params.size()) {
			out.addAll(body.apply(fnScope, in));
		} else {
			final String param = params.get(i);
			if (param.startsWith("$")) {
				final String argname = param.substring(1);
				for (final JsonNode argvalue : args.get(i).apply(scope, in)) {
					fnScope.setValue(argname, argvalue);
					applyRecursive(out, fnScope, scope, args, in, i + 1);
				}
			} else {
				fnScope.addFunction(param, 0, new JsonQueryFunction(param, Collections. emptyList(), new FixedScopeQuery(scope, args.get(i)), fnScope));
				applyRecursive(out, fnScope, scope, args, in, i + 1);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy