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.
Qute: introduce ValidationParserHookBuildItem
- that can be used to hook into the parser logic during validation at build time
- Loading branch information
Showing
4 changed files
with
115 additions
and
8 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
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); | ||
} | ||
|
||
} |
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
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; | ||
} | ||
|
||
} | ||
|
||
} |