Skip to content

Commit

Permalink
Convert .env property names with Environment variable names rules (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jan 24, 2023
1 parent dd32f7e commit ac93c63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import io.smallrye.config.common.utils.ConfigSourceUtil;
import io.smallrye.config.common.utils.StringUtil;

public class DotEnvConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider {
private final String location;
Expand All @@ -28,7 +31,11 @@ protected String[] getFileExtensions() {

@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
return new EnvConfigSource(ConfigSourceUtil.urlToMap(url), ordinal) {
Map<String, String> envProperties = new HashMap<>();
for (final Map.Entry<String, String> entry : ConfigSourceUtil.urlToMap(url).entrySet()) {
envProperties.put(StringUtil.replaceNonAlphanumericByUnderscores(entry.getKey()), entry.getValue());
}
return new EnvConfigSource(envProperties, ordinal) {
@Override
public String getName() {
return super.getName() + "[source=" + url + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ void dotEnvSource(@TempDir Path tempDir) throws Exception {
}

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultSources()
.addDiscoveredSources()
.addDefaultInterceptors()
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.build();
Expand Down Expand Up @@ -63,8 +61,6 @@ void dotEnvSourceProfiles(@TempDir Path tempDir) throws Exception {
}

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultSources()
.addDiscoveredSources()
.addDefaultInterceptors()
.withProfile("common,dev")
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
Expand All @@ -77,4 +73,22 @@ void dotEnvSourceProfiles(@TempDir Path tempDir) throws Exception {
assertEquals("dev", config.getRawValue("my.prop.profile"));
assertEquals("dev", config.getRawValue("MY_PROP_PROFILE"));
}

@Test
void dotEnvSourceConvertNames(@TempDir Path tempDir) throws Exception {
Properties envProperties = new Properties();
envProperties.setProperty("MY-PROP", "1234");
envProperties.setProperty("FOO_BAR_BAZ", "1234");
try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env").toFile())) {
envProperties.store(out, null);
}

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.build();

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

0 comments on commit ac93c63

Please sign in to comment.