Skip to content

Commit

Permalink
Fix escaping error
Browse files Browse the repository at this point in the history
  • Loading branch information
ogesaku committed Dec 31, 2023
1 parent 696bda4 commit 06b9acd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/coditory/quark/config/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public ConfigBuilder resolveExpressions(@NotNull Config config) {
return resolveExpressions(config, Expression::failOnUnresolved);
}

@NotNull
public ConfigBuilder resolveExpressionsWithEscaped(@NotNull Config variables) {
expectNonNull(variables, "variables");
MapConfigNode rootWithExpressions = root.mapLeaves(ExpressionParser::parse);
MapConfigNode rootWithExpressionsAndVariables = variables.getRootNode()
.withDefaults(rootWithExpressions)
.mapLeaves(Path.root(), (path, value) -> ExpressionNode.staticNode(value));
Config resolutionConfig = new ResolvableConfig(rootWithExpressionsAndVariables, valueParser, secretHidingValueMapper);
ExpressionResolver resolver = new ExpressionResolver(resolutionConfig);
root = root.mapLeaves(ExpressionParser::parse)
.mapLeaves(resolver::resolve)
.mapLeaves(Expression::failOnUnresolved);
return this;
}

@NotNull
public ConfigBuilder resolveExpressionsOrSkip() {
return resolveExpressionsOrSkip(Config.empty());
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/coditory/quark/config/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@ static Object parse(Object template) {
private static Expression parse(String template) {
List<ExpressionNode> nodes = new ArrayList<>();
boolean expression = false;
Set<Character> escapable = Set.of('{', '}');
boolean escaped = false;
StringBuilder chunk = new StringBuilder();
char[] chars = template.toCharArray();
for (int i = 0; i < chars.length; ++i) {
char c = chars[i];
if (escaped) {
if (!escapable.contains(c)) {
throw new RuntimeException("Could not escape: \"" + c + "\" in template: \"" + template + "\"");
}
escaped = false;
chunk.append(c);
} else if ('\\' == c) {
escaped = true;
} else if (startsWith(chars, i, "${")) {
if (chunk.length() > 0) {
if (!chunk.isEmpty()) {
nodes.add(staticNode(chunk.toString()));
chunk = new StringBuilder();
}
expression = true;
i += 1;
} else if ('}' == c && expression) {
if (chunk.length() > 0) {
if (!chunk.isEmpty()) {
nodes.add(parseExpression(chunk.toString().trim()));
chunk = new StringBuilder();
}
Expand All @@ -52,7 +48,7 @@ private static Expression parse(String template) {
chunk.append(c);
}
}
if (chunk.length() > 0) {
if (!chunk.isEmpty()) {
nodes.add(staticNode(chunk.toString()));
}
return new Expression(template, nodes);
Expand Down

0 comments on commit 06b9acd

Please sign in to comment.