Skip to content

Commit

Permalink
Qute: add SectionResolutionContext.evaluate()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Jun 13, 2023
1 parent 197c921 commit 2894c98
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 24 deletions.
24 changes: 20 additions & 4 deletions docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1538,20 +1538,36 @@ public class CustomSectionFactory implements SectionHelperFactory<CustomSectionF
public List<String> 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<ResultNode> resolve(SectionResolutionContext context) {
return CompletableFuture.completedStage(new SingleResultNode(service.getValue())); <4>
return context.evaluate(foo).thenApply(fooVal -> new SingleResultNode(service.getValueForFoo(fooVal))); <4>
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,19 +25,33 @@ public List<String> 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<ResultNode> resolve(SectionResolutionContext context) {
return CompletableFuture.completedStage(new SingleResultNode(bar));
return context.evaluate(foo).thenApply(fooVal -> new SingleResultNode(fooVal.toString() + ":" + bar));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CustomSectionHelperTest {

@Test
public void testSectionHelper() {
assertEquals("BAR!", foo.render());
assertEquals("1:BAR!", foo.render());
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +24,7 @@ public EvalSectionHelper(Map<String, Expression> parameters, Engine engine) {
@Override
public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
CompletableFuture<ResultNode> result = new CompletableFuture<>();
evaluateParams(parameters, context.resolutionContext()).whenComplete((evaluatedParams, t1) -> {
context.evaluate(parameters).whenComplete((evaluatedParams, t1) -> {
if (t1 != null) {
result.completeExceptionally(t1);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,7 +49,7 @@ public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
return root.resolve(resolutionContext, t.isFragment() ? FRAGMENT_PARAMS : null);
} else {
CompletableFuture<ResultNode> result = new CompletableFuture<>();
evaluateParams(parameters, context.resolutionContext()).whenComplete((evaluatedParams, t1) -> {
context.evaluate(parameters).whenComplete((evaluatedParams, t1) -> {
if (t1 != null) {
result.completeExceptionally(t1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, Object>> evaluate(Map<String, Expression> expressions);

/**
* Evaluates a single expression.
*
* @param expression
* @return the evaluated value
* @see #evaluate(Map)
*/
default CompletionStage<Object> evaluate(Expression expression) {
return resolutionContext().evaluate(expression);
}

/**
*
* @return the current resolution context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public SectionResolutionContextImpl(ResolutionContext resolutionContext, Map<Str
this.params = params;
}

@Override
public CompletionStage<Map<String, Object>> evaluate(Map<String, Expression> parameters) {
return Futures.evaluateParams(parameters, resolutionContext);
}

@Override
public CompletionStage<ResultNode> execute(SectionBlock block, ResolutionContext context) {
if (block == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,7 +39,7 @@ public class SetSectionHelper implements SectionHelper {
public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
CompletableFuture<ResultNode> 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 {
Expand All @@ -57,7 +55,7 @@ public CompletionStage<ResultNode> 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 {
Expand All @@ -80,7 +78,7 @@ public CompletionStage<ResultNode> 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 {
Expand Down

0 comments on commit 2894c98

Please sign in to comment.