Skip to content

Commit 58332c0

Browse files
committed
Introduce SmallRyeConfig.subset
- Fixes #981
1 parent 6bd3669 commit 58332c0

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

implementation/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<groupId>io.smallrye.testing</groupId>
8383
<artifactId>smallrye-testing-utilities</artifactId>
8484
</dependency>
85+
<dependency>
86+
<groupId>org.assertj</groupId>
87+
<artifactId>assertj-core</artifactId>
88+
<scope>test</scope>
89+
</dependency>
8590
</dependencies>
8691

8792
<build>

implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ public <K, V> Map<K, V> getValuesAsMap(String name, Converter<K> keyConverter, C
223223
}
224224

225225
/**
226-
*
227226
* This method handles calls from both {@link Config#getValue} and {@link Config#getOptionalValue}.<br>
228227
*/
229228
@SuppressWarnings("unchecked")
@@ -246,17 +245,17 @@ public <T> T getValue(String name, Converter<T> converter) {
246245
/**
247246
* This method handles converting values for both CDI injections and programatical calls.<br>
248247
* <br>
249-
*
248+
* <p>
250249
* Calls for converting non-optional values ({@link Config#getValue} and "Injecting Native Values")
251250
* should throw an {@link Exception} for each of the following:<br>
252-
*
251+
* <p>
253252
* 1. {@link IllegalArgumentException} - if the property cannot be converted by the {@link Converter} to the specified type
254253
* <br>
255254
* 2. {@link NoSuchElementException} - if the property is not defined <br>
256255
* 3. {@link NoSuchElementException} - if the property is defined as an empty string <br>
257256
* 4. {@link NoSuchElementException} - if the {@link Converter} returns {@code null} <br>
258257
* <br>
259-
*
258+
* <p>
260259
* Calls for converting optional values ({@link Config#getOptionalValue} and "Injecting Optional Values")
261260
* should only throw an {@link Exception} for #1 ({@link IllegalArgumentException} when the property cannot be converted to
262261
* the specified type).
@@ -459,6 +458,37 @@ public Optional<ConfigSource> getConfigSource(final String name) {
459458
return Optional.empty();
460459
}
461460

461+
/**
462+
* Return a {@link Config} containing every key from the current {@link Config} that starts with the specified
463+
* prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following
464+
* properties:
465+
*
466+
* <pre>
467+
* prefix.number = 1
468+
* prefix.string = Hello
469+
* prefixed.foo = bar
470+
* prefix = World
471+
* </pre>
472+
* <p>
473+
* the Configuration returned by {@code subset("prefix")} will contain the properties:
474+
*
475+
* <pre>
476+
* number = 1
477+
* string = Hello
478+
* = World
479+
* </pre>
480+
* <p>
481+
* (The key for the value "World" is an empty string)
482+
* <p>
483+
*
484+
* @param prefix The prefix used to select the properties.
485+
* @return a subset configuration
486+
*/
487+
@Experimental("Return a subset of the configuration")
488+
public Config subset(final String prefix) {
489+
return new SmallRyeSubsetConfig(prefix, this);
490+
}
491+
462492
public <T> T convert(String value, Class<T> asType) {
463493
return value != null ? requireConverter(asType).convert(value) : null;
464494
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package io.smallrye.config;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.StreamSupport;
7+
8+
import org.eclipse.microprofile.config.Config;
9+
import org.eclipse.microprofile.config.ConfigValue;
10+
import org.eclipse.microprofile.config.spi.ConfigSource;
11+
import org.eclipse.microprofile.config.spi.Converter;
12+
13+
/**
14+
* @author George Gastaldi
15+
*/
16+
class SmallRyeSubsetConfig implements Config {
17+
18+
private final String prefix;
19+
20+
private final Config delegate;
21+
22+
public SmallRyeSubsetConfig(String prefix, Config delegate) {
23+
this.prefix = prefix;
24+
this.delegate = delegate;
25+
}
26+
27+
@Override
28+
public <T> T getValue(String propertyName, Class<T> propertyType) {
29+
return delegate.getValue(toSubsetPropertyName(propertyName), propertyType);
30+
}
31+
32+
@Override
33+
public ConfigValue getConfigValue(String propertyName) {
34+
return delegate.getConfigValue(toSubsetPropertyName(propertyName));
35+
}
36+
37+
@Override
38+
public <T> List<T> getValues(String propertyName, Class<T> propertyType) {
39+
return delegate.getValues(toSubsetPropertyName(propertyName), propertyType);
40+
}
41+
42+
@Override
43+
public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) {
44+
return delegate.getOptionalValue(toSubsetPropertyName(propertyName), propertyType);
45+
}
46+
47+
@Override
48+
public <T> Optional<List<T>> getOptionalValues(String propertyName, Class<T> propertyType) {
49+
return delegate.getOptionalValues(toSubsetPropertyName(propertyName), propertyType);
50+
}
51+
52+
@Override
53+
public Iterable<String> getPropertyNames() {
54+
return StreamSupport.stream(delegate.getPropertyNames().spliterator(), false)
55+
.map(this::chopSubsetPropertyName)
56+
.collect(Collectors.toSet());
57+
}
58+
59+
@Override
60+
public Iterable<ConfigSource> getConfigSources() {
61+
return delegate.getConfigSources();
62+
}
63+
64+
@Override
65+
public <T> Optional<Converter<T>> getConverter(Class<T> forType) {
66+
return delegate.getConverter(forType);
67+
}
68+
69+
@Override
70+
public <T> T unwrap(Class<T> type) {
71+
return delegate.unwrap(type);
72+
}
73+
74+
private String toSubsetPropertyName(String propertyName) {
75+
if (propertyName.isBlank()) {
76+
return prefix;
77+
} else {
78+
return prefix + "." + propertyName;
79+
}
80+
}
81+
82+
private String chopSubsetPropertyName(String propertyName) {
83+
if (propertyName.equalsIgnoreCase(prefix)) {
84+
return "";
85+
} else if (propertyName.startsWith(prefix)) {
86+
return propertyName.substring(prefix.length() + 1);
87+
} else {
88+
return propertyName;
89+
}
90+
}
91+
}

implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static java.util.Collections.singletonMap;
77
import static java.util.stream.Collectors.toSet;
88
import static java.util.stream.StreamSupport.stream;
9+
import static org.assertj.core.api.Assertions.assertThat;
910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011
import static org.junit.jupiter.api.Assertions.assertFalse;
1112
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -405,4 +406,19 @@ void emptyPropertyNames() {
405406

406407
assertEquals("value", config.getRawValue(""));
407408
}
409+
410+
@Test
411+
void subset() {
412+
SmallRyeConfig config = new SmallRyeConfigBuilder()
413+
.withSources(config(
414+
"app.foo", "bar",
415+
"app.foo.user", "guest",
416+
"app.foo.password", "apassword"))
417+
.build();
418+
Config subset = config.subset("app.foo");
419+
assertEquals("bar", subset.getValue("", String.class));
420+
assertEquals("guest", subset.getValue("user", String.class));
421+
assertEquals("apassword", subset.getValue("password", String.class));
422+
assertThat(subset.getPropertyNames()).containsExactlyInAnyOrder("", "user", "password");
423+
}
408424
}

0 commit comments

Comments
 (0)