-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: aserkes <[email protected]>
- Loading branch information
Showing
4 changed files
with
343 additions
and
5 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
139 changes: 139 additions & 0 deletions
139
...etype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/ValueHandler.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,139 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.archetype.engine.v2.util; | ||
|
||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import io.helidon.build.archetype.engine.v2.ast.Expression; | ||
import io.helidon.build.archetype.engine.v2.ast.Value; | ||
|
||
/** | ||
* Process string and if it represents an expression evaluate it and return {@link Value}. | ||
* | ||
* @see Expression | ||
*/ | ||
public class ValueHandler { | ||
|
||
private static final Pattern VAR_NO_BRACE = Pattern.compile("^\\w+"); | ||
|
||
/** | ||
* Process expression. | ||
* | ||
* @param expression expression | ||
* @param resolver variable resolver | ||
* @return {@link Value} object | ||
*/ | ||
public static Value process(String expression, Function<String, Value> resolver) { | ||
if (expression == null) { | ||
return Value.NULL; | ||
} | ||
for (ExpressionType type : ExpressionType.values()) { | ||
Matcher matcher = type.pattern.matcher(expression); | ||
if (matcher.find()) { | ||
String value = matcher.group("expression"); | ||
if (type == ExpressionType.NO_BRACE_VARS) { | ||
value = preprocessExpression(value); | ||
} | ||
return Expression.parse(value).eval(resolver); | ||
} | ||
|
||
} | ||
return Value.create(expression); | ||
} | ||
|
||
/** | ||
* Process expression. | ||
* | ||
* @param expression expression | ||
* @return {@link Value} object | ||
*/ | ||
public static Value process(String expression) { | ||
return process(expression, s -> null); | ||
} | ||
|
||
private static String preprocessExpression(String expression) { | ||
StringBuilder output = new StringBuilder(); | ||
int cursor = 0; | ||
String current = expression.trim(); | ||
while (current.length() > 0) { | ||
current = current.substring(cursor); | ||
cursor = 0; | ||
boolean tokenFound = false; | ||
for (Token token : Token.values()) { | ||
Matcher matcher = token.pattern.matcher(current); | ||
if (matcher.find()) { | ||
String value = matcher.group(); | ||
cursor += value.length(); | ||
output.append(value); | ||
tokenFound = true; | ||
break; | ||
} | ||
} | ||
if (tokenFound) { | ||
continue; | ||
} | ||
Matcher matcherVar = VAR_NO_BRACE.matcher(current); | ||
while (matcherVar.find()) { | ||
var value = matcherVar.group(); | ||
cursor += value.length(); | ||
output.append("${").append(value).append("}"); | ||
tokenFound = true; | ||
} | ||
if (!tokenFound && current.trim().length() > 0) { | ||
throw new Expression.FormatException("Unexpected token - " + current); | ||
} | ||
} | ||
return output.toString(); | ||
} | ||
|
||
private enum ExpressionType { | ||
|
||
BACKTICK("^`(?<expression>.*)`$"), | ||
BRACE_VARS("^#\\{(?<expression>.*(\\$\\{)+.*}+.*)}$"), | ||
NO_BRACE_VARS("^#\\{(?<expression>[^\\$\\{}]*)}$"); | ||
|
||
private final Pattern pattern; | ||
|
||
ExpressionType(String regex) { | ||
this.pattern = Pattern.compile(regex); | ||
} | ||
} | ||
|
||
private enum Token { | ||
SKIP("^\\s+"), | ||
ARRAY("^\\[[^]\\[]*]"), | ||
BOOLEAN("^(true|false)\\b"), | ||
STRING("^['\"][^'\"]*['\"]"), | ||
VARIABLE("^\\$\\{(?<varName>~?[\\w.-]+)}"), | ||
EQUALITY_OPERATOR("^(!=|==)"), | ||
BINARY_LOGICAL_OPERATOR("^(\\|\\||&&)"), | ||
UNARY_LOGICAL_OPERATOR("^[!]"), | ||
CONTAINS_OPERATOR("^contains\\b"), | ||
PARENTHESIS("^[()]"), | ||
COMMENT("#.*\\R"), | ||
TERNARY_IF_OPERATOR("^\\?"), | ||
TERNARY_ELSE_OPERATOR("^:"), | ||
INTEGER("^[0-9]+"); | ||
|
||
private final Pattern pattern; | ||
|
||
Token(String regex) { | ||
this.pattern = Pattern.compile(regex); | ||
} | ||
} | ||
} |
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
175 changes: 175 additions & 0 deletions
175
...e/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/util/ValueHandlerTest.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,175 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.archetype.engine.v2.util; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.helidon.build.archetype.engine.v2.ast.Expression; | ||
import io.helidon.build.archetype.engine.v2.ast.Value; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests for {@link ValueHandler} | ||
*/ | ||
public class ValueHandlerTest { | ||
|
||
@Test | ||
public void testBacktickExpression() { | ||
Map<String, Value> variables; | ||
Value result; | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b"), | ||
"var3", Value.create("c")); | ||
result = ValueHandler.process("`${shape} == 'circle' ? ${var1} contains ${var2} ? 'circle_b' : 'not_circle_b'" | ||
+ " : ${var1} contains ${var3} ? 'circle_c' : 'not_circle_c'`", variables::get); | ||
assertThat(result.asText(), is("circle_b")); | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b")); | ||
result = ValueHandler.process("`${shape} == 'circle' ? ${var1} contains ${var2} : false`", variables::get); | ||
assertThat(result.asBoolean(), is(true)); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("`${shape} == 'circle' ? 'red' : 'blue'`", variables::get); | ||
assertThat(result.asString(), is("red")); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("`${shape}`", variables::get); | ||
assertThat(result.asString(), is("circle")); | ||
|
||
Map<String, Value> varMap = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create("circle")); | ||
Expression.FormatException e = assertThrows(Expression.FormatException.class, | ||
() -> ValueHandler.process("`${shape} == var1`", varMap::get)); | ||
assertThat(e.getMessage(), containsString("Unexpected token - var1")); | ||
|
||
e = assertThrows(Expression.FormatException.class, | ||
() -> ValueHandler.process("`shape`")); | ||
assertThat(e.getMessage(), containsString("Unexpected token - shape")); | ||
} | ||
|
||
@Test | ||
public void testBraceVarExpression() { | ||
Map<String, Value> variables; | ||
Value result; | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b"), | ||
"var3", Value.create("c")); | ||
result = ValueHandler.process("#{${shape} == 'circle' ? ${var1} contains ${var2} ? 'circle_b' : 'not_circle_b'" | ||
+ " : ${var1} contains ${var3} ? 'circle_c' : 'not_circle_c'}", variables::get); | ||
assertThat(result.asText(), is("circle_b")); | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b")); | ||
result = ValueHandler.process("#{${shape} == 'circle' ? ${var1} contains ${var2} : false}", variables::get); | ||
assertThat(result.asBoolean(), is(true)); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("#{${shape} == 'circle' ? 'red' : 'blue'}", variables::get); | ||
assertThat(result.asString(), is("red")); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("#{${shape}}", variables::get); | ||
assertThat(result.asString(), is("circle")); | ||
|
||
Map<String, Value> varMap = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create("circle")); | ||
Expression.FormatException e = assertThrows(Expression.FormatException.class, | ||
() -> ValueHandler.process("#{${shape} == var1}", varMap::get)); | ||
assertThat(e.getMessage(), containsString("Unexpected token - var1")); | ||
} | ||
|
||
@Test | ||
public void testNoBraceVarExpression() { | ||
Map<String, Value> variables; | ||
Value result; | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b"), | ||
"var3", Value.create("c")); | ||
result = ValueHandler.process("#{shape == 'circle' ? var1 contains var2 ? 'circle_b' : 'not_circle_b' : " | ||
+ "var1 contains var3 ? 'circle_c' : 'not_circle_c'}", variables::get); | ||
assertThat(result.asText(), is("circle_b")); | ||
|
||
variables = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create(List.of("a", "b", "c")), | ||
"var2", Value.create("b")); | ||
result = ValueHandler.process("#{shape == 'circle' ? var1 contains var2 : false}", variables::get); | ||
assertThat(result.asBoolean(), is(true)); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("#{shape == 'circle' ? 'red' : 'blue'}", variables::get); | ||
assertThat(result.asString(), is("red")); | ||
|
||
variables = Map.of("shape", Value.create("circle")); | ||
result = ValueHandler.process("#{shape}", variables::get); | ||
assertThat(result.asString(), is("circle")); | ||
|
||
result = ValueHandler.process("`true`"); | ||
assertThat(result.asBoolean(), is(true)); | ||
|
||
result = ValueHandler.process("#{1}"); | ||
assertThat(result.asInt(), is(1)); | ||
|
||
result = ValueHandler.process("`['', 'adc', 'def']`"); | ||
assertThat(result.asList(), containsInAnyOrder("", "adc", "def")); | ||
|
||
Map<String, Value> varMap = Map.of( | ||
"shape", Value.create("circle"), | ||
"var1", Value.create("circle")); | ||
Expression.FormatException e = assertThrows(Expression.FormatException.class, | ||
() -> ValueHandler.process("#{${shape} ^ var1}", varMap::get)); | ||
assertThat(e.getMessage(), containsString("Unexpected token - ^")); | ||
} | ||
|
||
@Test | ||
public void testNoExpression() { | ||
var value = ValueHandler.process("circle"); | ||
assertThat(value.asString(), is("circle")); | ||
|
||
value = ValueHandler.process("true"); | ||
assertThat(value.asString(), is("true")); | ||
|
||
value = ValueHandler.process("1"); | ||
assertThat(value.asString(), is("1")); | ||
|
||
value = ValueHandler.process("['', 'adc', 'def']"); | ||
assertThat(value.asString(), is("['', 'adc', 'def']")); | ||
} | ||
} |