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

builtins/0: add builtins function #130

Merged
merged 1 commit into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions jackson-jq/src/main/java/net/thisptr/jackson/jq/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public Function getFunction(final String name, final int nargs) {
return getFunctionRecursive(name);
}

public Map<String, Function> getLocalFunctions() {
if (functions == null)
return new HashMap<>();
return new HashMap<>(functions);
}

public Scope getParentScope() {
return parentScope;
}

private Function getFunctionRecursive(final String name) {
if (functions != null) {
final Function q = functions.get(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.thisptr.jackson.jq.internal.functions;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.google.auto.service.AutoService;

import net.thisptr.jackson.jq.BuiltinFunction;
import net.thisptr.jackson.jq.Expression;
import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.PathOutput;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.Version;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.path.Path;

@AutoService(Function.class)
@BuiltinFunction("builtins/0")
public class BuiltinsFunction implements Function {

@Override
public void apply(Scope scope, final List<Expression> args, final JsonNode in, final Path path, final PathOutput output, final Version version) throws JsonQueryException {
// root scope
while (scope.getParentScope() != null)
scope = scope.getParentScope();

final List<String> builtins = new ArrayList<>(scope.getLocalFunctions().keySet());
Collections.sort(builtins);
output.emit(scope.getObjectMapper().valueToTree(builtins), null);
}
}