Skip to content

Commit

Permalink
allows to pass any <String, ?> map to the withValues() function, inst…
Browse files Browse the repository at this point in the history
…ead of using a strict Object type for the value. (#377)
  • Loading branch information
uklimaschewski authored Apr 8, 2023
1 parent f51fe0a commit df8eeeb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
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

0 comments on commit df8eeeb

Please sign in to comment.