Skip to content

Commit

Permalink
#141 Change CoreConfigurationBuilder to set appropriate source for fi…
Browse files Browse the repository at this point in the history
…les and resources
  • Loading branch information
rbygrave committed May 6, 2024
1 parent b237077 commit 7d655fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
Expand All @@ -18,7 +17,7 @@
final class CoreConfigurationBuilder implements Configuration.Builder {

private final Parsers parsers = new Parsers();
private final Map<String, String> sourceMap = new LinkedHashMap<>();
private final CoreEntry.CoreMap sourceMap = CoreEntry.newMap();
private ResourceLoader resourceLoader = initialiseResourceLoader();
private ModificationEventRunner eventRunner;
private ConfigurationLog configurationLog;
Expand Down Expand Up @@ -47,7 +46,7 @@ public Configuration.Builder resourceLoader(ResourceLoader resourceLoader) {
public Configuration.Builder put(String key, String value) {
requireNonNull(key);
requireNonNull(value);
sourceMap.put(key, value);
sourceMap.put(key, value, "initial");
return this;
}

Expand All @@ -56,7 +55,7 @@ public Configuration.Builder putAll(Map<String, ?> source) {
requireNonNull(source);
source.forEach((key, value) -> {
if (key != null && value != null) {
put(key, value.toString());
sourceMap.put(key, value.toString(), "initial");
}
});
return this;
Expand All @@ -67,7 +66,7 @@ public Configuration.Builder putAll(Properties source) {
requireNonNull(source);
source.forEach((key, value) -> {
if (key != null && value != null) {
put(key.toString(), value.toString());
sourceMap.put(key.toString(), value.toString(), "initial");
}
});
return this;
Expand All @@ -79,7 +78,8 @@ public Configuration.Builder load(String resource) {
try {
try (var inputStream = resourceLoader.getResourceAsStream(resource)) {
if (inputStream != null) {
putAll(configParser.load(inputStream));
var source = "resource:" + resource;
configParser.load(inputStream).forEach((k, v) -> sourceMap.put(k, v, source));
}
return this;
}
Expand All @@ -96,7 +96,8 @@ public Configuration.Builder load(File file) {
final var configParser = parser(file.getName());
try {
try (var reader = new FileReader(file)) {
putAll(configParser.load(reader));
var source = "file:" + file.getName();
configParser.load(reader).forEach((k, v) -> sourceMap.put(k, v, source));
return this;
}
} catch (IOException e) {
Expand Down Expand Up @@ -144,7 +145,7 @@ public Configuration build() {

private CoreEntry.CoreMap initEntries() {
final var entries = initEntryMap();
sourceMap.forEach((key, value) -> entries.put(key, value, "initial"));
entries.addAll(sourceMap);
return CoreExpressionEval.evalFor(entries);
}

Expand Down
7 changes: 7 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/CoreEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ static class CoreMap {
});
}

/**
* Add all the entries from another source.
*/
void addAll(CoreMap source) {
entryMap.putAll(source.entryMap);
}

int size() {
return entryMap.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ void builder() {
assertEquals(conf.get("myExtraOne"), "baz");
assertEquals(conf.get("my.name"), "Nom");
assertEquals(conf.get("hi.iAmInProps"), "There it is");
var entry = conf.entry("hi.iAmInProps");
assertThat(entry).isPresent().get().satisfies(e -> {
assertThat(e.source()).isEqualTo("resource:hi.properties");
assertThat(e.value()).isEqualTo("There it is");
});
var entryFile = conf.entry("my.name");
assertThat(entryFile).isPresent().get().satisfies(e -> {
assertThat(e.source()).isEqualTo("file:minimal.yaml");
assertThat(e.value()).isEqualTo("Nom");
});

String userHome = System.getProperty("user.home");
assertEquals(conf.get("myHome"), "my/" + userHome + "/home");
Expand Down

0 comments on commit 7d655fe

Please sign in to comment.