From e2e0cd0eb3e8e750b9ba79fd7904dc70f15b12b8 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 21 Jun 2022 11:37:27 +0200 Subject: [PATCH] Qute - fix Template#getParameterDeclarations() - the param declaration node may be removed as a part of a standalone line --- ...tionDefaultValueValidationFailureTest.java | 2 +- .../io/quarkus/qute/ParameterDeclaration.java | 24 +++++++++++++++++++ .../java/io/quarkus/qute/ParserHelper.java | 3 ++- .../main/java/io/quarkus/qute/Template.java | 2 +- .../java/io/quarkus/qute/TemplateImpl.java | 5 +++- .../java/io/quarkus/qute/TemplateNode.java | 1 + .../ParamDeclarationDefaultValueTest.java | 14 +++++++++++ 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/ParamDeclarationDefaultValueValidationFailureTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/ParamDeclarationDefaultValueValidationFailureTest.java index 01e276d389ce0..4487548a95a91 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/ParamDeclarationDefaultValueValidationFailureTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/ParamDeclarationDefaultValueValidationFailureTest.java @@ -17,7 +17,7 @@ public class ParamDeclarationDefaultValueValidationFailureTest { static final QuarkusUnitTest config = new QuarkusUnitTest() .withApplicationRoot(root -> root .addAsResource(new StringAsset( - "{@java.lang.String myName=1}{myName}"), + "{@java.lang.String myName=1}\n{myName}"), "templates/myName.html")) .assertException(t -> { Throwable e = t; diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParameterDeclaration.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParameterDeclaration.java index 72b17671ba0e7..6a6da4bce112b 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParameterDeclaration.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParameterDeclaration.java @@ -1,14 +1,38 @@ package io.quarkus.qute; +import io.quarkus.qute.Expression.Part; +import io.quarkus.qute.TemplateNode.Origin; + /** * Represents a parameter declaration, i.e. {@org.acme.Foo foo}. */ public interface ParameterDeclaration { + /** + * The type info for {@org.acme.Foo foo} is {@code |org.acme.Foo|}. + * + * @return the unparsed type info + * @see Part#getTypeInfo() + */ String getTypeInfo(); + /** + * The key for {@org.acme.Foo foo} is {@code foo}. + * + * @return the key + */ String getKey(); + /** + * + * @return the default value or {@code null} if no default value is set + */ Expression getDefaultValue(); + /** + * + * @return the origin of the template node + */ + Origin getOrigin(); + } diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParserHelper.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParserHelper.java index 2b22ccbb5d090..9d25886f270b2 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParserHelper.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/ParserHelper.java @@ -15,7 +15,8 @@ public interface ParserHelper { String getTemplateId(); /** - * Adds an implicit parameter declaration. This an alternative approach to explicit parameter declarations + * Adds an implicit parameter declaration. This is an alternative approach to explicit parameter + * declarations * used directly in the templates, e.g. {@org.acme.Foo foo}. * * @param name diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java index 3a2e315e53877..6b66071cccdbe 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java @@ -152,7 +152,7 @@ default String render() { /** * - * @return an immutable list of parameter declarations + * @return an immutable list of all parameter declarations defined in the template */ List getParameterDeclarations(); diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateImpl.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateImpl.java index 9cd74648b889e..737f9283e7af9 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateImpl.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateImpl.java @@ -23,6 +23,7 @@ class TemplateImpl implements Template { private final EngineImpl engine; private final Optional variant; final SectionNode root; + private final List parameterDeclarations; TemplateImpl(EngineImpl engine, SectionNode root, String templateId, String generatedId, Optional variant) { this.engine = engine; @@ -30,6 +31,8 @@ class TemplateImpl implements Template { this.templateId = templateId; this.generatedId = generatedId; this.variant = variant; + // Note that param declarations can be removed if placed on a standalone line + this.parameterDeclarations = ImmutableList.copyOf(root.getParameterDeclarations()); } @Override @@ -55,7 +58,7 @@ public Expression findExpression(Predicate predicate) { @Override public List getParameterDeclarations() { - return root.getParameterDeclarations(); + return parameterDeclarations; } @Override diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateNode.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateNode.java index a28e768609ebc..6b82450d057bb 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateNode.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/TemplateNode.java @@ -26,6 +26,7 @@ default List getExpressions() { } /** + * Returns the parameter declarations defined in this template node. * * @return a list of param declarations */ diff --git a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ParamDeclarationDefaultValueTest.java b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ParamDeclarationDefaultValueTest.java index 8720ad3dbb58c..ed5872e058209 100644 --- a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ParamDeclarationDefaultValueTest.java +++ b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ParamDeclarationDefaultValueTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.List; import org.junit.jupiter.api.Test; public class ParamDeclarationDefaultValueTest { @@ -24,6 +25,19 @@ public void testMultipleDefaultValues() { Template template = engine .parse("{@java.lang.String val='foo'}\n{@int anotherVal=1}\n{val}::{anotherVal}"); assertEquals("foo::2", template.data("anotherVal", 2).render()); + List parameterDeclarations = template.getParameterDeclarations(); + assertEquals(2, parameterDeclarations.size()); + for (ParameterDeclaration pd : parameterDeclarations) { + if (pd.getKey().equals("val")) { + assertEquals("'foo'", pd.getDefaultValue().toOriginalString()); + assertEquals("|java.lang.String|", pd.getTypeInfo()); + assertEquals(1, pd.getOrigin().getLine()); + } else { + assertEquals("anotherVal", pd.getKey()); + assertEquals("1", pd.getDefaultValue().toOriginalString()); + assertEquals("|int|", pd.getTypeInfo()); + } + } } private void assertDefaultValue(Template template, String expectedOutput) {