Skip to content

Commit

Permalink
Fix #363 - Multiple Configuration Profiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Oct 3, 2020
1 parent da4ff04 commit d310df3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.smallrye.config;

import static io.smallrye.config.Converters.getImplicitConverter;
import static io.smallrye.config.Converters.newArrayConverter;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -27,10 +30,11 @@ public class ProfileConfigSourceInterceptor implements ConfigSourceInterceptor {
}
};

private final String profile;
private final String[] profiles;

public ProfileConfigSourceInterceptor(final String profile) {
this.profile = profile;
this.profiles = profile != null ? newArrayConverter(getImplicitConverter(String.class), String[].class).convert(profile)
: new String[0];
}

public ProfileConfigSourceInterceptor(final ConfigSourceInterceptorContext context) {
Expand All @@ -40,14 +44,14 @@ public ProfileConfigSourceInterceptor(final ConfigSourceInterceptorContext conte
public ProfileConfigSourceInterceptor(
final ConfigSourceInterceptorContext context,
final String profileConfigName) {
this.profile = Optional.ofNullable(context.proceed(profileConfigName)).map(ConfigValue::getValue).orElse(null);
this(Optional.ofNullable(context.proceed(profileConfigName)).map(ConfigValue::getValue).orElse(null));
}

@Override
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) {
if (profile != null) {
if (profiles.length > 0) {
final String normalizeName = normalizeName(name);
final ConfigValue profileValue = context.proceed("%" + profile + "." + normalizeName);
final ConfigValue profileValue = getProfileValue(context, normalizeName);
if (profileValue != null) {
try {
final ConfigValue originalValue = context.proceed(normalizeName);
Expand All @@ -64,6 +68,17 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
return context.proceed(name);
}

public ConfigValue getProfileValue(final ConfigSourceInterceptorContext context, final String normalizeName) {
for (String profile : profiles) {
final ConfigValue profileValue = context.proceed("%" + profile + "." + normalizeName);
if (profileValue != null) {
return profileValue;
}
}

return null;
}

@Override
public Iterator<String> iterateNames(final ConfigSourceInterceptorContext context) {
final Set<String> names = new HashSet<>();
Expand All @@ -79,6 +94,6 @@ public Iterator<ConfigValue> iterateValues(final ConfigSourceInterceptorContext
}

private String normalizeName(final String name) {
return name.startsWith("%" + profile + ".") ? name.substring(profile.length() + 2) : name;
return name.startsWith("%") ? name.substring(name.indexOf(".") + 1) : name;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.smallrye.config;

import static io.smallrye.config.KeyValuesConfigSource.config;
import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -9,6 +10,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.StreamSupport;

import org.eclipse.microprofile.config.Config;
Expand All @@ -19,18 +21,18 @@
public class ProfileConfigSourceInterceptorTest {
@Test
public void profile() {
final SmallRyeConfig config = (SmallRyeConfig) buildConfig("my.prop", "1", "%prof.my.prop", "2", SMALLRYE_PROFILE,
"prof");
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "2", SMALLRYE_PROFILE, "prof");

assertEquals("2", config.getValue("my.prop", String.class));

assertEquals("my.prop", config.getConfigValue("my.prop").getName());
assertEquals("my.prop", config.getConfigValue("%prof.my.prop").getName());
assertEquals("2", config.getConfigValue("%prof.my.prop").getValue());
}

@Test
public void profileOnly() {
final Config config = buildConfig("my.prop", "1", "%prof.my.prop", "2", SMALLRYE_PROFILE, "prof");
final Config config = buildConfig("%prof.my.prop", "2", SMALLRYE_PROFILE, "prof");

assertEquals("2", config.getValue("my.prop", String.class));
}
Expand Down Expand Up @@ -59,13 +61,24 @@ public void profileExpressions() {
assertEquals("2", config.getValue("my.prop", String.class));
}

@Test
void cannotExpand() {
final Config config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(config("my.prop", "${another.prop}", SMALLRYE_PROFILE, "prof", "config_ordinal", "1000"))
.withSources(config("my.prop", "${another.prop}", "%prof.my.prop", "2", SMALLRYE_PROFILE, "prof"))
.build();

assertThrows(NoSuchElementException.class, () -> config.getValue("my.prop", String.class));
}

@Test
public void customConfigProfile() {
final String[] configs = { "my.prop", "1", "%prof.my.prop", "2", "config.profile", "prof" };
final Config config = new SmallRyeConfigBuilder()
.addDefaultSources()
.addDiscoveredInterceptors()
.withSources(KeyValuesConfigSource.config(configs))
.withSources(config(configs))
.build();

assertEquals("2", config.getValue("my.prop", String.class));
Expand All @@ -75,14 +88,11 @@ public void customConfigProfile() {
public void noConfigProfile() {
final String[] configs = { "my.prop", "1", "%prof.my.prop", "2" };
final Config config = new SmallRyeConfigBuilder()
.addDefaultSources()
.withSources(KeyValuesConfigSource.config(configs))
.withInterceptors(
new ProfileConfigSourceInterceptor("prof"),
new ExpressionConfigSourceInterceptor())
.addDefaultInterceptors()
.withSources(config(configs))
.build();

assertEquals("2", config.getValue("my.prop", String.class));
assertEquals("1", config.getValue("my.prop", String.class));
}

@Test
Expand Down Expand Up @@ -166,7 +176,7 @@ public void priorityProfileOverOriginal() {

@Test
public void propertyNames() {
final SmallRyeConfig config = (SmallRyeConfig) buildConfig("my.prop", "1", "%prof.my.prop", "2", "%prof.prof.only", "1",
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "2", "%prof.prof.only", "1",
SMALLRYE_PROFILE, "prof");

assertEquals("2", config.getConfigValue("my.prop").getValue());
Expand All @@ -181,16 +191,64 @@ public void propertyNames() {
@Test
public void profileName() {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(KeyValuesConfigSource.config("my.prop", "1", "%prof.my.prop", "2"))
.withSources(config("my.prop", "1", "%prof.my.prop", "2"))
.withProfile("prof")
.build();

assertEquals("2", config.getConfigValue("my.prop").getValue());
}

private static Config buildConfig(String... keyValues) {
@Test
void multipleProfiles() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_PROFILE, "common,prof", "config_ordinal", "1000"))
.withSources(config("%common.common.prop", "1234", "%prof.my.prop", "5678"))
.addDefaultInterceptors()
.build();

assertEquals("1234", config.getRawValue("common.prop"));
assertEquals("5678", config.getRawValue("my.prop"));
}

@Test
void multipleProfilesSamePriority() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_PROFILE, "common,prof", "config_ordinal", "1000"))
.withSources(config("%common.common.prop", "1234", "%prof.common.prop", "5678"))
.addDefaultInterceptors()
.build();

assertEquals("1234", config.getRawValue("common.prop"));
}

@Test
void multipleProfilesDifferentPriorities() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_PROFILE, "common,prof", "config_ordinal", "1000"))
.withSources(config("%prof.common.prop", "5678", "config_ordinal", "500"))
.withSources(config("%common.common.prop", "1234", "config_ordinal", "300"))
.addDefaultInterceptors()
.build();

assertEquals("1234", config.getRawValue("common.prop"));
}

@Test
void multipleProfilesDifferentPrioritiesMain() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_PROFILE, "common,prof", "config_ordinal", "1000"))
.withSources(config("common.prop", "9", "config_ordinal", "900"))
.withSources(config("%prof.common.prop", "5678", "config_ordinal", "500"))
.withSources(config("%common.common.prop", "1234", "config_ordinal", "300"))
.addDefaultInterceptors()
.build();

assertEquals("9", config.getRawValue("common.prop"));
}

private static SmallRyeConfig buildConfig(String... keyValues) {
return new SmallRyeConfigBuilder()
.withSources(KeyValuesConfigSource.config(keyValues))
.withSources(config(keyValues))
.withInterceptors(
new ProfileConfigSourceInterceptor("prof"),
new ExpressionConfigSourceInterceptor())
Expand Down

0 comments on commit d310df3

Please sign in to comment.