diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java index 697406470..d6018203c 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java @@ -596,11 +596,15 @@ public static Builder builder() { return new Builder(); } - public KeyMap getDefaultValues() { + KeyMap 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<>()); } @@ -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) { diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappings.java b/implementation/src/main/java/io/smallrye/config/ConfigMappings.java index c25103d9d..42920f7db 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappings.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappings.java @@ -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, Map> mappings; - public ConfigMappings() { + ConfigMappings() { this(new HashMap<>()); } @@ -22,11 +22,7 @@ void registerConfigMappings(Map, Map> mapp this.mappings.putAll(mappings); } - public Map, Map> getMappings() { - return mappings; - } - - public T getConfigMapping(Class type, String prefix) { + T getConfigMapping(Class type, String prefix) { final ConfigMappingObject configMappingObject = mappings.getOrDefault(type, Collections.emptyMap()).get(prefix); if (configMappingObject == null) { throw ConfigMessages.msg.mappingNotFound(type.getName(), prefix); diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index 43ce67bbd..eeca7c1b8 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -56,11 +56,12 @@ public class SmallRyeConfig implements Config, Serializable { private final Map> converters; private final Map>> 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 @@ -69,6 +70,7 @@ protected SmallRyeConfig(List configSources, Map(Converters.ALL_CONVERTERS); this.converters.putAll(converters); + this.mappings = new ConfigMappings(); } private List buildConfigSources(final SmallRyeConfigBuilder builder) { @@ -202,11 +204,6 @@ public > Optional getOptionalValues(String name, C return getOptionalValue(name, Converters.newCollectionConverter(converter, collectionFactory)); } - @Experimental("TODO") - void registerMappings(Map, Map> mappings) { - this.mappings.registerConfigMappings(mappings); - } - @Experimental("TODO") public T getConfigMapping(Class type) { return getConfigMapping(type, ""); diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java index 25d94a703..e14c9b49e 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java @@ -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 mappingProviderDefaultValues = mappingProvider.getDefaultValues(); defaultValues.forEach((key, value) -> mappingProviderDefaultValues.findOrAdd(key).putRootValue(value)); @@ -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);