Skip to content

Commit

Permalink
[#60] Fix constant inlining for execl serializer to inline like a lit…
Browse files Browse the repository at this point in the history
…eral
  • Loading branch information
beikov committed Apr 27, 2022
1 parent a737065 commit bfa47c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ private Boolean inlineIfConstant(Expression expression, int startIndex, boolean
try {
Object value = interpreter.evaluateAsModelType(expression, interpreterContextForInlining);
sb.setLength(startIndex);
if (value instanceof CharSequence) {
renderStringLiteral((CharSequence) value);
} else {
sb.append(value);
}
return Boolean.TRUE;
return visitLiteral(value, expression.getType());
} catch (RuntimeException ex) {
throw new IllegalArgumentException("Could not inline expression '" + expression + "'", ex);
}
Expand Down Expand Up @@ -216,15 +211,18 @@ public Boolean visit(FunctionInvocation e) {

@Override
public Boolean visit(Literal e) {
return visitLiteral(e.getValue(), e.getType());
}

private Boolean visitLiteral(Object value, DomainType type) {
// TODO: implement some kind of SPI contract for literal rendering which can be replaced i.e. there should be a default impl that can be replaced
if (e.getType().getKind() == DomainType.DomainTypeKind.COLLECTION) {
if (type.getKind() == DomainType.DomainTypeKind.COLLECTION) {
if (interpreterContextForInlining != null) {
// This could be a problem if the literal was the root expression, but we assume this is not the case
return Boolean.TRUE;
}
throw new UnsupportedOperationException("No support for collections in Excel");
} else {
Object value = e.getValue();
if (value instanceof CharSequence) {
renderStringLiteral((CharSequence) value);
} else if (value instanceof Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private String serialize(String expressionString) {
rootDomainTypes.put("u", domainModel.getType("User"));
rootDomainTypes.put("g", domainModel.getType("GlobalState"));
ExpressionCompiler.Context compilerContext = compiler.createContext(rootDomainTypes);
Expression expression = compiler.createExpression(expressionString, compilerContext);
Expression expression = compiler.createExpressionOrPredicate(expressionString, compilerContext);
ExpressionSerializer<StringBuilder> excelSerializer = expressionService.createSerializer(StringBuilder.class, "excel");
ExpressionInterpreter.Context interpreterContext = ExpressionInterpreterContext.create(expressionService)
.withRoot("g", new GlobalState("The root"));
Expand Down Expand Up @@ -152,4 +152,9 @@ public void testInlineGlobalSimple() {
public void testInlineGlobalExpression() {
Assert.assertEquals("A1 & \"The\"", serialize("u.name + SUBSTRING(g.rootName, 1, 3)"));
}

@Test
public void testBooleanLiteral() {
Assert.assertEquals("E1 = TRUE()", serialize("u.active = true"));
}
}

0 comments on commit bfa47c3

Please sign in to comment.