Skip to content

Commit

Permalink
Override defaults from build time that also apply to runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored and gsmet committed Jan 9, 2025
1 parent 7c545fb commit 64573d5
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ final class ReadOperation {
final ConfigTrackingInterceptor buildConfigTracker;
final Set<String> processedNames = new HashSet<>();
final Map<Class<?>, Object> objectsByClass = new HashMap<>();
final Map<String, String> allBuildTimeValues = new TreeMap<>();
final Map<String, String> buildTimeRunTimeValues = new TreeMap<>();
final Map<String, String> runTimeDefaultValues = new TreeMap<>();
final Map<String, String> runTimeValues = new TreeMap<>();
final Map<String, ConfigValue> allBuildTimeValues = new TreeMap<>();
final Map<String, ConfigValue> buildTimeRunTimeValues = new TreeMap<>();
final Map<String, ConfigValue> runTimeDefaultValues = new TreeMap<>();
final Map<String, ConfigValue> runTimeValues = new TreeMap<>();

final Map<ConverterType, Converter<?>> convByType = new HashMap<>();

Expand Down Expand Up @@ -538,7 +538,7 @@ ReadResult run() {
if (matched instanceof FieldContainer) {
ConfigValue configValue = config.getConfigValue(propertyName);
if (configValue.getValue() != null) {
allBuildTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
allBuildTimeValues.put(configValue.getNameProfiled(), configValue);
ni.goToEnd();
// cursor is located after group property key (if any)
getGroup((FieldContainer) matched, ni);
Expand All @@ -557,7 +557,7 @@ ReadResult run() {
Field field = matched.findField();
Converter<?> converter = getConverter(config, field, ConverterType.of(field));
map.put(key, config.convertValue(configValue, converter));
allBuildTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
allBuildTimeValues.put(configValue.getNameProfiled(), configValue);
}
}
// build time (run time visible) patterns
Expand All @@ -570,8 +570,8 @@ ReadResult run() {
ni.goToEnd();
// cursor is located after group property key (if any)
getGroup((FieldContainer) matched, ni);
allBuildTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
buildTimeRunTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
allBuildTimeValues.put(configValue.getNameProfiled(), configValue);
buildTimeRunTimeValues.put(configValue.getNameProfiled(), configValue);
}
} else if (matched != null) {
assert matched instanceof MapContainer;
Expand All @@ -587,8 +587,8 @@ ReadResult run() {
Converter<?> converter = getConverter(config, field, ConverterType.of(field));
map.put(key, config.convertValue(configValue, converter));
// cache the resolved value
allBuildTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
buildTimeRunTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
allBuildTimeValues.put(configValue.getNameProfiled(), configValue);
buildTimeRunTimeValues.put(configValue.getNameProfiled(), configValue);
}
}
// run time patterns
Expand All @@ -599,7 +599,7 @@ ReadResult run() {
// it's a run-time default (record for later)
ConfigValue configValue = withoutExpansion(() -> runtimeConfig.getConfigValue(propertyName));
if (configValue.getValue() != null) {
runTimeValues.put(configValue.getNameProfiled(), configValue.getValue());
runTimeValues.put(configValue.getNameProfiled(), configValue);
}
}

Expand All @@ -610,7 +610,7 @@ ReadResult run() {
// it's not managed by us; record it
ConfigValue configValue = withoutExpansion(() -> runtimeConfig.getConfigValue(propertyName));
if (configValue.getValue() != null) {
runTimeValues.put(propertyName, configValue.getValue());
runTimeValues.put(propertyName, configValue);
}

// in the case the user defined compound keys in YAML (or similar config source, that quotes the name)
Expand Down Expand Up @@ -638,22 +638,22 @@ ReadResult run() {
unknownBuildProperties.remove(property);
ConfigValue value = config.getConfigValue(property);
if (value.getRawValue() != null) {
allBuildTimeValues.put(value.getNameProfiled(), value.getRawValue());
allBuildTimeValues.put(value.getNameProfiled(), value);
}
}
if (buildTimeRunTimeNames.contains(name)) {
unknownBuildProperties.remove(property);
ConfigValue value = config.getConfigValue(property);
if (value.getRawValue() != null) {
allBuildTimeValues.put(value.getNameProfiled(), value.getRawValue());
buildTimeRunTimeValues.put(value.getNameProfiled(), value.getRawValue());
allBuildTimeValues.put(value.getNameProfiled(), value);
buildTimeRunTimeValues.put(value.getNameProfiled(), value);
}
}
if (runTimeNames.contains(name)) {
unknownBuildProperties.remove(property);
ConfigValue value = runtimeConfig.getConfigValue(property);
if (value.getRawValue() != null) {
runTimeValues.put(value.getNameProfiled(), value.getRawValue());
runTimeValues.put(value.getNameProfiled(), value);
}
}
}
Expand Down Expand Up @@ -1168,7 +1168,7 @@ public String getValue(final String propertyName) {
return builder.build();
}

private Map<String, String> filterActiveProfileProperties(final Map<String, String> properties) {
private Map<String, ConfigValue> filterActiveProfileProperties(final Map<String, ConfigValue> properties) {
Set<String> propertiesToRemove = new HashSet<>();
for (String property : properties.keySet()) {
for (String profile : config.getProfiles()) {
Expand All @@ -1182,16 +1182,16 @@ private Map<String, String> filterActiveProfileProperties(final Map<String, Stri
return properties;
}

private static Map<String, String> getDefaults(final SmallRyeConfig config,
private static Map<String, ConfigValue> getDefaults(final SmallRyeConfig config,
final ConfigPatternMap<Container> patternMap) {
Map<String, String> defaultValues = new TreeMap<>();
Map<String, ConfigValue> defaultValues = new TreeMap<>();
getDefaults(config, defaultValues, new StringBuilder(), patternMap);
return defaultValues;
}

private static void getDefaults(
final SmallRyeConfig config,
final Map<String, String> defaultValues,
final Map<String, ConfigValue> defaultValues,
final StringBuilder propertyName,
final ConfigPatternMap<Container> patternMap) {

Expand All @@ -1204,10 +1204,17 @@ private static void getDefaults(
if (defaultValue != null) {
// lookup config to make sure we catch relocates or fallbacks and override the value
ConfigValue configValue = config.getConfigValue(propertyName.toString());
if (configValue.getValue() != null && !configValue.getName().equals(propertyName.toString())) {
defaultValues.put(propertyName.toString(), configValue.getValue());
if (configValue.getValue() != null && !configValue.getName().contentEquals(propertyName)) {
defaultValues.put(propertyName.toString(), configValue);
} else {
defaultValues.put(propertyName.toString(), defaultValue);
defaultValues.put(propertyName.toString(),
ConfigValue.builder()
.withName(propertyName.toString())
.withValue(defaultValue)
.withRawValue(defaultValue)
.withConfigSourceName("DefaultValuesConfigSource")
.withConfigSourceOrdinal(Integer.MIN_VALUE)
.build());
}
}
}
Expand Down Expand Up @@ -1250,10 +1257,10 @@ private static Map<PropertyName, String> mappingsToNames(final List<ConfigClass>
public static final class ReadResult {
final Map<Class<?>, Object> objectsByClass;

final Map<String, String> allBuildTimeValues;
final Map<String, String> buildTimeRunTimeValues;
final Map<String, String> runTimeDefaultValues;
final Map<String, String> runTimeValues;
final Map<String, ConfigValue> allBuildTimeValues;
final Map<String, ConfigValue> buildTimeRunTimeValues;
final Map<String, ConfigValue> runTimeDefaultValues;
final Map<String, ConfigValue> runTimeValues;

final ConfigPatternMap<Container> buildTimePatternMap;
final ConfigPatternMap<Container> buildTimeRunTimePatternMap;
Expand Down Expand Up @@ -1327,19 +1334,19 @@ public Map<Class<?>, Object> getObjectsByClass() {
return objectsByClass;
}

public Map<String, String> getAllBuildTimeValues() {
public Map<String, ConfigValue> getAllBuildTimeValues() {
return allBuildTimeValues;
}

public Map<String, String> getBuildTimeRunTimeValues() {
public Map<String, ConfigValue> getBuildTimeRunTimeValues() {
return buildTimeRunTimeValues;
}

public Map<String, String> getRunTimeDefaultValues() {
public Map<String, ConfigValue> getRunTimeDefaultValues() {
return runTimeDefaultValues;
}

public Map<String, String> getRunTimeValues() {
public Map<String, ConfigValue> getRunTimeValues() {
return runTimeValues;
}

Expand Down Expand Up @@ -1409,10 +1416,10 @@ public ConfigTrackingInterceptor.ReadOptionsProvider getReadOptionsProvider() {

static class Builder {
private Map<Class<?>, Object> objectsByClass;
private Map<String, String> allBuildTimeValues;
private Map<String, String> buildTimeRunTimeValues;
private Map<String, String> runTimeDefaultValues;
private Map<String, String> runtimeValues;
private Map<String, ConfigValue> allBuildTimeValues;
private Map<String, ConfigValue> buildTimeRunTimeValues;
private Map<String, ConfigValue> runTimeDefaultValues;
private Map<String, ConfigValue> runtimeValues;
private ConfigPatternMap<Container> buildTimePatternMap;
private ConfigPatternMap<Container> buildTimeRunTimePatternMap;
private ConfigPatternMap<Container> runTimePatternMap;
Expand All @@ -1433,38 +1440,38 @@ Builder setObjectsByClass(final Map<Class<?>, Object> objectsByClass) {
return this;
}

Map<String, String> getAllBuildTimeValues() {
Map<String, ConfigValue> getAllBuildTimeValues() {
return allBuildTimeValues;
}

Builder setAllBuildTimeValues(final Map<String, String> allBuildTimeValues) {
Builder setAllBuildTimeValues(final Map<String, ConfigValue> allBuildTimeValues) {
this.allBuildTimeValues = allBuildTimeValues;
return this;
}

Map<String, String> getBuildTimeRunTimeValues() {
Map<String, ConfigValue> getBuildTimeRunTimeValues() {
return buildTimeRunTimeValues;
}

Builder setBuildTimeRunTimeValues(final Map<String, String> buildTimeRunTimeValues) {
Builder setBuildTimeRunTimeValues(final Map<String, ConfigValue> buildTimeRunTimeValues) {
this.buildTimeRunTimeValues = buildTimeRunTimeValues;
return this;
}

Map<String, String> getRunTimeDefaultValues() {
Map<String, ConfigValue> getRunTimeDefaultValues() {
return runTimeDefaultValues;
}

Builder setRunTimeDefaultValues(final Map<String, String> runTimeDefaultValues) {
Builder setRunTimeDefaultValues(final Map<String, ConfigValue> runTimeDefaultValues) {
this.runTimeDefaultValues = runTimeDefaultValues;
return this;
}

Map<String, String> getRuntimeValues() {
Map<String, ConfigValue> getRuntimeValues() {
return runtimeValues;
}

Builder setRuntimeValues(final Map<String, String> runtimeValues) {
Builder setRuntimeValues(final Map<String, ConfigValue> runtimeValues) {
this.runtimeValues = runtimeValues;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ public static final class GenerateOperation implements AutoCloseable {
final ResultHandle clinitNameBuilder;
final BuildTimeConfigurationReader.ReadResult buildTimeConfigResult;
final List<RootDefinition> roots;
final Map<String, String> allBuildTimeValues;
// default values given in the build configuration
final Map<String, String> runTimeDefaultValues;
final Map<String, String> buildTimeRunTimeValues;
final Map<Container, MethodDescriptor> enclosingMemberMethods = new HashMap<>();
final Map<Class<?>, MethodDescriptor> groupInitMethods = new HashMap<>();
final Map<Class<?>, FieldDescriptor> configRootsByType = new HashMap<>();
Expand Down Expand Up @@ -256,11 +252,6 @@ public static final class GenerateOperation implements AutoCloseable {
this.liveReloadPossible = builder.liveReloadPossible;
final BuildTimeConfigurationReader.ReadResult buildTimeReadResult = builder.buildTimeReadResult;
buildTimeConfigResult = Assert.checkNotNullParam("buildTimeReadResult", buildTimeReadResult);
allBuildTimeValues = Assert.checkNotNullParam("allBuildTimeValues", buildTimeReadResult.getAllBuildTimeValues());
runTimeDefaultValues = Assert.checkNotNullParam("runTimeDefaultValues",
buildTimeReadResult.getRunTimeDefaultValues());
buildTimeRunTimeValues = Assert.checkNotNullParam("buildTimeRunTimeValues",
buildTimeReadResult.getBuildTimeRunTimeValues());
classOutput = Assert.checkNotNullParam("classOutput", builder.getClassOutput());
roots = Assert.checkNotNullParam("builder.roots", builder.getBuildTimeReadResult().getAllRoots());
additionalTypes = Assert.checkNotNullParam("additionalTypes", builder.getAdditionalTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.quarkus.bootstrap.util.PropertyUtils;
import io.quarkus.deployment.configuration.BuildTimeConfigurationReader;
import io.quarkus.runtime.LaunchMode;
import io.smallrye.config.ConfigValue;

public class ConfigTrackingWriter {

Expand Down Expand Up @@ -77,8 +78,8 @@ public static void write(Map<String, String> readOptions, ConfigTrackingConfig c
final List<Pattern> excludePatterns = config.getExcludePatterns();
final ConfigTrackingValueTransformer valueTransformer = ConfigTrackingValueTransformer.newInstance(config);

final Map<String, String> allBuildTimeValues = configReadResult.getAllBuildTimeValues();
final Map<String, String> buildTimeRuntimeValues = configReadResult.getBuildTimeRunTimeValues();
final Map<String, ConfigValue> allBuildTimeValues = configReadResult.getAllBuildTimeValues();
final Map<String, ConfigValue> buildTimeRuntimeValues = configReadResult.getBuildTimeRunTimeValues();
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
final List<String> names = new ArrayList<>(readOptions.size());
for (var name : readOptions.keySet()) {
Expand Down
Loading

0 comments on commit 64573d5

Please sign in to comment.