Skip to content

Commit

Permalink
Merge pull request #46071 from gsmet/configmapping-qute
Browse files Browse the repository at this point in the history
Qute - Switch to @ConfigMapping
  • Loading branch information
mkouba authored Feb 6, 2025
2 parents 1aa5dae + 00dd1bf commit 822227e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 76 deletions.
3 changes: 0 additions & 3 deletions extensions/qute/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public String apply(String id) {
TemplateAnalysis templateAnalysis = exprEntry.getKey();

String path = templateAnalysis.path;
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
if (path.endsWith(suffix)) {
path = path.substring(0, path.length() - (suffix.length() + 1));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public void beforeParsing(ParserHelper parserHelper) {
}

private String templatePathWithoutSuffix(String path, QuteConfig config) {
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
if (path.endsWith(suffix)) {
// Remove the suffix
path = path.substring(0, path.length() - (suffix.length() + 1));
Expand Down Expand Up @@ -911,7 +911,7 @@ void validateCheckedFragments(List<CheckedFragmentValidationBuildItem> validatio

@BuildStep(onlyIf = IsTest.class)
SyntheticBeanBuildItem registerRenderedResults(QuteConfig config) {
if (config.testMode.recordRenderedResults) {
if (config.testMode().recordRenderedResults()) {
return SyntheticBeanBuildItem.configure(RenderedResults.class)
.unremovable()
.scope(Singleton.class)
Expand Down Expand Up @@ -1131,7 +1131,7 @@ private CheckedTemplateBuildItem findCheckedTemplate(QuteConfig config, Template
List<CheckedTemplateBuildItem> checkedTemplates) {
// Try to find the checked template
String path = analysis.path;
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
if (path.endsWith(suffix)) {
path = path.substring(0, path.length() - (suffix.length() + 1));
break;
Expand Down Expand Up @@ -1638,19 +1638,19 @@ private static boolean isInvalidCheckedTemplateExpression(QuteConfig config, Che
if (!expression.hasNamespace() && expression.getParts().size() == 1
&& ITERATION_METADATA_KEYS.contains(expression.getParts().get(0).getName())) {
String prefixInfo;
if (config.iterationMetadataPrefix
if (config.iterationMetadataPrefix()
.equals(LoopSectionHelper.Factory.ITERATION_METADATA_PREFIX_ALIAS_UNDERSCORE)) {
prefixInfo = String.format(
"based on the iteration alias, i.e. the correct key should be something like {it_%1$s} or {element_%1$s}",
expression.getParts().get(0).getName());
} else if (config.iterationMetadataPrefix
} else if (config.iterationMetadataPrefix()
.equals(LoopSectionHelper.Factory.ITERATION_METADATA_PREFIX_ALIAS_QM)) {
prefixInfo = String.format(
"based on the iteration alias, i.e. the correct key should be something like {it?%1$s} or {element?%1$s}",
expression.getParts().get(0).getName());
} else {
prefixInfo = ": " + config.iterationMetadataPrefix + ", i.e. the correct key should be: "
+ config.iterationMetadataPrefix + expression.getParts().get(0).getName();
prefixInfo = ": " + config.iterationMetadataPrefix() + ", i.e. the correct key should be: "
+ config.iterationMetadataPrefix() + expression.getParts().get(0).getName();
}
incorrectExpressions.produce(new IncorrectExpressionBuildItem(expression.toOriginalString(),
"An invalid iteration metadata key is probably used\n\t- The configured iteration metadata prefix is "
Expand Down Expand Up @@ -2241,7 +2241,7 @@ private void scanPathTree(PathTree pathTree, TemplateRootsBuildItem templateRoot
// remove templateRoot + /
final String relativePath = visit.getRelativePath();
String templatePath = relativePath.substring(templateRoot.length() + 1);
if (config.templatePathExclude.matcher(templatePath).matches()) {
if (config.templatePathExclude().matcher(templatePath).matches()) {
LOGGER.debugf("Template file excluded: %s", visit.getPath());
return;
}
Expand All @@ -2261,7 +2261,7 @@ TemplateFilePathsBuildItem collectTemplateFilePaths(QuteConfig config, List<Temp
filePaths.add(path);
// Also add version without suffix from the path
// For example for "items.html" also add "items"
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
if (path.endsWith(suffix)) {
filePaths.add(path.substring(0, path.length() - (suffix.length() + 1)));
}
Expand Down Expand Up @@ -2480,8 +2480,8 @@ public boolean test(TypeCheck typeCheck) {
}
}, true));

if (config.typeCheckExcludes.isPresent()) {
for (String exclude : config.typeCheckExcludes.get()) {
if (config.typeCheckExcludes().isPresent()) {
for (String exclude : config.typeCheckExcludes().get()) {
//
String[] parts = exclude.split("\\.");
if (parts.length < 2) {
Expand Down Expand Up @@ -3527,14 +3527,14 @@ private static void produceTemplateBuildItems(BuildProducer<TemplatePathBuildIte
resourcePath,
originalPath);
boolean restartNeeded = true;
if (config.devMode.noRestartTemplates.isPresent()) {
restartNeeded = !config.devMode.noRestartTemplates.get().matcher(resourcePath).matches();
if (config.devMode().noRestartTemplates().isPresent()) {
restartNeeded = !config.devMode().noRestartTemplates().get().matcher(resourcePath).matches();
}
watchedPaths.produce(new HotDeploymentWatchedFileBuildItem(resourcePath, restartNeeded));
nativeImageResources.produce(new NativeImageResourceBuildItem(resourcePath));
templatePaths.produce(
new TemplatePathBuildItem(templatePath, originalPath,
readTemplateContent(originalPath, config.defaultCharset)));
readTemplateContent(originalPath, config.defaultCharset())));
}

private static boolean isExcluded(TypeCheck check, Iterable<Predicate<TypeCheck>> excludes) {
Expand Down
3 changes: 0 additions & 3 deletions extensions/qute/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getContentType(String templatePath) {
int dotIdx = fileName.lastIndexOf('.');
if (dotIdx != -1) {
String suffix = fileName.substring(dotIdx + 1, fileName.length());
String additionalContentType = config.contentTypes.get(suffix);
String additionalContentType = config.contentTypes().get(suffix);
if (additionalContentType != null) {
return additionalContentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public EngineProducer(QuteContext context, QuteConfig config, QuteRuntimeConfig
@All List<SectionHelperFactory<?>> sectionHelperFactories, @All List<ValueResolver> valueResolvers,
@All List<NamespaceResolver> namespaceResolvers, @All List<ParserHook> parserHooks) {
this.contentTypes = contentTypes;
this.suffixes = config.suffixes;
this.suffixes = config.suffixes();
this.templateRoots = context.getTemplateRoots();
this.templateContents = Map.copyOf(context.getTemplateContents());
this.tags = context.getTags();
this.templatePathExclude = config.templatePathExclude;
this.templatePathExclude = config.templatePathExclude();
this.defaultLocale = locales.defaultLocale().orElse(Locale.getDefault());
this.defaultCharset = config.defaultCharset;
this.defaultCharset = config.defaultCharset();
this.container = Arc.container();

LOGGER.debugf("Initializing Qute [templates: %s, tags: %s, resolvers: %s", context.getTemplatePaths(), tags,
Expand Down Expand Up @@ -127,13 +127,13 @@ public EngineProducer(QuteContext context, QuteConfig config, QuteRuntimeConfig
}

// Enable/disable strict rendering
if (runtimeConfig.strictRendering) {
if (runtimeConfig.strictRendering()) {
builder.strictRendering(true);
} else {
builder.strictRendering(false);
// If needed, use a specific result mapper for the selected strategy
if (runtimeConfig.propertyNotFoundStrategy.isPresent()) {
switch (runtimeConfig.propertyNotFoundStrategy.get()) {
if (runtimeConfig.propertyNotFoundStrategy().isPresent()) {
switch (runtimeConfig.propertyNotFoundStrategy().get()) {
case THROW_EXCEPTION:
builder.addResultMapper(new PropertyNotFoundThrowException());
break;
Expand All @@ -156,7 +156,7 @@ public EngineProducer(QuteContext context, QuteConfig config, QuteRuntimeConfig
}

// Escape some characters for HTML/XML templates
builder.addResultMapper(new HtmlEscaper(List.copyOf(config.escapeContentTypes)));
builder.addResultMapper(new HtmlEscaper(List.copyOf(config.escapeContentTypes())));

// Escape some characters for JSON templates
builder.addResultMapper(new JsonEscaper());
Expand All @@ -165,10 +165,10 @@ public EngineProducer(QuteContext context, QuteConfig config, QuteRuntimeConfig
builder.addValueResolver(new ReflectionValueResolver());

// Remove standalone lines if desired
builder.removeStandaloneLines(runtimeConfig.removeStandaloneLines);
builder.removeStandaloneLines(runtimeConfig.removeStandaloneLines());

// Iteration metadata prefix
builder.iterationMetadataPrefix(config.iterationMetadataPrefix);
builder.iterationMetadataPrefix(config.iterationMetadataPrefix());

// Default section helpers
builder.addDefaultSectionHelpers();
Expand Down Expand Up @@ -257,8 +257,8 @@ public void run() {
}
});

builder.timeout(runtimeConfig.timeout);
builder.useAsyncTimeout(runtimeConfig.useAsyncTimeout);
builder.timeout(runtimeConfig.timeout());
builder.useAsyncTimeout(runtimeConfig.useAsyncTimeout());

engine = builder.build();

Expand All @@ -267,7 +267,7 @@ public void run() {
for (String path : context.getTemplatePaths()) {
Template template = engine.getTemplate(path);
if (template != null) {
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
if (path.endsWith(suffix)) {
String pathNoSuffix = path.substring(0, path.length() - (suffix.length() + 1));
List<Template> templates = discovered.get(pathNoSuffix);
Expand Down Expand Up @@ -456,7 +456,7 @@ private void processDefaultTemplate(String path, List<Template> templates, QuteC
if (engine.isTemplateLoaded(path)) {
return;
}
for (String suffix : config.suffixes) {
for (String suffix : config.suffixes()) {
for (Template template : templates) {
if (template.getId().endsWith(suffix)) {
engine.putTemplate(path, template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.util.regex.Pattern;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class QuteConfig {
@ConfigMapping(prefix = "quarkus.qute")
public interface QuteConfig {

/**
* The list of suffixes used when attempting to locate a template file.
Expand All @@ -21,16 +23,15 @@ public class QuteConfig {
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "qute.html,qute.txt,html,txt")
public List<String> suffixes;
@WithDefault("qute.html,qute.txt,html,txt")
List<String> suffixes();

/**
* The additional map of suffixes to content types. This map is used when working with template variants. By default, the
* {@link java.net.URLConnection#getFileNameMap()} is used to determine the content type of a template file.
*/
@ConfigItem
@ConfigDocMapKey("file-suffix")
public Map<String, String> contentTypes;
Map<String, String> contentTypes();

/**
* The list of exclude rules used to intentionally ignore some parts of an expression when performing type-safe validation.
Expand All @@ -45,8 +46,7 @@ public class QuteConfig {
* <li>{@code *.age} - exclude the property/method {@code age} on any class</li>
* </ul>
*/
@ConfigItem
public Optional<List<String>> typeCheckExcludes;
Optional<List<String>> typeCheckExcludes();

/**
* This regular expression is used to exclude template files from the {@code templates} directory. Excluded templates are
Expand All @@ -57,8 +57,8 @@ public class QuteConfig {
* <p>
* By default, the hidden files are excluded. The name of a hidden file starts with a dot.
*/
@ConfigItem(defaultValue = "^\\..*|.*\\/\\..*$")
public Pattern templatePathExclude;
@WithDefault("^\\..*|.*\\/\\..*$")
Pattern templatePathExclude();

/**
* The prefix is used to access the iteration metadata inside a loop section.
Expand All @@ -74,32 +74,30 @@ public class QuteConfig {
* </ul>
* By default, the {@code <alias_>} constant is set.
*/
@ConfigItem(defaultValue = "<alias_>")
public String iterationMetadataPrefix;
@WithDefault("<alias_>")
String iterationMetadataPrefix();

/**
* The list of content types for which the {@code '}, {@code "}, {@code <}, {@code >} and {@code &} characters are escaped
* if a template variant is set.
*/
@ConfigItem(defaultValue = "text/html,text/xml,application/xml,application/xhtml+xml")
public List<String> escapeContentTypes;
@WithDefault("text/html,text/xml,application/xml,application/xhtml+xml")
List<String> escapeContentTypes();

/**
* The default charset of the templates files.
*/
@ConfigItem(defaultValue = "UTF-8")
public Charset defaultCharset;
@WithDefault("UTF-8")
Charset defaultCharset();

/**
* Development mode configuration.
*/
@ConfigItem
public QuteDevModeConfig devMode;
QuteDevModeConfig devMode();

/**
* Test mode configuration.
*/
@ConfigItem
public QuteTestModeConfig testMode;
QuteTestModeConfig testMode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.regex.Pattern;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class QuteDevModeConfig {
public interface QuteDevModeConfig {

/**
* By default, a template modification results in an application restart that triggers build-time validations.
Expand All @@ -18,7 +17,6 @@ public class QuteDevModeConfig {
* The matched input is the template path that starts with a template root, and the {@code /} is used as a path separator.
* For example, {@code templates/foo.html}.
*/
@ConfigItem
public Optional<Pattern> noRestartTemplates;
Optional<Pattern> noRestartTemplates();

}
Loading

0 comments on commit 822227e

Please sign in to comment.