Skip to content

Commit

Permalink
Match and replace semantic property names in the EnvConfigSource (#1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Sep 29, 2023
1 parent 1810643 commit 7624248
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static io.smallrye.config.ConfigMappingLoader.getConfigMapping;
import static io.smallrye.config.ConfigMappingLoader.getConfigMappingClass;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN;
import static io.smallrye.config.common.utils.StringUtil.replaceNonAlphanumericByUnderscores;
import static io.smallrye.config.common.utils.StringUtil.toLowerCaseAndDotted;
import static java.lang.Integer.parseInt;

import java.io.Serializable;
Expand Down Expand Up @@ -1094,8 +1096,11 @@ private static void matchPropertiesWithEnv(final SmallRyeConfig config, final Se
for (String configuredProperty : configuredProperties) {
Set<String> envNames = envConfigSource.getPropertyNames();
if (envConfigSource.hasPropertyName(configuredProperty)) {
envNames.remove(configuredProperty);
envNames.add(configuredProperty);
if (!envNames.contains(configuredProperty)) {
// this may be expensive, but it shouldn't happend that often
envNames.remove(toLowerCaseAndDotted(replaceNonAlphanumericByUnderscores(configuredProperty)));
envNames.add(configuredProperty);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.smallrye.config.Converters.STRING_CONVERTER;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.StreamSupport.stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -29,7 +30,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.stream.StreamSupport;
import java.util.Optional;
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -88,12 +90,10 @@ void empty() {
assertTrue(
stream(config.getPropertyNames().spliterator(), false).collect(toList()).contains("SMALLRYE_MP_CONFIG_EMPTY"));

ConfigSource envConfigSource = StreamSupport.stream(config.getConfigSources().spliterator(), false)
.filter(configSource -> configSource.getName().equals("EnvConfigSource"))
.findFirst()
.get();
Optional<ConfigSource> envConfigSource = config.getConfigSource("EnvConfigSource");

assertEquals("", envConfigSource.getValue("SMALLRYE_MP_CONFIG_EMPTY"));
assertTrue(envConfigSource.isPresent());
assertEquals("", envConfigSource.get().getValue("SMALLRYE_MP_CONFIG_EMPTY"));
}

@Test
Expand All @@ -107,7 +107,7 @@ void ordinal() {

@Test
void indexed() {
Map<String, String> env = new HashMap<String, String>() {
Map<String, String> env = new HashMap<>() {
{
put("INDEXED_0_", "foo");
put("INDEXED_0__PROP", "bar");
Expand Down Expand Up @@ -142,7 +142,7 @@ void numbers() {

@Test
void map() {
Map<String, String> env = new HashMap<String, String>() {
Map<String, String> env = new HashMap<>() {
{
put("TEST_LANGUAGE__DE_ETR__", "Einfache Sprache");
put("TEST_LANGUAGE_PT_BR", "FROM ENV");
Expand Down Expand Up @@ -217,4 +217,19 @@ void envEquals() {
assertEquals(new EnvProperty("TEST_LANGUAGE__DE_ETR_").hashCode(),
new EnvProperty("test.language.\"de.etr\"").hashCode());
}

@Test
void sameSemanticMeaning() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config("foo.bar-baz", "fromOther"))
.withSources(new EnvConfigSource(Map.of("FOO_BAR_BAZ", "fromEnv"), 300))
.build();

Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet());
assertTrue(properties.contains("FOO_BAR_BAZ"));
assertTrue(properties.contains("foo.bar-baz"));
assertFalse(properties.contains("foo.bar.baz"));

assertEquals("fromEnv", config.getRawValue("foo.bar-baz"));
}
}

0 comments on commit 7624248

Please sign in to comment.