-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add STRING_TEMPLATE function and add two more cases for STRING_CASE f…
…unction
- Loading branch information
Showing
6 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...urer-common/src/main/java/tao/dong/dataconjurer/common/evalex/StringTemplateFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tao.dong.dataconjurer.common.evalex; | ||
|
||
import com.ezylang.evalex.Expression; | ||
import com.ezylang.evalex.data.EvaluationValue; | ||
import com.ezylang.evalex.functions.AbstractFunction; | ||
import com.ezylang.evalex.functions.FunctionParameter; | ||
import com.ezylang.evalex.parser.Token; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.text.StringSubstitutor; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@FunctionParameter(name = "template") | ||
@FunctionParameter(name = "values", isVarArg = true) | ||
public class StringTemplateFunction extends AbstractFunction { | ||
|
||
private static final String DOUBLE_DOLLAR = "$$"; | ||
private static final String INTERNAL_REPLACE = "<???TEMP???>"; | ||
private static final String DOLLAR = "$"; | ||
|
||
@Override | ||
public EvaluationValue evaluate(Expression expression, Token token, EvaluationValue... evaluationValues) { | ||
var template = evaluationValues[0].getStringValue(); | ||
var modifiedTemplate = StringUtils.replace(template, DOUBLE_DOLLAR, INTERNAL_REPLACE); | ||
var values = evaluationValues[1].getArrayValue(); | ||
if (values.size() % 2 != 0) { | ||
throw new IllegalArgumentException("Invalid number of arguments for function STRING_TEMPLATE"); | ||
} | ||
|
||
final var valueMap = createValueMap(values); | ||
var substitutor = new StringSubstitutor(key -> valueMap.getOrDefault(key, "")); | ||
substitutor.setEnableUndefinedVariableException(false); | ||
substitutor.setValueDelimiter(""); | ||
var result = substitutor.replace(modifiedTemplate); | ||
var modifiedResult = StringUtils.replace(result, INTERNAL_REPLACE, DOLLAR); | ||
|
||
return EvaluationValue.stringValue(modifiedResult); | ||
} | ||
|
||
private Map<String, String> createValueMap(List<EvaluationValue> values) { | ||
Map<String, String> valueMap = new HashMap<>(); | ||
for (int i = 0; i < values.size(); i += 2) { | ||
valueMap.put(values.get(i).getStringValue(), values.get(i + 1).getStringValue()); | ||
} | ||
return valueMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...-common/src/test/java/tao/dong/dataconjurer/common/evalex/StringTemplateFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package tao.dong.dataconjurer.common.evalex; | ||
|
||
import com.ezylang.evalex.EvaluationException; | ||
import com.ezylang.evalex.Expression; | ||
import com.ezylang.evalex.config.ExpressionConfiguration; | ||
import com.ezylang.evalex.parser.ParseException; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class StringTemplateFunctionTest { | ||
|
||
private static final ExpressionConfiguration EXPRESSION_CONFIGURATION = ExpressionConfiguration.defaultConfiguration() | ||
.withAdditionalFunctions( | ||
Map.entry("STRING_TEMPLATE", new StringTemplateFunction()) | ||
); | ||
|
||
|
||
private static Stream<Arguments> evaluate() { | ||
return Stream.of( | ||
Arguments.of("hello${name}world", new String[]{"name", "new"}, "hellonewworld"), | ||
Arguments.of("Hello, ${name}!", new String[]{"name", "world"}, "Hello, world!"), | ||
Arguments.of("Hello, ${name}! How are you ${name}?", new String[]{"name", "world"}, "Hello, world! How are you world?"), | ||
Arguments.of("Hello, ${name}! How are you ${name1}?", new String[]{"name", "world", "name1", "today"}, "Hello, world! How are you today?"), | ||
Arguments.of("Hello, ${name}! How are you ${name1}?", new String[]{"name", "world", "name2", "today"}, "Hello, world! How are you ?"), | ||
Arguments.of("Hello, ${name}! How are you ${name1}?", new String[]{}, "Hello, ! How are you ?"), | ||
Arguments.of("Hello, ${name}! How are you $${name1}?", new String[]{"name", "world", "name1", "today"}, "Hello, world! How are you ${name1}?"), | ||
Arguments.of("Hello, ${name}! How are you $$${name1}?", new String[]{"name", "world", "name1", "today"}, "Hello, world! How are you $today?") | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("evaluate") | ||
void evaluate(String template, String[] values, String expected) throws EvaluationException, ParseException { | ||
var expr = new Expression("STRING_TEMPLATE(template, values)", EXPRESSION_CONFIGURATION); | ||
var result = expr.with("template", template).with("values", values).evaluate().getStringValue(); | ||
assertEquals(expected, result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters