Skip to content

Commit

Permalink
Introduce SmallRyeConfig.subset
Browse files Browse the repository at this point in the history
- Fixes #981
  • Loading branch information
gastaldi committed Aug 24, 2023
1 parent 6bd3669 commit 71a57e5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public <K, V> Map<K, V> getValuesAsMap(String name, Converter<K> keyConverter, C
}

/**
*
* This method handles calls from both {@link Config#getValue} and {@link Config#getOptionalValue}.<br>
*/
@SuppressWarnings("unchecked")
Expand All @@ -246,17 +245,17 @@ public <T> T getValue(String name, Converter<T> converter) {
/**
* This method handles converting values for both CDI injections and programatical calls.<br>
* <br>
*
* <p>
* Calls for converting non-optional values ({@link Config#getValue} and "Injecting Native Values")
* should throw an {@link Exception} for each of the following:<br>
*
* <p>
* 1. {@link IllegalArgumentException} - if the property cannot be converted by the {@link Converter} to the specified type
* <br>
* 2. {@link NoSuchElementException} - if the property is not defined <br>
* 3. {@link NoSuchElementException} - if the property is defined as an empty string <br>
* 4. {@link NoSuchElementException} - if the {@link Converter} returns {@code null} <br>
* <br>
*
* <p>
* Calls for converting optional values ({@link Config#getOptionalValue} and "Injecting Optional Values")
* should only throw an {@link Exception} for #1 ({@link IllegalArgumentException} when the property cannot be converted to
* the specified type).
Expand Down Expand Up @@ -459,6 +458,11 @@ public Optional<ConfigSource> getConfigSource(final String name) {
return Optional.empty();
}

@Experimental("Return a subset of the configuration")
public Config subset(final String prefix) {
return new SmallRyeSubsetConfig(prefix, this);
}

public <T> T convert(String value, Class<T> asType) {
return value != null ? requireConverter(asType).convert(value) : null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.smallrye.config;

import java.util.List;
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
*/
public 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> T getValue(String propertyName, Class<T> propertyType) {
return delegate.getValue(toSubsetPropertyName(propertyName), propertyType);
}

@Override
public ConfigValue getConfigValue(String propertyName) {
return delegate.getConfigValue(toSubsetPropertyName(propertyName));
}

@Override
public <T> List<T> getValues(String propertyName, Class<T> propertyType) {
return delegate.getValues(toSubsetPropertyName(propertyName), propertyType);
}

@Override
public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) {
return delegate.getOptionalValue(toSubsetPropertyName(propertyName), propertyType);
}

@Override
public <T> Optional<List<T>> getOptionalValues(String propertyName, Class<T> propertyType) {
return delegate.getOptionalValues(toSubsetPropertyName(propertyName), propertyType);
}

@Override
public Iterable<String> getPropertyNames() {
return StreamSupport.stream(delegate.getPropertyNames().spliterator(), false)
.filter(propertyName -> propertyName.startsWith(prefix))
.map(this::chopSubsetPropertyName)
.collect(Collectors.toSet());
}

@Override
public Iterable<ConfigSource> getConfigSources() {
return delegate.getConfigSources();
}

@Override
public <T> Optional<Converter<T>> getConverter(Class<T> forType) {
return delegate.getConverter(forType);
}

@Override
public <T> T unwrap(Class<T> type) {
return delegate.unwrap(type);
}

private String toSubsetPropertyName(String propertyName) {
if (propertyName.isBlank()) {
return prefix;
}
return prefix + "." + propertyName;
}

private String chopSubsetPropertyName(String propertyName) {
return propertyName.substring(prefix.length() + 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package io.smallrye.config;

import static io.smallrye.config.Converters.STRING_CONVERTER;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.StreamSupport.stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.smallrye.config.common.AbstractConfigSource;
import io.smallrye.config.common.MapBackedConfigSource;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -23,12 +16,13 @@
import java.util.Optional;
import java.util.Set;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.Test;

import io.smallrye.config.common.AbstractConfigSource;
import io.smallrye.config.common.MapBackedConfigSource;
import static io.smallrye.config.Converters.STRING_CONVERTER;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.StreamSupport.stream;
import static org.junit.jupiter.api.Assertions.*;

class SmallRyeConfigTest {
@Test
Expand Down Expand Up @@ -405,4 +399,18 @@ 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"))
.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));
}
}

0 comments on commit 71a57e5

Please # to comment.