Skip to content

Commit

Permalink
Support Parent Profile in Active Profile (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Mar 11, 2022
1 parent a2cf132 commit ced950e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static io.smallrye.config.Converters.STRING_CONVERTER;
import static io.smallrye.config.Converters.newCollectionConverter;
import static io.smallrye.config.Converters.newTrimmingConverter;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -107,14 +109,21 @@ public static List<String> convertProfile(final String profile) {

private static List<String> convertProfile(final ConfigSourceInterceptorContext context) {
final List<String> profiles = new ArrayList<>();
final ConfigValue profile = context.proceed(SmallRyeConfig.SMALLRYE_CONFIG_PROFILE);
final ConfigValue profile = context.proceed(SMALLRYE_CONFIG_PROFILE);
if (profile != null) {
final ConfigValue parentProfile = context.proceed(SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT);
final ConfigValue parentProfile = context.proceed(SMALLRYE_CONFIG_PROFILE_PARENT);
if (parentProfile != null) {
profiles.add(parentProfile.getValue());
}
final List<String> convertedProfiles = convertProfile(profile.getValue());
if (convertedProfiles != null) {
final ProfileConfigSourceInterceptor profileConfigSourceInterceptor = new ProfileConfigSourceInterceptor(
convertedProfiles);
final ConfigValue parentProfileInActiveProfile = profileConfigSourceInterceptor.getValue(context,
SMALLRYE_CONFIG_PROFILE_PARENT);
if (parentProfileInActiveProfile != null) {
profiles.add(parentProfileInActiveProfile.getValue());
}
profiles.addAll(convertedProfiles);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.OptionalInt;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -401,6 +403,86 @@ void parentProfile() {
assertEquals("5678", config.getRawValue("my.prop"));
}

@Test
void parentProfileInActiveProfile() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_CONFIG_PROFILE, "custom"))
.withSources(config("my.config1", "prod",
"my.config2", "prod",
"%dev.my.config1", "dev",
"%custom.smallrye.config.profile.parent", "dev",
"%custom.my.config2", "custom"))
.addDefaultInterceptors()
.build();

assertEquals("custom", config.getRawValue("my.config2"));
assertEquals("dev", config.getRawValue("my.config1"));
assertEquals("dev", config.getRawValue(SMALLRYE_CONFIG_PROFILE_PARENT));
}

@Test
void multipleProfilesSingleParent() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_CONFIG_PROFILE, "another,custom"))
.withSources(config("my.config1", "prod",
"my.config2", "prod",
"%dev.my.config1", "dev",
"%custom.my.config2", "custom"))
.withSources(config("config_ordinal", "1000",
"%custom.smallrye.config.profile.parent", "dev"))
.withSources(config("config_ordinal", "0",
"%another.smallrye.config.profile.parent", "prod"))
.addDefaultInterceptors()
.build();

assertEquals("custom", config.getRawValue("my.config2"));
assertEquals("dev", config.getRawValue("my.config1"));
assertEquals("dev", config.getRawValue(SMALLRYE_CONFIG_PROFILE_PARENT));
}

@Test
void parentProfileInActiveProfileWithRelocate() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
Map<String, String> relocations = new HashMap<>();
relocations.put(SMALLRYE_CONFIG_PROFILE_PARENT, "quarkus.config.profile.parent");

ConfigValue profileValue = context.proceed(SMALLRYE_CONFIG_PROFILE);
if (profileValue != null) {
List<String> profiles = ProfileConfigSourceInterceptor.convertProfile(profileValue.getValue());
for (String profile : profiles) {
relocations.put("%" + profile + "." + SMALLRYE_CONFIG_PROFILE_PARENT,
"%" + profile + "." + "quarkus.config.profile.parent");
}
}

return new RelocateConfigSourceInterceptor(relocations);
}

@Override
public OptionalInt getPriority() {
// Profile is 200, needs to execute before
return OptionalInt.of(Priorities.LIBRARY + 200 - 5);
}
})
.withSources(config(SMALLRYE_CONFIG_PROFILE, "custom"))
.withSources(config(
"my.config1", "prod",
"my.config2", "prod",
"%dev.my.config1", "dev",
"%custom.quarkus.config.profile.parent", "dev",
"%custom.my.config2", "custom"))
.addDefaultInterceptors()
.build();

assertEquals("dev", config.getRawValue(SMALLRYE_CONFIG_PROFILE_PARENT));
assertEquals("dev", config.getRawValue("quarkus.config.profile.parent"));
assertEquals("dev", config.getRawValue("my.config1"));
assertEquals("custom", config.getRawValue("my.config2"));
}

@Test
void whitespaceProfiles() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Set;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -70,6 +71,32 @@ void relocateWithProfile() {
assertEquals("Cookie", config.getValue("smallrye.jwt.token.header", String.class));
}

@Test
void relocateWithProfileWithMappingProperties() {
Map<String, String> relocations = new HashMap<>();
relocations.put("original.name", "relocated.name");

SmallRyeConfig config = new SmallRyeConfigBuilder()
.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
return new RelocateConfigSourceInterceptor(relocations);
}

@Override
public OptionalInt getPriority() {
return OptionalInt.of(Priorities.LIBRARY + 300);
}
})
.withSources(config(SMALLRYE_CONFIG_PROFILE, "custom"))
.withSources(config("%custom.original.name", "original", "%custom.relocated.name", "relocated"))
.addDefaultInterceptors()
.build();

assertEquals("relocated", config.getRawValue("original.name"));
assertEquals("relocated", config.getRawValue("relocated.name"));
}

@Test
void relocateWithProfileAndExpression() {
Config config = buildConfig(
Expand Down

0 comments on commit ced950e

Please sign in to comment.