Skip to content

Commit

Permalink
Support prefix in @configroot
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 13, 2021
1 parent 8926d29 commit 4a662fe
Show file tree
Hide file tree
Showing 26 changed files with 536 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static io.quarkus.deployment.util.ReflectUtil.toError;
import static io.quarkus.deployment.util.ReflectUtil.typeOfParameter;
import static io.quarkus.deployment.util.ReflectUtil.unwrapInvocationTargetException;
import static java.util.stream.Collectors.toSet;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -55,11 +56,13 @@
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.runtime.configuration.HyphenateEnumConverter;
import io.quarkus.runtime.configuration.NameIterator;
import io.quarkus.runtime.configuration.PropertiesUtil;
import io.smallrye.config.Converters;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.Expressions;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.SysPropConfigSource;

/**
* A configuration reader.
Expand Down Expand Up @@ -91,14 +94,17 @@ public BuildTimeConfigurationReader(final List<Class<?>> configRoots) {
List<RootDefinition> buildTimeRoots = new ArrayList<>();
Map<Class<?>, GroupDefinition> groups = new HashMap<>();
for (Class<?> configRoot : configRoots) {
String prefix = "quarkus";
String name = ConfigItem.HYPHENATED_ELEMENT_NAME;
ConfigPhase phase = ConfigPhase.BUILD_TIME;
ConfigRoot annotation = configRoot.getAnnotation(ConfigRoot.class);
if (annotation != null) {
prefix = annotation.prefix();
name = annotation.name();
phase = annotation.phase();
}
RootDefinition.Builder defBuilder = new RootDefinition.Builder();
defBuilder.setPrefix(prefix);
defBuilder.setConfigPhase(phase);
defBuilder.setRootName(name);
processClass(defBuilder, configRoot, groups);
Expand Down Expand Up @@ -272,9 +278,7 @@ final class ReadOperation {

ReadResult run() {
final StringBuilder nameBuilder;
nameBuilder = new StringBuilder().append("quarkus");
// eager init first
int len = nameBuilder.length();
nameBuilder = new StringBuilder();
for (RootDefinition root : buildTimeVisibleRoots) {
Class<?> clazz = root.getConfigurationClass();
Object instance;
Expand All @@ -292,23 +296,21 @@ ReadResult run() {
throw toError(e);
}
objectsByRootClass.put(clazz, instance);
String rootName = root.getRootName();
if (!rootName.isEmpty()) {
nameBuilder.append('.').append(rootName);
}
nameBuilder.append(root.getName());
readConfigGroup(root, instance, nameBuilder);
nameBuilder.setLength(len);
nameBuilder.setLength(0);
}

Set<String> registeredRoots = allRoots.stream().map(RootDefinition::getPrefix).collect(toSet());
// sweep-up
SmallRyeConfig runtimeDefaultsConfig = getConfigForRuntimeDefaults();
for (String propertyName : getAllProperties()) {
for (String propertyName : getAllProperties(registeredRoots)) {
if (propertyName.equals(ConfigSource.CONFIG_ORDINAL)) {
continue;
}

NameIterator ni = new NameIterator(propertyName);
if (ni.hasNext() && ni.nextSegmentEquals("quarkus")) {
ni.next();
if (ni.hasNext() && PropertiesUtil.isPropertyInRoot(registeredRoots, ni)) {
// build time patterns
Container matched = buildTimePatternMap.match(ni);
if (matched instanceof FieldContainer) {
Expand Down Expand Up @@ -337,7 +339,6 @@ ReadResult run() {
}
// build time (run time visible) patterns
ni.goToStart();
ni.next();
matched = buildTimeRunTimePatternMap.match(ni);
if (matched instanceof FieldContainer) {
ni.goToEnd();
Expand Down Expand Up @@ -369,7 +370,6 @@ ReadResult run() {
}
// run time patterns
ni.goToStart();
ni.next();
matched = runTimePatternMap.match(ni);
if (matched != null) {
// it's a specified run-time default (record for later)
Expand All @@ -380,7 +380,6 @@ ReadResult run() {
}
// also check for the bootstrap properties since those need to be added to specifiedRunTimeDefaultValues as well
ni.goToStart();
ni.next();
matched = bootstrapPatternMap.match(ni);
if (matched != null) {
// it's a specified run-time default (record for later)
Expand Down Expand Up @@ -732,11 +731,23 @@ private Converter<?> getConverter(SmallRyeConfig config, Field field, ConverterT
* We collect all properties from ConfigSources, because Config#getPropertyNames exclude the active profiled
* properties, meaning that the property is written in the default config source unprofiled. This may cause
* issues if we run with a different profile and fallback to defaults.
*
* We also filter the properties coming from the System with the registered roots, because we don't want to
* record properties set by the compiling JVM (or other properties set that are only related to the build).
*/
private Set<String> getAllProperties() {
private Set<String> getAllProperties(final Set<String> registeredRoots) {
Set<String> properties = new HashSet<>();
for (ConfigSource configSource : config.getConfigSources()) {
properties.addAll(configSource.getPropertyNames());
if (configSource instanceof SysPropConfigSource) {
for (String propertyName : configSource.getProperties().keySet()) {
NameIterator ni = new NameIterator(propertyName);
if (ni.hasNext() && PropertiesUtil.isPropertyInRoot(registeredRoots, ni)) {
properties.add(propertyName);
}
}
} else {
properties.addAll(configSource.getPropertyNames());
}
}
for (String propertyName : config.getPropertyNames()) {
properties.add(propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public Set<String> getPropertyNames() {
}

public String getValue(final String propertyName) {
if (!propertyName.startsWith("quarkus.")) {
return null;
}
final Container match = leafs.match(propertyName.substring(8));
final Container match = leafs.match(propertyName);
if (match == null) {
return null;
}
Expand Down

This file was deleted.

Loading

0 comments on commit 4a662fe

Please sign in to comment.