forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#41690 from mkouba/qute-parser-hook-engin…
…e-config Qute: extend the @EngineConfiguration support to ParserHook
- Loading branch information
Showing
16 changed files
with
301 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...te/deployment/src/main/java/io/quarkus/qute/deployment/ValidationParserHookBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.qute.deployment; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.qute.ParserHelper; | ||
|
||
/** | ||
* This build item can be used to hook into the parser logic during validation at build time. | ||
* <p> | ||
* Validation parser hooks are never used at runtime. | ||
*/ | ||
public final class ValidationParserHookBuildItem extends MultiBuildItem { | ||
|
||
private final Consumer<ParserHelper> hook; | ||
|
||
public ValidationParserHookBuildItem(Consumer<ParserHelper> hook) { | ||
this.hook = Objects.requireNonNull(hook); | ||
} | ||
|
||
public Consumer<ParserHelper> getHook() { | ||
return hook; | ||
} | ||
|
||
public void accept(ParserHelper helper) { | ||
hook.accept(helper); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...io/quarkus/qute/deployment/engineconfigurations/parserhook/CustomParserHookBuildTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.quarkus.qute.deployment.engineconfigurations.parserhook; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.EngineConfiguration; | ||
import io.quarkus.qute.ParserHelper; | ||
import io.quarkus.qute.ParserHook; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class CustomParserHookBuildTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
root -> root.addClasses(CustomParserHook.class, Foo.class) | ||
.addAsResource(new StringAsset("{foo.bar}"), "templates/foo.html")) | ||
.assertException(t -> { | ||
Throwable e = t; | ||
TemplateException te = null; | ||
while (e != null) { | ||
if (e instanceof TemplateException) { | ||
te = (TemplateException) e; | ||
break; | ||
} | ||
e = e.getCause(); | ||
} | ||
assertNotNull(te); | ||
assertTrue(te.getMessage().contains("Found incorrect expressions (1)"), te.getMessage()); | ||
assertTrue(te.getMessage().contains("{foo.bar}"), te.getMessage()); | ||
});; | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
@EngineConfiguration | ||
public static class CustomParserHook implements ParserHook { | ||
|
||
@Override | ||
public void beforeParsing(ParserHelper helper) { | ||
if (helper.getTemplateId().contains("foo")) { | ||
helper.addParameter("foo", Foo.class.getName()); | ||
} | ||
} | ||
|
||
} | ||
|
||
public static class Foo { | ||
|
||
// package-private method is ignored | ||
String bar() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
.../quarkus/qute/deployment/engineconfigurations/parserhook/CustomParserHookRuntimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.quarkus.qute.deployment.engineconfigurations.parserhook; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Engine; | ||
import io.quarkus.qute.EngineConfiguration; | ||
import io.quarkus.qute.ParserHelper; | ||
import io.quarkus.qute.ParserHook; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class CustomParserHookRuntimeTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
root -> root.addClasses(CustomParserHook.class) | ||
.addAsResource(new StringAsset("{foo}"), "templates/foo.html")); | ||
|
||
@Inject | ||
Engine engine; | ||
|
||
@Test | ||
public void testParserHook() { | ||
assertEquals("42", engine.getTemplate("foo").data("bar", 42).render()); | ||
} | ||
|
||
@EngineConfiguration | ||
public static class CustomParserHook implements ParserHook { | ||
|
||
@Inject | ||
Engine engine; | ||
|
||
@Override | ||
public void beforeParsing(ParserHelper helper) { | ||
if (helper.getTemplateId().contains("foo") && engine != null) { | ||
helper.addContentFilter(c -> "{bar}"); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...t/java/io/quarkus/qute/deployment/engineconfigurations/parserhook/ValidationHookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.quarkus.qute.deployment.engineconfigurations.parserhook; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.builder.BuildChainBuilder; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.qute.deployment.ValidationParserHookBuildItem; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ValidationHookTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
root -> root.addClasses(Foo.class) | ||
.addAsResource(new StringAsset("{foo.bar}"), "templates/foo.html")) | ||
.assertException(t -> { | ||
Throwable e = t; | ||
TemplateException te = null; | ||
while (e != null) { | ||
if (e instanceof TemplateException) { | ||
te = (TemplateException) e; | ||
break; | ||
} | ||
e = e.getCause(); | ||
} | ||
assertNotNull(te); | ||
assertTrue(te.getMessage().contains("Found incorrect expressions (1)"), te.getMessage()); | ||
assertTrue(te.getMessage().contains("{foo.bar}"), te.getMessage()); | ||
}).addBuildChainCustomizer(buildCustomizer()); | ||
|
||
static Consumer<BuildChainBuilder> buildCustomizer() { | ||
return new Consumer<BuildChainBuilder>() { | ||
@Override | ||
public void accept(BuildChainBuilder builder) { | ||
builder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new ValidationParserHookBuildItem(helper -> { | ||
if (helper.getTemplateId().contains("foo")) { | ||
helper.addParameter("foo", Foo.class.getName()); | ||
} | ||
})); | ||
} | ||
}).produces(ValidationParserHookBuildItem.class) | ||
.build(); | ||
|
||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
public static class Foo { | ||
|
||
// package-private method is ignored | ||
String bar() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.