Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

allows to pass any <String, ?> map to the withValues() function, instead of using a strict Object type for the value. #377

Merged
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
16 changes: 15 additions & 1 deletion docs/concepts/parsing_evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ passing it to the _withValues()_ method:
```java
Expression expression = new Expression("(a + b) * (a - b)");

Map<String, Object> values = new HashMap<>();
Map<String, Double> values = new HashMap<>();
values.put("a", 3.5);
values.put("b", 2.5);

Expand All @@ -111,6 +111,20 @@ System.out.println(result.getNumberValue()); // prints 6.00
The data conversion of the passed values will automatically be performed through the created
_EvaluationObject_.

The map can also hold data of different types:
```java
Expression expression = new Expression("a+b+c");

Map<String, Object> values = new HashMap<>();
values.put("a", true);
values.put("b", " ");
values.put("c", 24.7);

EvaluationValue result = expression.withValues(values).evaluate();

System.out.println(result.getStringValue()); // prints "true 24.7"
```

See chapter [Data Types](datatypes.html) for details on the conversion.

Another option to have EvalEx use your data is to define a custom data accessor.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ezylang/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ public Expression and(String variable, Object value) {
* @param values A map with variable values.
* @return The Expression instance, to allow chaining of methods.
*/
public Expression withValues(Map<String, Object> values) {
for (Map.Entry<String, Object> entry : values.entrySet()) {
public Expression withValues(Map<String, ?> values) {
for (Map.Entry<String, ?> entry : values.entrySet()) {
with(entry.getKey(), entry.getValue());
}
return this;
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/ezylang/evalex/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ void testWithValues() throws ParseException, EvaluationException {
assertThat(result.getStringValue()).isEqualTo("6");
}

@Test
void testWithValuesDoubleMap() throws ParseException, EvaluationException {
Expression expression = new Expression("a+b");

Map<String, Double> values = new HashMap<>();
values.put("a", 3.9);
values.put("b", 3.1);

EvaluationValue result = expression.withValues(values).evaluate();

assertThat(result.getStringValue()).isEqualTo("7");
}

@Test
void testWithValuesStringMap() throws ParseException, EvaluationException {
Expression expression = new Expression("a+b+c");

Map<String, String> values = new HashMap<>();
values.put("a", "Hello");
values.put("b", " ");
values.put("c", "world");

EvaluationValue result = expression.withValues(values).evaluate();

assertThat(result.getStringValue()).isEqualTo("Hello world");
}

@Test
void testWithValuesMixedMap() throws ParseException, EvaluationException {
Expression expression = new Expression("a+b+c");

Map<String, Object> values = new HashMap<>();
values.put("a", true);
values.put("b", " ");
values.put("c", 24.7);

EvaluationValue result = expression.withValues(values).evaluate();

assertThat(result.getStringValue()).isEqualTo("true 24.7");
}

@Test
void testDefaultExpressionOwnsOwnConfigurationEntries() {
Expression expression1 = new Expression("1+1");
Expand Down