-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #13471 from mkouba/issue-12933
Qute: implement strategies if a property is not found in an expression
- Loading branch information
Showing
11 changed files
with
255 additions
and
21 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
32 changes: 32 additions & 0 deletions
32
...t/src/test/java/io/quarkus/qute/deployment/propertynotfound/PropertyNotFoundNoopTest.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,32 @@ | ||
package io.quarkus.qute.deployment.propertynotfound; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class PropertyNotFoundNoopTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset("foos:{foos}"), "templates/test.html") | ||
.addAsResource(new StringAsset("quarkus.qute.property-not-found-strategy=noop"), "application.properties")); | ||
|
||
@Inject | ||
Template test; | ||
|
||
@Test | ||
public void testNoop() { | ||
assertEquals("foos:", test.render()); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
.../java/io/quarkus/qute/deployment/propertynotfound/PropertyNotFoundOutputOriginalTest.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,33 @@ | ||
package io.quarkus.qute.deployment.propertynotfound; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class PropertyNotFoundOutputOriginalTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset("foos:{foos}"), "templates/test.html") | ||
.addAsResource(new StringAsset("quarkus.qute.property-not-found-strategy=output-original"), | ||
"application.properties")); | ||
|
||
@Inject | ||
Template test; | ||
|
||
@Test | ||
public void testOriginal() { | ||
assertEquals("foos:{foos}", test.render()); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
.../java/io/quarkus/qute/deployment/propertynotfound/PropertyNotFoundThrowExceptionTest.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,44 @@ | ||
package io.quarkus.qute.deployment.propertynotfound; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.runtime.util.ExceptionUtil; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class PropertyNotFoundThrowExceptionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset("foos:{foos}"), "templates/test.html") | ||
.addAsResource(new StringAsset("quarkus.qute.property-not-found-strategy=throw-exception"), | ||
"application.properties")); | ||
|
||
@Inject | ||
Template test; | ||
|
||
@Test | ||
public void testException() { | ||
try { | ||
test.render(); | ||
fail(); | ||
} catch (Exception expected) { | ||
Throwable rootCause = ExceptionUtil.getRootCause(expected); | ||
assertEquals(TemplateException.class, rootCause.getClass()); | ||
assertTrue(rootCause.getMessage().contains("{foos}")); | ||
} | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/PropertyNotFoundNoop.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,25 @@ | ||
package io.quarkus.qute.runtime; | ||
|
||
import io.quarkus.qute.Expression; | ||
import io.quarkus.qute.ResultMapper; | ||
import io.quarkus.qute.Results.Result; | ||
import io.quarkus.qute.TemplateNode.Origin; | ||
|
||
class PropertyNotFoundNoop implements ResultMapper { | ||
|
||
@Override | ||
public int getPriority() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public boolean appliesTo(Origin origin, Object result) { | ||
return result.equals(Result.NOT_FOUND); | ||
} | ||
|
||
@Override | ||
public String map(Object result, Expression expression) { | ||
return ""; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
.../qute/runtime/src/main/java/io/quarkus/qute/runtime/PropertyNotFoundOutputExpression.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,25 @@ | ||
package io.quarkus.qute.runtime; | ||
|
||
import io.quarkus.qute.Expression; | ||
import io.quarkus.qute.ResultMapper; | ||
import io.quarkus.qute.Results.Result; | ||
import io.quarkus.qute.TemplateNode.Origin; | ||
|
||
class PropertyNotFoundOutputOriginal implements ResultMapper { | ||
|
||
@Override | ||
public int getPriority() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public boolean appliesTo(Origin origin, Object result) { | ||
return result.equals(Result.NOT_FOUND); | ||
} | ||
|
||
@Override | ||
public String map(Object result, Expression expression) { | ||
return "{" + expression.toOriginalString() + "}"; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...ns/qute/runtime/src/main/java/io/quarkus/qute/runtime/PropertyNotFoundThrowException.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,28 @@ | ||
package io.quarkus.qute.runtime; | ||
|
||
import io.quarkus.qute.Expression; | ||
import io.quarkus.qute.ResultMapper; | ||
import io.quarkus.qute.Results.Result; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.qute.TemplateNode.Origin; | ||
|
||
class PropertyNotFoundThrowException implements ResultMapper { | ||
|
||
@Override | ||
public int getPriority() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public boolean appliesTo(Origin origin, Object result) { | ||
return result.equals(Result.NOT_FOUND); | ||
} | ||
|
||
@Override | ||
public String map(Object result, Expression expression) { | ||
throw new TemplateException(expression.getOrigin(), | ||
String.format("Property not found in expression {%s} in template %s on line %s", expression.toOriginalString(), | ||
expression.getOrigin().getTemplateId(), expression.getOrigin().getLine())); | ||
} | ||
|
||
} |
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
45 changes: 45 additions & 0 deletions
45
extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/QuteRuntimeConfig.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,45 @@ | ||
package io.quarkus.qute.runtime; | ||
|
||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(name = "qute", phase = ConfigPhase.RUN_TIME) | ||
public class QuteRuntimeConfig { | ||
|
||
/** | ||
* The strategy used if a property is not found when evaluating a standalone expression at runtime. | ||
* <p> | ||
* This strategy is not used when evaluating an expression that is used in a section parameter, e.g. | ||
* <code>{#if foo.name}</code>. In such case, it's the responsibility of the section to handle this situation appropriately. | ||
*/ | ||
@ConfigItem(defaultValue = "default") | ||
public PropertyNotFoundStrategy propertyNotFoundStrategy; | ||
/** | ||
* Specify whether the parser should remove standalone lines from the output. A standalone line is a line that contains at | ||
* least one section tag, parameter declaration, or comment but no expression and no non-whitespace character. | ||
*/ | ||
@ConfigItem(defaultValue = "true") | ||
public boolean removeStandaloneLines; | ||
|
||
public enum PropertyNotFoundStrategy { | ||
/** | ||
* Output the {@code NOT_FOUND} constant. | ||
*/ | ||
DEFAULT, | ||
/** | ||
* No operation - no output. | ||
*/ | ||
NOOP, | ||
/** | ||
* Throw a {@link TemplateException}. | ||
*/ | ||
THROW_EXCEPTION, | ||
/** | ||
* Output the original expression string, e.g. <code>{foo.name}</code>. | ||
*/ | ||
OUTPUT_ORIGINAL | ||
} | ||
|
||
} |