Skip to content

Commit

Permalink
fix codes relying on deprecated Scope.rootScope()
Browse files Browse the repository at this point in the history
related to #17
  • Loading branch information
eiiches committed Jul 17, 2018
1 parent 14fb5e0 commit 41bda39
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void loadMacros(final ClassLoader classLoader, final String path) {
final List<JqJson> configs = loadConfig(classLoader, path);
for (final JqJson jqJson : configs) {
for (final JqJson.JqFuncDef def : jqJson.functions)
addFunction(def.name, def.args.size(), new JsonQueryFunction(def.name, def.args, JsonQuery.compile(def.body)));
addFunction(def.name, def.args.size(), new JsonQueryFunction(def.name, def.args, JsonQuery.compile(def.body), this));
}
} catch (final IOException e) {
throw new RuntimeException("Failed to load macros", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public JsonQueryFunction(final String name, final List<String> params, final Jso
this.closure = closure;
}

public JsonQueryFunction(final String name, final List<String> params, final JsonQuery body) {
this(name, params, body, Scope.rootScope());
}

@Override
public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException {
Preconditions.checkArgumentCount(name, args, params.size());
Expand All @@ -53,7 +49,7 @@ private void applyRecursive(final List<JsonNode> out, final Scope fnScope, final
applyRecursive(out, fnScope, scope, args, in, i + 1);
}
} else {
fnScope.addFunction(param, 0, new JsonQueryFunction(param, Collections.<String> emptyList(), new FixedScopeQuery(scope, args.get(i))));
fnScope.addFunction(param, 0, new JsonQueryFunction(param, Collections.<String> emptyList(), new FixedScopeQuery(scope, args.get(i)), fnScope));
applyRecursive(out, fnScope, scope, args, in, i + 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.internal.JsonQueryFunction;

import org.junit.Test;

Expand All @@ -19,11 +18,13 @@
public class JsonQueryFunctionTest {
@Test
public void test() throws JsonProcessingException, IOException {
final Scope scope = new Scope();
final ObjectMapper mapper = new ObjectMapper();

scope.addFunction("inc", 1, new JsonQueryFunction("inc", Arrays.asList("x"), JsonQuery.compile("x + 1")));
scope.addFunction("fib", 1, new JsonQueryFunction("fib", Arrays.asList("x"), JsonQuery.compile("if x == 0 then 0 elif x == 1 then 1 else fib(x-1) + fib(x-2) end")));
final Scope scope = Scope.newEmptyScope();
scope.loadFunctions(Scope.class.getClassLoader());

scope.addFunction("inc", 1, new JsonQueryFunction("inc", Arrays.asList("x"), JsonQuery.compile("x + 1"), scope));
scope.addFunction("fib", 1, new JsonQueryFunction("fib", Arrays.asList("x"), JsonQuery.compile("if x == 0 then 0 elif x == 1 then 1 else fib(x-1) + fib(x-2) end"), scope));
scope.addFunction("fib", 0, new JsonQueryFunction("fib", Arrays.<String> asList(), JsonQuery.compile("fib(.)"), scope));

assertEquals(Arrays.asList(mapper.readTree("2")), JsonQuery.compile("inc(1)").apply(scope, NullNode.getInstance()));
Expand Down

0 comments on commit 41bda39

Please sign in to comment.