Skip to content

Commit

Permalink
Correctly handle profile-specific properties
Browse files Browse the repository at this point in the history
  • Loading branch information
barchetta committed May 13, 2024
1 parent 2185987 commit 4993661
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.helidon.config.ConfigSources;
import io.helidon.config.mp.spi.MpConfigFilter;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -91,6 +92,8 @@ class MpConfigImpl implements Config {
this.converters.putIfAbsent(String.class, value -> value);
this.configProfile = profile;

dumpConfigSources(this.sources);

this.valueResolving = getOptionalValue("mp.config.property.expressions.enabled", Boolean.class)
.or(() -> getOptionalValue("helidon.config.value-resolving.enabled", Boolean.class))
.orElse(true);
Expand All @@ -105,15 +108,29 @@ class MpConfigImpl implements Config {
});
}

private void dumpConfigSources(List<ConfigSource> sources) {
System.out.println("CCCC ConfigSources");
for (ConfigSource source : sources) {
System.out.println(" " + source.getOrdinal() + " " + source.getName());
}
}

@Override
public ConfigValue getConfigValue(String key) {

ConfigValue value = findConfigValue(key)
.orElseGet(() -> new ConfigValueImpl(key, null, null, null, 0));

if (configProfile == null) {
return findConfigValue(key)
.orElseGet(() -> new ConfigValueImpl(key, null, null, null, 0));
return value;
}
return findConfigValue("%" + configProfile + "." + key)
.or(() -> findConfigValue(key))

ConfigValue profileValue = findConfigValue("%" + configProfile + "." + key)
.orElseGet(() -> new ConfigValueImpl(key, null, null, null, 0));

System.out.println("CCCCC " + "key=" + key + " value=" + value);
System.out.println("CCCCC " + "key=" + key + " profileValue=" + profileValue);
return value.getSourceOrdinal() > profileValue.getSourceOrdinal() ? value : profileValue;
}

@Override
Expand All @@ -126,12 +143,7 @@ public <T> T getValue(String propertyName, Class<T> propertyType) {
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) {
if (configProfile == null) {
return optionalValue(propertyName, propertyType);
}

return optionalValue("%" + configProfile + "." + propertyName, propertyType)
.or(() -> optionalValue(propertyName, propertyType));
return optionalValue(propertyName, propertyType);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -187,9 +199,9 @@ private <T> Optional<T> optionalValue(String propertyName, Class<T> propertyType
return Optional.empty();
}
} else {
return findConfigValue(propertyName)
.map(ConfigValue::getValue)
.map(it -> convert(propertyName, propertyType, it));
Optional<ConfigValue> value = Optional.of(getConfigValue(propertyName));
return value.map(ConfigValue::getValue)
.map(it -> convert(propertyName, propertyType, it));
}
}

Expand Down Expand Up @@ -314,6 +326,7 @@ private <T> T convert(String propertyName, Class<T> type, String value) {
}

private Optional<ConfigValue> findConfigValue(String propertyName) {

for (ConfigSource source : sources) {
String value = source.getValue(propertyName);

Expand Down

0 comments on commit 4993661

Please sign in to comment.