Skip to content

Commit 17af3a9

Browse files
#139 Add Configuration.Entry with info on the source of an entry (#140)
* #139 Add Configuration.Entry with info on the source of an entry * #139 Add missing test asserts for Configuration.Entry source * #139 Add test assert for Configuration.Entry source of "SystemProperty" --------- Co-authored-by: Rob Bygrave <[email protected]>
1 parent c1ed6e1 commit 17af3a9

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

avaje-config/src/main/java/io/avaje/config/Configuration.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public interface Configuration {
7373
*/
7474
Configuration forPath(String pathPrefix);
7575

76+
/**
77+
* Return the entry for the given key.
78+
*/
79+
Optional<Entry> entry(String key);
80+
7681
/**
7782
* Return a required configuration value as String.
7883
* <p>
@@ -800,4 +805,20 @@ interface Builder {
800805
*/
801806
Configuration build();
802807
}
808+
809+
/**
810+
* A configuration entry.
811+
*/
812+
interface Entry {
813+
814+
/**
815+
* Return the source of the entry.
816+
*/
817+
String source();
818+
819+
/**
820+
* Return the String value of the entry.
821+
*/
822+
String value();
823+
}
803824
}

avaje-config/src/main/java/io/avaje/config/CoreConfiguration.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ private String required(String key) {
242242
return value;
243243
}
244244

245+
@Override
246+
public Optional<Configuration.Entry> entry(String key) {
247+
return properties.optionalEntry(key);
248+
}
249+
245250
@Override
246251
public String get(String key) {
247252
return required(key);
@@ -526,6 +531,16 @@ CoreEntry entry(String key, String defaultValue) {
526531
return _entry(key, defaultValue);
527532
}
528533

534+
/**
535+
* Return the underlying entry for a given key WITHOUT creating it if it does not exist.
536+
* This also excludes entries that represent a null value.
537+
*/
538+
Optional<Entry> optionalEntry(String key) {
539+
return Optional.ofNullable(entries.get(key))
540+
.filter(entry -> !entry.isNull())
541+
.map(entry -> entry);
542+
}
543+
529544
/**
530545
* Get property with caching taking into account defaultValue and "null".
531546
*/

avaje-config/src/main/java/io/avaje/config/CoreEntry.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Configuration entry.
1414
*/
1515
@NonNullApi
16-
final class CoreEntry {
16+
final class CoreEntry implements Configuration.Entry {
1717

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

82-
String value() {
82+
@Override
83+
public String value() {
8384
return value;
8485
}
8586

8687
boolean boolValue() {
8788
return boolValue;
8889
}
8990

90-
String source() {
91+
@Override
92+
public String source() {
9193
return source;
9294
}
9395

avaje-config/src/test/java/io/avaje/config/ConfigTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ void fallbackToSystemProperty_initial() {
2121
System.setProperty("MySystemProp0", "bar");
2222
assertThat(Config.get("MySystemProp0", "foo")).isEqualTo("bar");
2323

24+
var entry = Config.asConfiguration().entry("MySystemProp0");
25+
assertThat(entry).isPresent().get().satisfies(e -> {
26+
assertThat(e.source()).isEqualTo("SystemProperty");
27+
assertThat(e.value()).isEqualTo("bar");
28+
});
29+
2430
// cached the initial value, still bar even when system property changed
2531
System.setProperty("MySystemProp0", "bazz");
2632
assertThat(Config.get("MySystemProp0")).isEqualTo("bar");
@@ -37,6 +43,9 @@ void fallbackToSystemProperty_initial() {
3743
void getNullable() {
3844
assertThat(Config.getNullable("IDoNotExist0")).isNull();
3945
assertThat(Config.getNullable("IDoNotExist0", System.getenv("AlsoDoNotExist"))).isNull();
46+
47+
var entry = Config.asConfiguration().entry("IDoNotExist0");
48+
assertThat(entry).isEmpty();
4049
}
4150

4251
@Test
@@ -115,6 +124,12 @@ void asProperties() {
115124
assertThat(properties.getProperty("myapp.fooName")).isEqualTo("Hello");
116125
assertThat(properties.getProperty("myapp.fooHome")).isEqualTo(home + "/config");
117126

127+
var entry = Config.asConfiguration().entry("myapp.fooName");
128+
assertThat(entry).isPresent().get().satisfies(e -> {
129+
assertThat(e.source()).isEqualTo("resource:application-test.yaml");
130+
assertThat(e.value()).isEqualTo("Hello");
131+
});
132+
118133
assertThat(Config.get("myExternalLoader")).isEqualTo("wasExecuted");
119134

120135
assertThat(properties.getProperty("config.load.systemProperties")).isEqualTo("true");

avaje-config/src/test/java/io/avaje/config/CoreConfigurationTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import java.util.concurrent.atomic.AtomicLong;
1313
import java.util.concurrent.atomic.AtomicReference;
1414

15-
import static org.assertj.core.api.Assertions.assertThat;
16-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
15+
import static org.assertj.core.api.Assertions.*;
1716
import static org.junit.jupiter.api.Assertions.*;
1817

1918
class CoreConfigurationTest {
@@ -58,6 +57,21 @@ void parser() {
5857

5958
assertThat(base.get("my.key.other")).isEqualTo("42");
6059
assertThat(base.getLong("my.key.more.again")).isEqualTo(52);
60+
61+
var entry = base.entry("my.key.other");
62+
assertThat(entry).isPresent().hasValueSatisfying( e -> {
63+
assertThat(e.source()).isEqualTo("event:PutAll");
64+
assertThat(e.value()).isEqualTo("42");
65+
});
66+
67+
var entry2 = base.entry("my.key.more.again");
68+
assertThat(entry2).isPresent().hasValueSatisfying( e -> {
69+
assertThat(e.source()).isEqualTo("event:PutAll");
70+
assertThat(e.value()).isEqualTo("52");
71+
});
72+
73+
var entry3 = base.entry("my.key.DoesNotExist");
74+
assertThat(entry3).isEmpty();
6175
}
6276

6377
@Test
@@ -72,6 +86,9 @@ void asProperties() {
7286
assertThat(loaded.get("a")).isEqualTo("1");
7387
assertThat(loaded.get("SetViaSystemProperty")).isNull();
7488

89+
Optional<Configuration.Entry> entry = configuration.entry("SetViaSystemProperty");
90+
assertThat(entry).isEmpty();
91+
7592
System.clearProperty("SetViaSystemProperty");
7693
System.clearProperty("foo.bar");
7794
}

0 commit comments

Comments
 (0)