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

allows all array types as input parameter #447

Merged
merged 6 commits into from
Mar 18, 2024
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
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 All @@ -66,6 +66,7 @@ public class DefaultEvaluationValueConverter implements EvaluationValueConverter
new ArrayConverter(),
new StructureConverter());

@Override
public EvaluationValue convertObject(Object object, ExpressionConfiguration configuration) {

if (object == null) {
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,9 @@ void testNestedEvaluationValueNull() {

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