Skip to content

Commit db5d421

Browse files
committed
Issues-83: Custom function pipe error
fix: pipe call custom function with input data
1 parent 78ce14c commit db5d421

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2184,9 +2184,20 @@ public static class JFunction implements JFunctionCallable, JFunctionSignatureVa
21842184
Signature signature;
21852185
Method method;
21862186
Object methodInstance;
2187+
int numberOfArgs;
21872188

21882189
public JFunction(JFunctionCallable function, String signature) {
2190+
this(null, function, signature);
2191+
}
2192+
2193+
public JFunction(String functionName, JFunctionCallable function, String signature) {
2194+
this(functionName, function, signature, 0);
2195+
}
2196+
2197+
public JFunction(String functionName, JFunctionCallable function, String signature, int numberOfArgs) {
2198+
this.functionName = functionName;
21892199
this.function = function;
2200+
this.numberOfArgs = numberOfArgs;
21902201
if (signature!=null)
21912202
// use classname as default, gets overwritten once the function is registered
21922203
this.signature = new Signature(signature, function.getClass().getName());
@@ -2231,7 +2242,7 @@ public Object validate(Object args, Object context) {
22312242
}
22322243

22332244
public int getNumberOfArgs() {
2234-
return method != null ? method.getParameterTypes().length : 0;
2245+
return method != null ? method.getParameterTypes().length : numberOfArgs;
22352246
}
22362247
}
22372248

src/test/java/com/dashjoin/jsonata/CustomFunctionTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,48 @@ public Object call(Object input, List args) throws Throwable {
5858
Assertions.assertEquals("abc", expression.evaluate(Map.of("a", "a", "b", "b", "c", "c")));
5959
}
6060

61+
@Test
62+
public void testFunctionWithInputData() {
63+
JFunction addWithFactor = new JFunction("addWithFactor", (input, args) -> {
64+
var inputMap = (Map<String, Object>)input;
65+
String factor = inputMap.get("factor").toString();
66+
int factorInt = Integer.parseInt(factor);
67+
return ((Integer) args.get(0) + (Integer) args.get(1)) * factorInt;
68+
}, null);
69+
70+
var expression = Jsonata.jsonata("$addWithFactor(1, 2)");
71+
expression.registerFunction(addWithFactor.functionName, addWithFactor);
72+
Assertions.assertEquals(9, expression.evaluate(Map.of("factor", "3")));
73+
}
74+
75+
@Test
76+
public void testFunctionWithInputData2() {
77+
JFunction addWithFactor = new JFunction("multiply", (input, args) -> {
78+
Object first = args.get(0);
79+
Object second;
80+
if (args.size() > 1) {
81+
second = args.get(1);
82+
} else {
83+
var inputMap = (Map<String, Object>)input;
84+
String factor = inputMap.get("factor").toString();
85+
second = Integer.parseInt(factor);
86+
}
87+
return (Integer) first * (Integer) second;
88+
}, null, 2);
89+
90+
var expression = Jsonata.jsonata("$multiply(3) ~> $multiply(?, 2)");
91+
expression.registerFunction(addWithFactor.functionName, addWithFactor);
92+
Assertions.assertEquals(18, expression.evaluate(Map.of("factor", "3")));
93+
}
94+
95+
@Test
96+
public void testCustomFunctionPipe() {
97+
JFunction add = new JFunction("add", (input, args) -> (Integer) args.get(0) + (Integer) args.get(1), null, 2);
98+
var expression = Jsonata.jsonata("$add(1, 2) ~> $add(?, 3)");
99+
expression.registerFunction(add.functionName, add);
100+
Assertions.assertEquals(6, expression.evaluate(null));
101+
}
102+
61103
/**
62104
* Lambdas use no signature - in case of an error, a ClassCastException is thrown
63105
*/

0 commit comments

Comments
 (0)