Skip to content

Commit

Permalink
Fix apache#3307 - Easier string interpolation
Browse files Browse the repository at this point in the history
As proposed in the issue description, remove the need for initial
quoutes in case of string interpolation
  • Loading branch information
fjtirado committed Dec 4, 2023
1 parent 82f9b74 commit e8e43bf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.javacc.ExpressionParser;
import net.thisptr.jackson.jq.internal.tree.FunctionCall;
import net.thisptr.jackson.jq.internal.tree.StringInterpolation;
import net.thisptr.jackson.jq.internal.tree.binaryop.BinaryOperatorExpression;

public class JqExpression implements Expression {
Expand Down Expand Up @@ -76,13 +77,38 @@ public JqExpression(Supplier<Scope> scope, String expr, Version version) {
this.expr = expr;
this.scope = scope;
try {
this.internalExpr = ExpressionParser.compile(expr, version);
this.internalExpr = compile(version);
checkFunctionCall(internalExpr);
} catch (JsonQueryException ex) {
validationError = ex;
}
}

private net.thisptr.jackson.jq.Expression compile(Version version) throws JsonQueryException {
net.thisptr.jackson.jq.Expression expression;
try {
expression = ExpressionParser.compile(expr, version);
} catch (JsonQueryException ex) {
expression = handleStringInterpolation(version, ex);
}
checkFunctionCall(expression);
return expression;
}

private net.thisptr.jackson.jq.Expression handleStringInterpolation(Version version, JsonQueryException ex) throws JsonQueryException {
if (!expr.startsWith("\"")) {
try {
net.thisptr.jackson.jq.Expression expression = ExpressionParser.compile("\"" + expr + "\"", version);
if (expression instanceof StringInterpolation) {
return expression;
}
} catch (JsonQueryException withQuotes) {
// ignoring it
}
}
throw ex;
}

private interface TypedOutput extends Output {
Object getResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,21 @@ void testJava() {

@Test
void testInterpolation() {
interpolation("\"My name is \\(.name) and my surname is \\(.surname)\"");
}

@Test
void testAbreviatedInterpolation() {
interpolation("My name is \\(.name) and my surname is \\(.surname)");
}

private void interpolation(String text) {
final String INTERPOLATION = "interpolation";
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
Workflow workflow = workflow("PlayingWithExpression").function(expr(INTERPOLATION, "\"My name is \\(.name)\""))
Workflow workflow = workflow("PlayingWithExpression").function(expr(INTERPOLATION, text))
.start(operation().action(call(INTERPOLATION))).end().build();
assertThat(application.execute(workflow, Collections.singletonMap("name", "Javierito")).getWorkflowdata().get("response").asText()).isEqualTo("My name is Javierito");
assertThat(application.execute(workflow, Map.of("name", "Javierito", "surname", "unknown")).getWorkflowdata().get("response").asText())
.isEqualTo("My name is Javierito and my surname is unknown");
}
}

Expand Down

0 comments on commit e8e43bf

Please sign in to comment.