Skip to content

Commit

Permalink
Improve SmallRyeConfig and ConfigMappings integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 31, 2020
1 parent 547ecea commit 942a132
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,15 @@ public static Builder builder() {
return new Builder();
}

public KeyMap<String> getDefaultValues() {
KeyMap<String> getDefaultValues() {
return defaultValues;
}

public ConfigMappings mapConfiguration(SmallRyeConfig config) throws ConfigValidationException {
ConfigMappings mapConfiguration(SmallRyeConfig config) throws ConfigValidationException {
return mapConfiguration(config, new ConfigMappings());
}

ConfigMappings mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) throws ConfigValidationException {
if (roots.isEmpty()) {
return new ConfigMappings(new HashMap<>());
}
Expand Down Expand Up @@ -640,7 +644,9 @@ public ConfigMappings mapConfiguration(SmallRyeConfig config) throws ConfigValid
problems.toArray(ConfigValidationException.Problem.NO_PROBLEMS));
}
context.fillInOptionals();
return new ConfigMappings(context.getRootsMap());

mappings.registerConfigMappings(context.getRootsMap());
return mappings;
}

private boolean isPropertyInRoot(String propertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.util.HashMap;
import java.util.Map;

public final class ConfigMappings implements Serializable {
final class ConfigMappings implements Serializable {
private static final long serialVersionUID = -7790784345796818526L;

private final Map<Class<?>, Map<String, ConfigMappingObject>> mappings;

public ConfigMappings() {
ConfigMappings() {
this(new HashMap<>());
}

Expand All @@ -22,11 +22,7 @@ void registerConfigMappings(Map<Class<?>, Map<String, ConfigMappingObject>> mapp
this.mappings.putAll(mappings);
}

public Map<Class<?>, Map<String, ConfigMappingObject>> getMappings() {
return mappings;
}

public <T> T getConfigMapping(Class<T> type, String prefix) {
<T> T getConfigMapping(Class<T> type, String prefix) {
final ConfigMappingObject configMappingObject = mappings.getOrDefault(type, Collections.emptyMap()).get(prefix);
if (configMappingObject == null) {
throw ConfigMessages.msg.mappingNotFound(type.getName(), prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ public class SmallRyeConfig implements Config, Serializable {
private final Map<Type, Converter<?>> converters;
private final Map<Type, Converter<Optional<?>>> optionalConverters = new ConcurrentHashMap<>();

private final ConfigMappings mappings = new ConfigMappings();
private final ConfigMappings mappings;

SmallRyeConfig(SmallRyeConfigBuilder builder) {
SmallRyeConfig(SmallRyeConfigBuilder builder, ConfigMappings mappings) {
this.configSources = new AtomicReference<>(new ConfigSources(buildConfigSources(builder), buildInterceptors(builder)));
this.converters = buildConverters(builder);
this.mappings = mappings;
}

@Deprecated
Expand All @@ -69,6 +70,7 @@ protected SmallRyeConfig(List<ConfigSource> configSources, Map<Type, Converter<?
new ConfigSources(configSources, buildInterceptors(new SmallRyeConfigBuilder())));
this.converters = new ConcurrentHashMap<>(Converters.ALL_CONVERTERS);
this.converters.putAll(converters);
this.mappings = new ConfigMappings();
}

private List<ConfigSource> buildConfigSources(final SmallRyeConfigBuilder builder) {
Expand Down Expand Up @@ -202,11 +204,6 @@ public <T, C extends Collection<T>> Optional<C> getOptionalValues(String name, C
return getOptionalValue(name, Converters.newCollectionConverter(converter, collectionFactory));
}

@Experimental("TODO")
void registerMappings(Map<Class<?>, Map<String, ConfigMappingObject>> mappings) {
this.mappings.registerConfigMappings(mappings);
}

@Experimental("TODO")
public <T> T getConfigMapping(Class<T> type) {
return getConfigMapping(type, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ boolean isAddDiscoveredInterceptors() {

@Override
public SmallRyeConfig build() {
final ConfigMappingProvider mappingProvider = mappingsBuilder.build();
ConfigMappingProvider mappingProvider = mappingsBuilder.build();
if (!defaultValues.isEmpty() || !mappingProvider.getDefaultValues().isEmpty()) {
final KeyMap<String> mappingProviderDefaultValues = mappingProvider.getDefaultValues();
defaultValues.forEach((key, value) -> mappingProviderDefaultValues.findOrAdd(key).putRootValue(value));
Expand All @@ -348,11 +348,10 @@ public int getOrdinal() {
});
}

final SmallRyeConfig config = new SmallRyeConfig(this);

try {
final ConfigMappings result = mappingProvider.mapConfiguration(config);
config.registerMappings(result.getMappings());
ConfigMappings configMappings = new ConfigMappings();
SmallRyeConfig config = new SmallRyeConfig(this, configMappings);
mappingProvider.mapConfiguration(config, configMappings);
return config;
} catch (ConfigValidationException e) {
throw new IllegalStateException(e);
Expand Down

0 comments on commit 942a132

Please sign in to comment.