Skip to content

Commit

Permalink
allows all array types as input parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
uklimaschewski committed Mar 18, 2024
1 parent 61b80c7 commit a77e597
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ArrayConverter implements ConverterIfc {
public EvaluationValue convert(Object object, ExpressionConfiguration configuration) {
List<EvaluationValue> list = new ArrayList<>();

if (object instanceof Object[]) {
if (object.getClass().isArray()) {
for (Object element : (Object[]) object) {
list.add(new EvaluationValue(element, configuration));
}
Expand All @@ -41,6 +41,6 @@ public EvaluationValue convert(Object object, ExpressionConfiguration configurat

@Override
public boolean canConvert(Object object) {
return object instanceof List || object instanceof Object[];
return object instanceof List || object.getClass().isArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* <tr><td>Duration</td><td>DurationConverter</td></tr>
* <tr><td>ASTNode</td><td>ASTNode</td></tr>
* <tr><td>List&lt;?&gt;</td><td>ArrayConverter - each entry will be converted</td></tr>
* <tr><td>Map&lt?,?&gt;</td><td>StructureConverter - each entry will be converted.</td></tr>
* <tr><td>Map&lt?,?&gt;</td><td>StructureConverter - each entry will be converted</td></tr>
* </table>
*
* <i>* Be careful with conversion problems when using float or double, which are fractional
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/ezylang/evalex/ExpressionEvaluatorArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ void testArrayAndList() throws EvaluationException, ParseException {
assertThat(expression.evaluate().getStringValue()).isEqualTo("4");
}

@Test
void testArrayTypes() throws EvaluationException, ParseException {
Expression expression =
createExpression("decimals[1] + integers[1] + doubles[1] + strings[1] + booleans[1]")
.with("decimals", new BigDecimal[] {new BigDecimal(1), new BigDecimal(2)})
.and("integers", new Integer[] {1, 2})
.and("doubles", new Double[] {1.1, 2.2})
.and("strings", new String[] {" Hello ", " World "})
.and("booleans", new Boolean[] {true, false});

assertThat(expression.evaluate().getStringValue()).isEqualTo("6.2 World false");
}

@Test
void testThrowsUnsupportedDataTypeForArray() {
assertThatThrownBy(() -> createExpression("a[0]").with("a", "aString").evaluate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void testNestedEvaluationValueNull() {

@Test
void testException() {
assertThatThrownBy(() -> converter.convertObject(new int[] {1, 2, 3}, defaultConfiguration))
assertThatThrownBy(() -> converter.convertObject(new Error(), defaultConfiguration))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unsupported data type '[I'");
.hasMessage("Unsupported data type 'java.lang.Error'");
}
}

0 comments on commit a77e597

Please sign in to comment.