Skip to content

Commit

Permalink
Fix #3307 (#3309)
Browse files Browse the repository at this point in the history
* Fix #3307 - Easier string interpolation

As proposed in the issue description, remove the need for initial
quoutes in case of string interpolation

* Alternative implementation using optional
  • Loading branch information
fjtirado authored Dec 5, 2023
1 parent 0b42d02 commit a5c4af1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

Expand All @@ -46,6 +47,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 +78,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).orElseThrow(() -> ex);
}
checkFunctionCall(expression);
return expression;
}

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

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 a5c4af1

Please sign in to comment.