Skip to content

Commit

Permalink
Qute: fix validation of array index-based expressions
Browse files Browse the repository at this point in the history
- such as {myArray[1].name}
  • Loading branch information
mkouba committed Nov 6, 2023
1 parent 19cc4ee commit d6fc605
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,8 @@ private static boolean processArray(Info info, MatchResult match) {
// myArray[0], myArray.1
try {
Integer.parseInt(name);
match.setValues(null, match.type().asArrayType().constituent());
Type constituent = match.type().asArrayType().constituent();
match.setValues(match.assignabilityCheck.computingIndex.getClassByName(constituent.name()), constituent);
return true;
} catch (NumberFormatException e) {
// not an integer index
Expand All @@ -1436,7 +1437,8 @@ private static boolean processArray(Info info, MatchResult match) {
Expression param = params.get(0);
Object literalValue = param.getLiteral();
if (literalValue == null || literalValue instanceof Integer) {
match.setValues(null, match.type().asArrayType().constituent());
Type constituent = match.type().asArrayType().constituent();
match.setValues(match.assignabilityCheck.computingIndex.getClassByName(constituent.name()), constituent);
return true;
}
} else if (name.equals("take") || name.equals("takeLast")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,37 @@ public class CheckedTemplateArrayParamTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Templates.class)
.addClasses(Templates.class, Color.class)
.addAsResource(new StringAsset("Hello {myArray[1]}! ::{myArray.take(1).size}"),
"templates/CheckedTemplateArrayParamTest/arrays.txt"));
"templates/CheckedTemplateArrayParamTest/arrays.txt")
.addAsResource(new StringAsset("{array.0.asStr}"),
"templates/CheckedTemplateArrayParamTest/colors.txt"));

@Test
public void testBasePath() {
assertEquals("Hello 1! ::1",
Templates.arrays(new int[] { 0, 1 }).render());
assertEquals("RED",
Templates.colors(new Color[] { Color.RED, Color.BLUE }).render());
}

@CheckedTemplate
public static class Templates {

static native TemplateInstance arrays(int[] myArray);

static native TemplateInstance colors(Color[] array);

}

public static enum Color {

RED,
BLUE;

public String asStr() {
return toString();
}
}

}

0 comments on commit d6fc605

Please sign in to comment.