jamesnetherton commented on code in PR #30376:
URL: https://github.com/apache/camel/pull/30376#discussion_r4625837945
##########
components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java:
##########
@@ -19,106 +19,167 @@
import java.util.List;
import java.util.Map;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.NullNode;
-import com.fasterxml.jackson.databind.node.TextNode;
-import net.thisptr.jackson.jq.BuiltinFunctionLoader;
-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.Versions;
-import net.thisptr.jackson.jq.exception.JsonQueryException;
-import net.thisptr.jackson.jq.internal.tree.FunctionCall;
-import net.thisptr.jackson.jq.path.Path;
+import net.thisptr.jackson.jq.v2.core.EnvironmentBuilder;
+import net.thisptr.jackson.jq.v2.core.version.Versions;
+import net.thisptr.jackson.jq.v2.json.JsonNodeType;
+import net.thisptr.jackson.jq.v2.json.JsonProvider;
+import net.thisptr.jackson.jq.v2.json.impl.jackson3.Jackson3JsonProvider;
+import net.thisptr.jackson.jq.v2.spi.BindContext;
+import net.thisptr.jackson.jq.v2.spi.Expression;
+import net.thisptr.jackson.jq.v2.spi.Function;
+import net.thisptr.jackson.jq.v2.spi.FunctionSignature;
+import net.thisptr.jackson.jq.v2.spi.Output;
+import net.thisptr.jackson.jq.v2.spi.RuntimeContext;
+import net.thisptr.jackson.jq.v2.spi.annotations.FunctionRegistration;
+import net.thisptr.jackson.jq.v2.spi.exception.JsonQueryException;
+import net.thisptr.jackson.jq.v2.spi.path.Path;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.StreamCache;
import org.apache.camel.util.concurrent.ContextValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
+import tools.jackson.databind.JsonNode;
+
+/**
+ * The Camel supplied jq functions.
+ * <p/>
+ * A new function must be annotated with {@link FunctionRegistration}, listed
in
+ * {@code META-INF/services/net.thisptr.jackson.jq.v2.spi.Function} and added
to {@link #CAMEL_FUNCTIONS}. The
+ * {@code ServiceLoader} lookup brings the functions to an environment that
Camel does not build, such as one from a CDI
+ * producer, as a built environment cannot be changed. There a function of the
same signature in another jar may win, as
+ * jackson-jq resolves such a clash in classpath order.
+ */
public final class JqFunctions {
public static final ContextValue<Exchange> EXCHANGE_LOCAL =
ContextValue.newThreadLocal("JqExchange");
private static final Logger LOGGER =
LoggerFactory.getLogger(JqFunctions.class);
+ private static final Map<FunctionSignature, Function> CAMEL_FUNCTIONS =
Map.of(
+ FunctionSignature.of(Header.NAME, 1), new Header(),
+ FunctionSignature.of(Header.NAME, 2), new Header(),
+ FunctionSignature.of(Property.NAME, 1), new Property(),
+ FunctionSignature.of(Property.NAME, 2), new Property(),
+ FunctionSignature.of(Constant.NAME, 1), new Constant(),
+ FunctionSignature.of(Constant.NAME, 2), new Constant(),
+ FunctionSignature.of(Variable.NAME, 1), new Variable(),
+ FunctionSignature.of(Variable.NAME, 2), new Variable(),
+ FunctionSignature.of(Body.NAME, 0), new Body());
+
private JqFunctions() {
}
- public static void load(CamelContext camelContext, Scope scope) {
- Map<String, Function> fromServiceLoader =
BuiltinFunctionLoader.getInstance()
- .loadFunctionsFromServiceLoader(
- camelContext.getApplicationContextClassLoader() != null
- ?
camelContext.getApplicationContextClassLoader()
- : BuiltinFunctionLoader.class.getClassLoader(),
- Versions.JQ_1_7);
-
- Map<String, Function> fromJq = BuiltinFunctionLoader.getInstance()
- .loadFunctionsFromJsonJq(
- camelContext.getApplicationContextClassLoader() != null
- ?
camelContext.getApplicationContextClassLoader()
- : BuiltinFunctionLoader.class.getClassLoader(),
- Versions.JQ_1_7,
- scope);
+ /**
+ * Creates an environment builder with the jq builtins, the functions and
modules found on the classpath, and the
+ * Camel functions in this class. The Camel functions are defined on the
builder, so they win over a function of the
+ * same signature in another jar.
+ */
+ public static EnvironmentBuilder<JsonNode>
newEnvironmentBuilder(CamelContext camelContext) {
+ ClassLoader classLoader = camelContext != null &&
camelContext.getApplicationContextClassLoader() != null
+ ? camelContext.getApplicationContextClassLoader()
+ : JqFunctions.class.getClassLoader();
+ EnvironmentBuilder<JsonNode> builder
+ =
EnvironmentBuilder.withDefaultLoaders(Jackson3JsonProvider.getInstance(),
Versions.JQ_1_7, classLoader);
+ CAMEL_FUNCTIONS.forEach(builder::defineFunction);
+ return builder;
+ }
- if (fromServiceLoader != null) {
- LOGGER.debug("Loading {} jq functions from ServiceLoader",
fromServiceLoader.size());
- fromServiceLoader.forEach(scope::addFunction);
+ /**
+ * Adds the functions bound in the registry. A function bound as {@code
name/arity} gets that arity, otherwise it is
+ * variadic. A function with the signature of a Camel function is skipped,
as the Camel function wins.
+ */
+ public static void loadFromRegistry(CamelContext camelContext,
EnvironmentBuilder<JsonNode> builder) {
+ Map<String, Function> fromRegistry =
camelContext.getRegistry().findByTypeWithName(Function.class);
+ if (fromRegistry != null) {
+ LOGGER.debug("Loading {} jq functions from Registry",
fromRegistry.size());
+ fromRegistry.forEach((name, function) -> {
+ FunctionSignature signature = signature(name);
+ if (CAMEL_FUNCTIONS.containsKey(signature)) {
+ LOGGER.warn("Ignoring the jq function {} from the
registry, as it has the name of a Camel jq function",
+ name);
+ } else {
+ builder.defineFunction(signature, function);
+ }
+ });
}
+ }
- if (fromJq != null) {
- LOGGER.debug("Loading {} jq functions from Json JQ",
fromJq.size());
- fromJq.forEach(scope::addFunction);
+ private static FunctionSignature signature(String name) {
+ int pos = name.lastIndexOf('/');
+ if (pos > 0) {
+ return FunctionSignature.of(name.substring(0, pos),
Integer.parseInt(name.substring(pos + 1)));
Review Comment:
Done: a registry name with a non-numeric arity now fails with an
`IllegalArgumentException` naming the expected `name/arity` form, covered by
`JqRegistryFunctionTest.testInvalidArityFromRegistry`.
_Claude Code on behalf of jamesnetherton_
##########
components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java:
##########
@@ -19,106 +19,167 @@
import java.util.List;
import java.util.Map;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.NullNode;
-import com.fasterxml.jackson.databind.node.TextNode;
-import net.thisptr.jackson.jq.BuiltinFunctionLoader;
-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.Versions;
-import net.thisptr.jackson.jq.exception.JsonQueryException;
-import net.thisptr.jackson.jq.internal.tree.FunctionCall;
-import net.thisptr.jackson.jq.path.Path;
+import net.thisptr.jackson.jq.v2.core.EnvironmentBuilder;
+import net.thisptr.jackson.jq.v2.core.version.Versions;
+import net.thisptr.jackson.jq.v2.json.JsonNodeType;
+import net.thisptr.jackson.jq.v2.json.JsonProvider;
+import net.thisptr.jackson.jq.v2.json.impl.jackson3.Jackson3JsonProvider;
+import net.thisptr.jackson.jq.v2.spi.BindContext;
+import net.thisptr.jackson.jq.v2.spi.Expression;
+import net.thisptr.jackson.jq.v2.spi.Function;
+import net.thisptr.jackson.jq.v2.spi.FunctionSignature;
+import net.thisptr.jackson.jq.v2.spi.Output;
+import net.thisptr.jackson.jq.v2.spi.RuntimeContext;
+import net.thisptr.jackson.jq.v2.spi.annotations.FunctionRegistration;
+import net.thisptr.jackson.jq.v2.spi.exception.JsonQueryException;
+import net.thisptr.jackson.jq.v2.spi.path.Path;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.StreamCache;
import org.apache.camel.util.concurrent.ContextValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
+import tools.jackson.databind.JsonNode;
+
+/**
+ * The Camel supplied jq functions.
+ * <p/>
+ * A new function must be annotated with {@link FunctionRegistration}, listed
in
+ * {@code META-INF/services/net.thisptr.jackson.jq.v2.spi.Function} and added
to {@link #CAMEL_FUNCTIONS}. The
+ * {@code ServiceLoader} lookup brings the functions to an environment that
Camel does not build, such as one from a CDI
+ * producer, as a built environment cannot be changed. There a function of the
same signature in another jar may win, as
+ * jackson-jq resolves such a clash in classpath order.
+ */
public final class JqFunctions {
public static final ContextValue<Exchange> EXCHANGE_LOCAL =
ContextValue.newThreadLocal("JqExchange");
private static final Logger LOGGER =
LoggerFactory.getLogger(JqFunctions.class);
+ private static final Map<FunctionSignature, Function> CAMEL_FUNCTIONS =
Map.of(
+ FunctionSignature.of(Header.NAME, 1), new Header(),
+ FunctionSignature.of(Header.NAME, 2), new Header(),
+ FunctionSignature.of(Property.NAME, 1), new Property(),
+ FunctionSignature.of(Property.NAME, 2), new Property(),
+ FunctionSignature.of(Constant.NAME, 1), new Constant(),
+ FunctionSignature.of(Constant.NAME, 2), new Constant(),
+ FunctionSignature.of(Variable.NAME, 1), new Variable(),
+ FunctionSignature.of(Variable.NAME, 2), new Variable(),
+ FunctionSignature.of(Body.NAME, 0), new Body());
Review Comment:
Keeping `Map.of`: going over the limit would fail to compile straight away,
so there is nothing that could break at runtime.
_Claude Code on behalf of jamesnetherton_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]