Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wnm3 committed May 24, 2024
1 parent 343c0f1 commit 3006650
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
}
if (argArray == null) {
return null;
} else if (useContext == false) {
FunctionUtils.validateArguments(ERR_ARG_TYPE, expressionVisitor, ctx, 0, getSignature());
}
if (argArray.isArray() == false) {
argArray = ExpressionsVisitor.ensureArray(argArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
}
if (argArray == null) {
return null;
} else if (useContext == false) {
FunctionUtils.validateArguments(ERR_ARG_TYPE, expressionVisitor, ctx, 0, getSignature());
}
if (argArray.isArray() == false) {
argArray = ExpressionsVisitor.ensureArray(argArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class SumFunction extends FunctionBase {

public static String ERR_BAD_CONTEXT = String.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_SUM);
public static final String ERR_ARG1ARRTYPE = String.format(Constants.ERR_MSG_ARG1_MUST_BE_ARRAY_OF_NUMBER,
public static final String ERR_ARG_TYPE = String.format(Constants.ERR_MSG_ARG1_MUST_BE_ARRAY_OF_NUMBER,
Constants.FUNCTION_SUM);
public static String ERR_ARG1BADTYPE = String.format(Constants.ERR_MSG_ARG1_BAD_TYPE, Constants.FUNCTION_SUM);
public static String ERR_ARG2BADTYPE = String.format(Constants.ERR_MSG_ARG2_BAD_TYPE, Constants.FUNCTION_SUM);
Expand All @@ -60,12 +60,15 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
if (argCount == 1) {
if (!useContext) {
argArray = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, 0);
FunctionUtils.validateArguments(ERR_ARG1ARRTYPE, expressionVisitor, ctx, 0, getSignature());
}
// if arg is an array, sum its values

if (argArray == null) {
return null;
} else if (argArray.isArray()) {
return null;
} else if (useContext == false) {
FunctionUtils.validateArguments(ERR_ARG_TYPE, expressionVisitor, ctx, 0, getSignature());
}
// if arg is an array, sum its values
if (argArray.isArray()) {
ArrayNode arr = (ArrayNode) argArray;

// if ALL of the array members are integral, return as a LongNode
Expand All @@ -82,7 +85,7 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
} else if (!a.isIntegralNumber()) {
// also complain if any non-numeric types are included in the
// array
throw new EvaluateRuntimeException(ERR_ARG1ARRTYPE);
throw new EvaluateRuntimeException(ERR_ARG_TYPE);
}
}

Expand All @@ -105,7 +108,7 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
} else if (argArray.isFloatingPointNumber() || argArray.isDouble()) {
return new DoubleNode(argArray.asDouble());
} else {
throw new EvaluateRuntimeException(ERR_ARG1ARRTYPE);
throw new EvaluateRuntimeException(ERR_ARG_TYPE);
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ public static boolean checkArgument(ExpressionsVisitor exprVisitor, ExprContext
return true;
} else if (exprCtx instanceof IdContext) {
return true;
} else if (exprCtx instanceof Function_callContext) {
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ public void testSumFunction() throws Exception {
e.evaluate(null);
Assert.fail("Expected exception was not thrown for param: " + param);
} catch (EvaluateException ex) {
Assert.assertEquals(SumFunction.ERR_ARG1ARRTYPE, ex.getMessage());
Assert.assertEquals(SumFunction.ERR_ARG_TYPE, ex.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public class SumFunctionTests implements Serializable {
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{
"$sum({})", null, SumFunction.ERR_ARG1ARRTYPE
"$sum({})", null, SumFunction.ERR_ARG_TYPE
}, //
{
"$sum(null)", null, SumFunction.ERR_ARG1ARRTYPE
"$sum(null)", null, SumFunction.ERR_ARG_TYPE
}, //
{
"$sum([])", "0", null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ public void testSumFunction() throws Exception {
e.evaluate(null);
Assert.fail("Expected exception was not thrown for param: " + param);
} catch (EvaluateException ex) {
Assert.assertEquals(SumFunction.ERR_ARG1ARRTYPE, ex.getMessage());
Assert.assertEquals(SumFunction.ERR_ARG_TYPE, ex.getMessage());
}
}
}
Expand Down

0 comments on commit 3006650

Please sign in to comment.