From 2894c9878dea545876c7b3ec204eb02ec79d9798 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 13 Jun 2023 16:07:20 +0200 Subject: [PATCH] Qute: add SectionResolutionContext.evaluate() --- docs/src/main/asciidoc/qute-reference.adoc | 24 ++++++++++++++--- .../section/CustomSectionFactory.java | 27 ++++++++++++++----- .../CustomSectionHelperFailureTest.java | 5 ++-- .../section/CustomSectionHelperTest.java | 2 +- .../io/quarkus/qute/EvalSectionHelper.java | 4 +-- .../io/quarkus/qute/IncludeSectionHelper.java | 4 +-- .../java/io/quarkus/qute/SectionHelper.java | 20 ++++++++++++++ .../java/io/quarkus/qute/SectionNode.java | 5 ++++ .../io/quarkus/qute/SetSectionHelper.java | 8 +++--- 9 files changed, 75 insertions(+), 24 deletions(-) diff --git a/docs/src/main/asciidoc/qute-reference.adoc b/docs/src/main/asciidoc/qute-reference.adoc index 8d6fe3f3195c4..709a2dcbe102a 100644 --- a/docs/src/main/asciidoc/qute-reference.adoc +++ b/docs/src/main/asciidoc/qute-reference.adoc @@ -1538,20 +1538,36 @@ public class CustomSectionFactory implements SectionHelperFactory getDefaultAliases() { return List.of("custom"); } + + @Override + public ParametersInfo getParameters() { + // Param "foo" is required + return ParametersInfo.builder().addParameter("foo").build(); <3> + } + + @Override + public Scope initializeBlock(Scope outerScope, BlockInfo block) { + block.addExpression("foo", block.getParameter("foo")); + return outerScope; + } + @Override public CustomSectionHelper initialize(SectionInitContext context) { - if (context.getParameter("foo") == null) { - throw new IllegalStateException("Foo param not found"); <3> - } return new CustomSectionHelper(); } class CustomSectionHelper implements SectionHelper { + + private final Expression foo; + + public CustomSectionHelper(Expression foo) { + this.foo = foo; + } @Override public CompletionStage resolve(SectionResolutionContext context) { - return CompletableFuture.completedStage(new SingleResultNode(service.getValue())); <4> + return context.evaluate(foo).thenApply(fooVal -> new SingleResultNode(service.getValueForFoo(fooVal))); <4> } } } diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionFactory.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionFactory.java index 356931ff103a5..8cb63431c0d9a 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionFactory.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionFactory.java @@ -1,13 +1,14 @@ package io.quarkus.qute.deployment.engineconfigurations.section; import java.util.List; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import jakarta.inject.Inject; import io.quarkus.qute.EngineConfiguration; +import io.quarkus.qute.Expression; import io.quarkus.qute.ResultNode; +import io.quarkus.qute.Scope; import io.quarkus.qute.SectionHelper; import io.quarkus.qute.SectionHelperFactory; import io.quarkus.qute.SingleResultNode; @@ -24,19 +25,33 @@ public List getDefaultAliases() { return List.of("custom"); } + @Override + public ParametersInfo getParameters() { + return ParametersInfo.builder().addParameter("foo").build(); + } + + @Override + public Scope initializeBlock(Scope outerScope, BlockInfo block) { + block.addExpression("foo", block.getParameter("foo")); + return outerScope; + } + @Override public CustomSectionHelper initialize(SectionInitContext context) { - if (context.getParameter("foo") == null) { - throw new IllegalStateException("Foo param not found"); - } - return new CustomSectionHelper(); + return new CustomSectionHelper(context.getExpression("foo")); } class CustomSectionHelper implements SectionHelper { + private final Expression foo; + + public CustomSectionHelper(Expression foo) { + this.foo = foo; + } + @Override public CompletionStage resolve(SectionResolutionContext context) { - return CompletableFuture.completedStage(new SingleResultNode(bar)); + return context.evaluate(foo).thenApply(fooVal -> new SingleResultNode(fooVal.toString() + ":" + bar)); } } diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperFailureTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperFailureTest.java index b5eabcace2463..811d7ba0e2589 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperFailureTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperFailureTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.qute.TemplateException; import io.quarkus.runtime.util.ExceptionUtil; import io.quarkus.test.QuarkusUnitTest; @@ -18,9 +19,9 @@ public class CustomSectionHelperFailureTest { .addAsResource(new StringAsset("{#custom bar=1 /}"), "templates/bar.html")) .assertException(t -> { Throwable rootCause = ExceptionUtil.getRootCause(t); - if (rootCause instanceof IllegalStateException) { + if (rootCause instanceof TemplateException) { assertTrue(rootCause.getMessage().contains( - "Foo param not found")); + "mandatory section parameters not declared")); } else { fail(t); } diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperTest.java index 78e048aadbbbe..b0bb0c7e9be24 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/engineconfigurations/section/CustomSectionHelperTest.java @@ -23,7 +23,7 @@ public class CustomSectionHelperTest { @Test public void testSectionHelper() { - assertEquals("BAR!", foo.render()); + assertEquals("1:BAR!", foo.render()); } } diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/EvalSectionHelper.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/EvalSectionHelper.java index 3032ba8b3a3fc..bd76d4401a6f6 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/EvalSectionHelper.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/EvalSectionHelper.java @@ -1,7 +1,5 @@ package io.quarkus.qute; -import static io.quarkus.qute.Futures.evaluateParams; - import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,7 +24,7 @@ public EvalSectionHelper(Map parameters, Engine engine) { @Override public CompletionStage resolve(SectionResolutionContext context) { CompletableFuture result = new CompletableFuture<>(); - evaluateParams(parameters, context.resolutionContext()).whenComplete((evaluatedParams, t1) -> { + context.evaluate(parameters).whenComplete((evaluatedParams, t1) -> { if (t1 != null) { result.completeExceptionally(t1); } else { diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/IncludeSectionHelper.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/IncludeSectionHelper.java index 8edfcf3df81b3..d3e6489f497d5 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/IncludeSectionHelper.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/IncludeSectionHelper.java @@ -1,7 +1,5 @@ package io.quarkus.qute; -import static io.quarkus.qute.Futures.evaluateParams; - import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -51,7 +49,7 @@ public CompletionStage resolve(SectionResolutionContext context) { return root.resolve(resolutionContext, t.isFragment() ? FRAGMENT_PARAMS : null); } else { CompletableFuture result = new CompletableFuture<>(); - evaluateParams(parameters, context.resolutionContext()).whenComplete((evaluatedParams, t1) -> { + context.evaluate(parameters).whenComplete((evaluatedParams, t1) -> { if (t1 != null) { result.completeExceptionally(t1); } else { diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionHelper.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionHelper.java index ae1ae7528a2e8..ba2d54d4debdc 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionHelper.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionHelper.java @@ -23,6 +23,26 @@ public interface SectionHelper { */ public interface SectionResolutionContext { + /** + * Evaluates the given expressions and returns the map of expression keys to evaluated values. + * + * @param expressions + * @return the map of expression keys to evaluated values + * @see #evaluate(Expression) + */ + CompletionStage> evaluate(Map expressions); + + /** + * Evaluates a single expression. + * + * @param expression + * @return the evaluated value + * @see #evaluate(Map) + */ + default CompletionStage evaluate(Expression expression) { + return resolutionContext().evaluate(expression); + } + /** * * @return the current resolution context diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionNode.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionNode.java index c0228ba100d0c..e90f6cc4d3d5f 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionNode.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SectionNode.java @@ -207,6 +207,11 @@ public SectionResolutionContextImpl(ResolutionContext resolutionContext, Map> evaluate(Map parameters) { + return Futures.evaluateParams(parameters, resolutionContext); + } + @Override public CompletionStage execute(SectionBlock block, ResolutionContext context) { if (block == null) { diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SetSectionHelper.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SetSectionHelper.java index 7cd5901b980e1..d20a8a2977806 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/SetSectionHelper.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/SetSectionHelper.java @@ -1,7 +1,5 @@ package io.quarkus.qute; -import static io.quarkus.qute.Futures.evaluateParams; - import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -41,7 +39,7 @@ public class SetSectionHelper implements SectionHelper { public CompletionStage resolve(SectionResolutionContext context) { CompletableFuture result = new CompletableFuture<>(); if (defaultKeys.isEmpty()) { - evaluateParams(parameters, context.resolutionContext()).whenComplete((r, t) -> { + context.evaluate(parameters).whenComplete((r, t) -> { if (t != null) { result.completeExceptionally(t); } else { @@ -57,7 +55,7 @@ public CompletionStage resolve(SectionResolutionContext context) { }); } else { // First evaluate the keys - evaluateParams(defaultKeys, context.resolutionContext()).whenComplete((r, t) -> { + context.evaluate(defaultKeys).whenComplete((r, t) -> { if (t != null) { result.completeExceptionally(t); } else { @@ -80,7 +78,7 @@ public CompletionStage resolve(SectionResolutionContext context) { }); } else { // Evaluate the default values - evaluateParams(toEval, context.resolutionContext()).whenComplete((r2, t2) -> { + context.evaluate(toEval).whenComplete((r2, t2) -> { if (t2 != null) { result.completeExceptionally(t2); } else {