getConfigSource(final String name) {
return Optional.empty();
}
+ /**
+ * Returns a {@link Config} containing every key from the current {@link Config} that starts with the specified
+ * prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following
+ * properties:
+ *
+ *
+ * prefix.number = 1
+ * prefix.string = Hello
+ * prefixed.foo = bar
+ * prefix = World
+ *
+ *
+ * the Configuration returned by {@code subset("prefix")} will contain the properties:
+ *
+ *
+ * number = 1
+ * string = Hello
+ * = World
+ *
+ *
+ * (The key for the value "World" is an empty string)
+ *
+ *
+ * @param prefix The prefix used to select the properties.
+ * @return a subset configuration
+ */
+ @Experimental("Return a subset of the configuration")
+ public Config subset(final String prefix) {
+ return new SmallRyeSubsetConfig(prefix, this);
+ }
+
public T convert(String value, Class asType) {
return value != null ? requireConverter(asType).convert(value) : null;
}
diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeSubsetConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeSubsetConfig.java
new file mode 100644
index 000000000..fab6cd585
--- /dev/null
+++ b/implementation/src/main/java/io/smallrye/config/SmallRyeSubsetConfig.java
@@ -0,0 +1,93 @@
+package io.smallrye.config;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigValue;
+import org.eclipse.microprofile.config.spi.ConfigSource;
+import org.eclipse.microprofile.config.spi.Converter;
+
+/**
+ * @author George Gastaldi
+ */
+class SmallRyeSubsetConfig implements Config {
+
+ private final String prefix;
+
+ private final Config delegate;
+
+ public SmallRyeSubsetConfig(String prefix, Config delegate) {
+ this.prefix = prefix;
+ this.delegate = delegate;
+ }
+
+ @Override
+ public T getValue(String propertyName, Class propertyType) {
+ return delegate.getValue(toSubsetPropertyName(propertyName), propertyType);
+ }
+
+ @Override
+ public ConfigValue getConfigValue(String propertyName) {
+ return delegate.getConfigValue(toSubsetPropertyName(propertyName));
+ }
+
+ @Override
+ public List getValues(String propertyName, Class propertyType) {
+ return delegate.getValues(toSubsetPropertyName(propertyName), propertyType);
+ }
+
+ @Override
+ public Optional getOptionalValue(String propertyName, Class propertyType) {
+ return delegate.getOptionalValue(toSubsetPropertyName(propertyName), propertyType);
+ }
+
+ @Override
+ public Optional> getOptionalValues(String propertyName, Class propertyType) {
+ return delegate.getOptionalValues(toSubsetPropertyName(propertyName), propertyType);
+ }
+
+ @Override
+ public Iterable getPropertyNames() {
+ return StreamSupport.stream(delegate.getPropertyNames().spliterator(), false)
+ .map(this::chopSubsetPropertyName)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet());
+ }
+
+ @Override
+ public Iterable getConfigSources() {
+ return delegate.getConfigSources();
+ }
+
+ @Override
+ public Optional> getConverter(Class forType) {
+ return delegate.getConverter(forType);
+ }
+
+ @Override
+ public T unwrap(Class type) {
+ throw new UnsupportedOperationException("Cannot unwrap a subset configuration");
+ }
+
+ private String toSubsetPropertyName(String propertyName) {
+ if (propertyName.isBlank()) {
+ return prefix;
+ } else {
+ return prefix + "." + propertyName;
+ }
+ }
+
+ private String chopSubsetPropertyName(String propertyName) {
+ if (propertyName.equalsIgnoreCase(prefix)) {
+ return "";
+ } else if (propertyName.startsWith(prefix + '.')) {
+ return propertyName.substring(prefix.length() + 1);
+ } else {
+ return null;
+ }
+ }
+}
diff --git a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java
index 9d9f4476d..2693a5373 100644
--- a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java
+++ b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java
@@ -6,6 +6,8 @@
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.StreamSupport.stream;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -405,4 +407,21 @@ void emptyPropertyNames() {
assertEquals("value", config.getRawValue(""));
}
+
+ @Test
+ void subset() {
+ SmallRyeConfig config = new SmallRyeConfigBuilder()
+ .withSources(config(
+ "app.foo", "bar",
+ "app.foo.user", "guest",
+ "app.foo.password", "apassword",
+ "app.fooed.user", "wrong"))
+ .build();
+ Config subset = config.subset("app.foo");
+ assertEquals("bar", subset.getValue("", String.class));
+ assertEquals("guest", subset.getValue("user", String.class));
+ assertEquals("apassword", subset.getValue("password", String.class));
+ assertThat(subset.getPropertyNames()).containsExactlyInAnyOrder("", "user", "password");
+ assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> subset.unwrap(SmallRyeConfig.class));
+ }
}