-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixes #981
- Loading branch information
Showing
3 changed files
with
119 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
implementation/src/main/java/io/smallrye/config/SmallRyeSubsetConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters