Skip to content

Commit

Permalink
Relocate property keys with multiple profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuel Alves <[email protected]>
  • Loading branch information
ejba committed Sep 15, 2023
1 parent e06b7b7 commit 88b8c82
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ private String normalizeName(final String name) {
}

public static List<String> convertProfile(final String profile) {
List<String> profiles = newCollectionConverter(newTrimmingConverter(STRING_CONVERTER), ArrayList::new).convert(profile);
final String value;
if (profile.charAt(0) == '"' && profile.charAt(profile.length() - 1) == '"') {
value = profile.substring(2, profile.length() - 1);
} else {
value = profile;
}
List<String> profiles = newCollectionConverter(newTrimmingConverter(STRING_CONVERTER), ArrayList::new).convert(value);
return profiles != null ? profiles : Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorConte
multipleProfileProperties
.add(new MultipleProfileProperty(name, name.substring(profileSegment.length()), profiles));
}
} else if (name.length() > 1 && name.charAt(0) == '"' && name.charAt(1) == '%') {
String segments = name.substring(name.indexOf('"', 1) + 1);
NameIterator ni = new NameIterator(name);
String profileSegment = ni.getNextSegment();
List<String> profiles = convertProfile(profileSegment.substring(1));

if (profiles.size() > 1) {
multipleProfileProperties
.add(new MultipleProfileProperty(name, segments, profiles));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ void multipleProfiles() {
assertEquals("5678", config.getRawValue("my.prop"));
}

@Test
void multipleProfilesWithDynamicKey() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_CONFIG_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 relocatePropertyKeysWithMultipleProfiles() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(SMALLRYE_CONFIG_PROFILE, "common", "config_ordinal", "1000"))
.withSources(config("\"%common,another\".my.prop", "1234"))
.addDefaultInterceptors()
.build();

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

@Test
void multipleProfilesDocs() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
Expand Down

0 comments on commit 88b8c82

Please sign in to comment.