forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qute - support multiple NamespaceResolvers for the same namespace
- resolves quarkusio#16759 - also add built-in StringTemplateExtensions
- Loading branch information
Showing
14 changed files
with
362 additions
and
87 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
49 changes: 49 additions & 0 deletions
49
...ent/src/test/java/io/quarkus/qute/deployment/extensions/StringTemplateExtensionsTest.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,49 @@ | ||
package io.quarkus.qute.deployment.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Locale; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.qute.Engine; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StringTemplateExtensionsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); | ||
|
||
@Inject | ||
Engine engine; | ||
|
||
@Test | ||
public void testTemplateExtensions() { | ||
assertEquals("hello:1", | ||
engine.parse("{str:format('%s:%s',greeting, 1)}").data("greeting", "hello").render()); | ||
assertEquals("1", | ||
engine.parse("{str:fmt('%s',1)}").render()); | ||
assertEquals(" d c b a", | ||
engine.parse("{myStr.fmt('a','b','c','d')}").data("myStr", "%4$2s %3$2s %2$2s %1$2s").render()); | ||
assertEquals("%s", | ||
engine.parse("{myStr.fmt(myStr)}").data("myStr", "%s").render()); | ||
assertEquals("Hello Dorka!", | ||
engine.parse("{myStr.format(name)}").data("myStr", "Hello %s!", "name", "Dorka").render()); | ||
assertEquals("Dienstag", | ||
engine.parse("{myStr.fmt(locale,now)}") | ||
.data("myStr", "%tA", "now", LocalDateTime.of(2016, 7, 26, 12, 0), "locale", Locale.GERMAN) | ||
.render()); | ||
assertEquals("Dienstag", | ||
engine.parse("{str:fmt(locale,'%tA',now)}") | ||
.data("now", LocalDateTime.of(2016, 7, 26, 12, 0), "locale", Locale.GERMAN) | ||
.render()); | ||
} | ||
|
||
} |
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
70 changes: 70 additions & 0 deletions
70
...te/runtime/src/main/java/io/quarkus/qute/runtime/extensions/StringTemplateExtensions.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,70 @@ | ||
package io.quarkus.qute.runtime.extensions; | ||
|
||
import java.util.Locale; | ||
|
||
import javax.enterprise.inject.Vetoed; | ||
|
||
import io.quarkus.qute.TemplateExtension; | ||
|
||
@Vetoed // Make sure no bean is created from this class | ||
public class StringTemplateExtensions { | ||
|
||
static final String STR = "str"; | ||
|
||
/** | ||
* E.g. {@code strVal.fmt(name,surname)}. The priority must be lower than | ||
* {@link #fmtInstance(String, String, Locale, Object...)}. | ||
* | ||
* @param format | ||
* @param ignoredPropertyName | ||
* @param args | ||
* @return the formatted value | ||
*/ | ||
@TemplateExtension(matchRegex = "fmt|format", priority = 2) | ||
static String fmtInstance(String format, String ignoredPropertyName, Object... args) { | ||
return String.format(format, args); | ||
} | ||
|
||
/** | ||
* E.g. {@code strVal.format(locale,name)}. The priority must be higher than | ||
* {@link #fmtInstance(String, String, Object...)}. | ||
* | ||
* @param format | ||
* @param ignoredPropertyName | ||
* @param locale | ||
* @param args | ||
* @return the formatted value | ||
*/ | ||
@TemplateExtension(matchRegex = "fmt|format", priority = 3) | ||
static String fmtInstance(String format, String ignoredPropertyName, Locale locale, Object... args) { | ||
return String.format(locale, format, args); | ||
} | ||
|
||
/** | ||
* E.g. {@cde str:fmt("Hello %s",name)}. The priority must be lower than {@link #fmt(String, Locale, String, Object...)}. | ||
* | ||
* @param ignoredPropertyName | ||
* @param format | ||
* @param args | ||
* @return the formatted value | ||
*/ | ||
@TemplateExtension(namespace = STR, matchRegex = "fmt|format", priority = 2) | ||
static String fmt(String ignoredPropertyName, String format, Object... args) { | ||
return String.format(format, args); | ||
} | ||
|
||
/** | ||
* E.g. {@code str:fmt(locale,"Hello %s",name)}. The priority must be higher than {@link #fmt(String, String, Object...)}. | ||
* | ||
* @param ignoredPropertyName | ||
* @param locale | ||
* @param format | ||
* @param args | ||
* @return the formatted value | ||
*/ | ||
@TemplateExtension(namespace = STR, matchRegex = "fmt|format", priority = 3) | ||
static String fmt(String ignoredPropertyName, Locale locale, String format, Object... args) { | ||
return String.format(locale, format, args); | ||
} | ||
|
||
} |
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
Oops, something went wrong.