Skip to content

Commit

Permalink
#139 Add Configuration.Entry with info on the source of an entry
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-bygrave committed Apr 30, 2024
1 parent c1ed6e1 commit 55d5eac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
21 changes: 21 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public interface Configuration {
*/
Configuration forPath(String pathPrefix);

/**
* Return the entry for the given key.
*/
Optional<Entry> entry(String key);

/**
* Return a required configuration value as String.
* <p>
Expand Down Expand Up @@ -800,4 +805,20 @@ interface Builder {
*/
Configuration build();
}

/**
* A configuration entry.
*/
interface Entry {

/**
* Return the source of the entry.
*/
String source();

/**
* Return the String value of the entry.
*/
String value();
}
}
15 changes: 15 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ private String required(String key) {
return value;
}

@Override
public Optional<Configuration.Entry> entry(String key) {
return properties.optionalEntry(key);
}

@Override
public String get(String key) {
return required(key);
Expand Down Expand Up @@ -526,6 +531,16 @@ CoreEntry entry(String key, String defaultValue) {
return _entry(key, defaultValue);
}

/**
* Return the underlying entry for a given key WITHOUT creating it if it does not exist.
* This also excludes entries that represent a null value.
*/
Optional<Entry> optionalEntry(String key) {
return Optional.ofNullable(entries.get(key))
.filter(entry -> !entry.isNull())
.map(entry -> entry);
}

/**
* Get property with caching taking into account defaultValue and "null".
*/
Expand Down
8 changes: 5 additions & 3 deletions avaje-config/src/main/java/io/avaje/config/CoreEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Configuration entry.
*/
@NonNullApi
final class CoreEntry {
final class CoreEntry implements Configuration.Entry {

/**
* Entry used to represent no entry / null.
Expand Down Expand Up @@ -79,15 +79,17 @@ boolean needsEvaluation() {
return value != null && value.contains("${");
}

String value() {
@Override
public String value() {
return value;
}

boolean boolValue() {
return boolValue;
}

String source() {
@Override
public String source() {
return source;
}

Expand Down
3 changes: 3 additions & 0 deletions avaje-config/src/test/java/io/avaje/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void fallbackToSystemProperty_initial() {
void getNullable() {
assertThat(Config.getNullable("IDoNotExist0")).isNull();
assertThat(Config.getNullable("IDoNotExist0", System.getenv("AlsoDoNotExist"))).isNull();

var entry = Config.asConfiguration().entry("IDoNotExist0");
assertThat(entry).isEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

class CoreConfigurationTest {
Expand Down Expand Up @@ -58,6 +57,15 @@ void parser() {

assertThat(base.get("my.key.other")).isEqualTo("42");
assertThat(base.getLong("my.key.more.again")).isEqualTo(52);

var entry = base.entry("my.key.other");
assertThat(entry).isPresent().hasValueSatisfying( e -> assertThat(e.value()).isEqualTo("42"));

var entry2 = base.entry("my.key.more.again");
assertThat(entry2).isPresent().hasValueSatisfying( e -> assertThat(e.value()).isEqualTo("52"));

var entry3 = base.entry("my.key.DoesNotExist");
assertThat(entry3).isEmpty();
}

@Test
Expand All @@ -72,6 +80,9 @@ void asProperties() {
assertThat(loaded.get("a")).isEqualTo("1");
assertThat(loaded.get("SetViaSystemProperty")).isNull();

Optional<Configuration.Entry> entry = configuration.entry("SetViaSystemProperty");
assertThat(entry).isEmpty();

System.clearProperty("SetViaSystemProperty");
System.clearProperty("foo.bar");
}
Expand Down

0 comments on commit 55d5eac

Please sign in to comment.