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

adds configuration possibility to allow/disallow to overwrite constants #328

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
7 changes: 7 additions & 0 deletions docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Example usage, showing all default configuration values:

```java
ExpressionConfiguration configuration = ExpressionConfiguration.builder()
.allowOverwriteConstants(true)
.arraysAllowed(true)
.dataAccessorSupplier(MapBasedDataAccessor::new)
.decimalPlacesRounding(ExpressionConfiguration.DECIMAL_PLACES_ROUNDING_UNLIMITED)
Expand All @@ -22,12 +23,18 @@ ExpressionConfiguration configuration = ExpressionConfiguration.builder()
.mathContext(ExpressionConfiguration.DEFAULT_MATH_CONTEXT)
.operatorDictionary(ExpressionConfiguration.StandardOperatorsDictionary)
.powerOfPrecedence(OperatorIfc.OPERATOR_PRECEDENCE_POWER)
.stripTrailingZeros(true)
.structuresAllowed(true)
.build();

Expression expression = new Expression("2.128 + a", configuration);
```

### Allow to Overwrite Constants

If set to true (default), then variables can be set that have the name of a constant. In that case,
the constant value will be removed and a variable value will be set.

### Arrays allowed

Specifies if the array index function is allowed (default is true). If set to false, the expression
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/ezylang/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ public void validate() throws ParseException {
* @return The Expression instance, to allow chaining of methods.
*/
public Expression with(String variable, Object value) {
if (constants.containsKey(variable)) {
if (configuration.isAllowOverwriteConstants()) {
constants.remove(variable);
} else {
throw new UnsupportedOperationException(
String.format("Can't set value for constant '%s'", variable));
}
}
getDataAccessor().setData(variable, new EvaluationValue(value));
return this;
}
Expand Down Expand Up @@ -281,7 +289,7 @@ public Expression and(String variable, Object value) {
*/
public Expression withValues(Map<String, Object> values) {
for (Map.Entry<String, Object> entry : values.entrySet()) {
getDataAccessor().setData(entry.getKey(), new EvaluationValue(entry.getValue()));
with(entry.getKey(), entry.getValue());
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ public class ExpressionConfiguration {
*/
@Builder.Default @Getter private final boolean stripTrailingZeros = true;

/**
* If set to true (default), then variables can be set that have the name of a constant. In that
* case, the constant value will be removed and a variable value will be set.
*/
@Builder.Default @Getter private final boolean allowOverwriteConstants = true;

/**
* Convenience method to create a default configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.ezylang.evalex;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
Expand Down Expand Up @@ -62,4 +63,28 @@ void testCustomConstantsMixedCase() throws EvaluationException, ParseException {

assertThat(expression.evaluate().getStringValue()).isEqualTo("6.4");
}

@Test
void testOverwriteConstantsWith() throws EvaluationException, ParseException {
Expression expression = new Expression("e");
assertThat(expression.with("e", 9).evaluate().getStringValue()).isEqualTo("9");
}

@Test
void testOverwriteConstantsWithValues() throws EvaluationException, ParseException {
Map<String, Object> values = new HashMap<>();
values.put("E", 6);
Expression expression = new Expression("e");
assertThat(expression.withValues(values).evaluate().getStringValue()).isEqualTo("6");
}

@Test
void testOverwriteConstantsNotAllowed() {
Expression expression =
new Expression(
"e", ExpressionConfiguration.builder().allowOverwriteConstants(false).build());
assertThatThrownBy(() -> expression.with("e", 9))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Can't set value for constant 'e'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void testDefaultSetup() {
assertThat(configuration.getDecimalPlacesRounding())
.isEqualTo(ExpressionConfiguration.DECIMAL_PLACES_ROUNDING_UNLIMITED);
assertThat(configuration.isStripTrailingZeros()).isTrue();
assertThat(configuration.isAllowOverwriteConstants()).isTrue();
}

@Test
Expand Down