Skip to content

Commit

Permalink
Merge branch 'smallrye:main' into unwrap-not-cast
Browse files Browse the repository at this point in the history
  • Loading branch information
craig-day authored Jan 16, 2023
2 parents 14f2d01 + 08cfa57 commit 62a3cea
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.smallrye.config.ConfigMappingInterface.rawTypeOf;
import static io.smallrye.config.ConfigMappingInterface.typeOfParameter;
import static io.smallrye.config.ConfigValidationException.Problem;
import static io.smallrye.config.common.utils.StringUtil.replaceNonAlphanumericByUnderscores;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
Expand Down Expand Up @@ -41,7 +42,8 @@ public final class ConfigMappingContext {
private final List<ConfigMappingObject> allInstances = new ArrayList<>();
private final SmallRyeConfig config;
private final StringBuilder stringBuilder = new StringBuilder();
private final ArrayList<Problem> problems = new ArrayList<>();
private final Set<String> unknownProperties = new HashSet<>();
private final List<Problem> problems = new ArrayList<>();

private NamingStrategy namingStrategy = null;

Expand Down Expand Up @@ -222,8 +224,34 @@ public NoSuchElementException noSuchElement(Class<?> type) {
return new NoSuchElementException("A required configuration group of type " + type.getName() + " was not provided");
}

public void unknownConfigElement(final String propertyName) {
problems.add(new Problem(propertyName + " does not map to any root"));
void unknownProperty(final String unknownProperty) {
unknownProperties.add(unknownProperty);
}

void validateUnknown(final boolean validateUnknown) {
Set<String> usedProperties = new HashSet<>();
for (String property : config.getPropertyNames()) {
if (unknownProperties.contains(property)) {
continue;
}

usedProperties.add(replaceNonAlphanumericByUnderscores(property));
}
usedProperties.removeAll(unknownProperties);

for (String property : unknownProperties) {
boolean found = false;
String envProperty = replaceNonAlphanumericByUnderscores(property);
for (String usedProperty : usedProperties) {
if (usedProperty.equalsIgnoreCase(envProperty)) {
found = true;
break;
}
}
if (!found && validateUnknown) {
problems.add(new Problem(property + " does not map to any root"));
}
}
}

void fillInOptionals() {
Expand All @@ -244,7 +272,7 @@ public void reportProblem(RuntimeException problem) {
problems.add(new Problem(problem.toString()));
}

ArrayList<Problem> getProblems() {
List<Problem> getProblems() {
return problems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,29 +925,26 @@ Map<String, Property> getProperties() {
return properties;
}

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

void mapConfiguration(SmallRyeConfig config) throws ConfigValidationException {
ConfigMappingContext mapConfiguration(SmallRyeConfig config) throws ConfigValidationException {
for (ConfigSource configSource : config.getConfigSources()) {
if (configSource instanceof DefaultValuesConfigSource) {
final DefaultValuesConfigSource defaultValuesConfigSource = (DefaultValuesConfigSource) configSource;
defaultValuesConfigSource.registerDefaults(this.getDefaultValues());
DefaultValuesConfigSource defaultValuesConfigSource = (DefaultValuesConfigSource) configSource;
defaultValuesConfigSource.registerDefaults(defaultValues);
}
}

config.addPropertyNames(additionalMappedProperties(new HashSet<>(getProperties().keySet()), config));
SecretKeys.doUnlocked(() -> mapConfiguration(config, config.getConfigMappings()));
return SecretKeys.doUnlocked(() -> mapConfigurationInternal(config));
}

private void mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) throws ConfigValidationException {
private ConfigMappingContext mapConfigurationInternal(SmallRyeConfig config) throws ConfigValidationException {
Assert.checkNotNullParam("config", config);
ConfigMappingContext context = new ConfigMappingContext(config);

if (roots.isEmpty()) {
return;
return context;
}

Assert.checkNotNullParam("config", config);
ConfigMappingContext context = new ConfigMappingContext(config);
// eagerly populate roots
for (Map.Entry<String, List<Class<?>>> entry : roots.entrySet()) {
String path = entry.getKey();
Expand All @@ -960,11 +957,7 @@ private void mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) th
}
}

boolean validateUnknown = config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, boolean.class)
.orElse(this.validateUnknown);

// lazily sweep
Set<String> unknownProperties = new HashSet<>();
for (String name : config.getPropertyNames()) {
NameIterator ni = new NameIterator(name);
// filter properties in root
Expand All @@ -976,20 +969,20 @@ private void mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) th
if (action != null) {
action.accept(context, ni);
} else {
if (validateUnknown) {
unknownProperties.add(name);
}
context.unknownProperty(name);
}
}

unknownProperties(unknownProperties, context);
ArrayList<ConfigValidationException.Problem> problems = context.getProblems();
context.validateUnknown(
config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, boolean.class).orElse(this.validateUnknown));

List<ConfigValidationException.Problem> problems = context.getProblems();
if (!problems.isEmpty()) {
throw new ConfigValidationException(problems.toArray(ConfigValidationException.Problem.NO_PROBLEMS));
}
context.fillInOptionals();

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

private boolean isPropertyInRoot(NameIterator propertyName) {
Expand Down Expand Up @@ -1150,32 +1143,6 @@ private static boolean isIndexed(final String propertyName) {
return false;
}

private static void unknownProperties(Set<String> properties, ConfigMappingContext context) {
Set<String> usedProperties = new HashSet<>();
for (String property : context.getConfig().getPropertyNames()) {
if (properties.contains(property)) {
continue;
}

usedProperties.add(replaceNonAlphanumericByUnderscores(property));
}
usedProperties.removeAll(properties);

for (String property : properties) {
boolean found = false;
String envProperty = replaceNonAlphanumericByUnderscores(property);
for (String usedProperty : usedProperties) {
if (usedProperty.equalsIgnoreCase(envProperty)) {
found = true;
break;
}
}
if (!found) {
context.unknownConfigElement(property);
}
}
}

public static final class Builder {
final Set<Class<?>> types = new HashSet<>();
final Map<String, List<Class<?>>> roots = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,19 @@ public final class ConfigMappings implements Serializable {
this.mappings = new ConcurrentHashMap<>();
}

void registerConfigMappings(final Map<Class<?>, Map<String, ConfigMappingObject>> mappings) {
this.mappings.putAll(mappings);
}

public static void registerConfigMappings(final SmallRyeConfig config, final Set<ConfigClassWithPrefix> configClasses)
throws ConfigValidationException {
if (!configClasses.isEmpty()) {
Boolean validateUnknown = config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, Boolean.class)
.orElse(TRUE);
mapConfiguration(ConfigMappingProvider.builder().validateUnknown(validateUnknown), config, configClasses);
mapConfiguration(config, ConfigMappingProvider.builder().validateUnknown(validateUnknown), configClasses);
}
}

public static void registerConfigProperties(final SmallRyeConfig config, final Set<ConfigClassWithPrefix> configClasses)
throws ConfigValidationException {
if (!configClasses.isEmpty()) {
mapConfiguration(ConfigMappingProvider.builder().validateUnknown(false), config, configClasses);
mapConfiguration(config, ConfigMappingProvider.builder().validateUnknown(false), configClasses);
}
}

Expand Down Expand Up @@ -73,13 +69,27 @@ public static Set<String> mappedProperties(final ConfigClassWithPrefix configCla
}

static void mapConfiguration(
final ConfigMappingProvider.Builder builder,
final SmallRyeConfig config,
final Set<ConfigClassWithPrefix> configClasses) throws ConfigValidationException {
final ConfigMappingProvider.Builder builder)
throws ConfigValidationException {
mapConfiguration(config, builder, new HashSet<>());
}

static void mapConfiguration(
final SmallRyeConfig config,
final ConfigMappingProvider.Builder builder,
final Set<ConfigClassWithPrefix> configClasses)
throws ConfigValidationException {
for (ConfigClassWithPrefix configClass : configClasses) {
builder.addRoot(configClass.getPrefix(), configClass.getKlass());
}
builder.build().mapConfiguration(config);
ConfigMappingProvider mappingProvider = builder.build();
mapConfiguration(config, mappingProvider);
}

static void mapConfiguration(SmallRyeConfig config, ConfigMappingProvider mappingProvider) {
ConfigMappingContext mappingContext = mappingProvider.mapConfiguration(config);
config.getConfigMappings().mappings.putAll(mappingContext.getRootsMap());
}

<T> T getConfigMapping(Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.smallrye.config;

public class DefaultValuesConfigSource extends KeyMapBackedConfigSource {
public final class DefaultValuesConfigSource extends KeyMapBackedConfigSource {
private static final long serialVersionUID = -6386021034957868328L;

public DefaultValuesConfigSource(final KeyMap<String> properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
import org.eclipse.microprofile.config.spi.Converter;

import io.smallrye.common.annotation.Experimental;
Expand All @@ -64,10 +65,10 @@ public class SmallRyeConfig implements Config, Serializable {

private final ConfigMappings mappings;

SmallRyeConfig(SmallRyeConfigBuilder builder, ConfigMappings mappings) {
SmallRyeConfig(SmallRyeConfigBuilder builder) {
this.configSources = new ConfigSources(builder);
this.converters = buildConverters(builder);
this.mappings = mappings;
this.mappings = new ConfigMappings(builder.getValidator());
}

private Map<Type, Converter<?>> buildConverters(final SmallRyeConfigBuilder builder) {
Expand Down Expand Up @@ -575,7 +576,13 @@ private static class ConfigSources implements Serializable {
}

private static List<ConfigSource> buildSources(final SmallRyeConfigBuilder builder) {
final List<ConfigSource> sourcesToBuild = new ArrayList<>(builder.getSources());
List<ConfigSource> sourcesToBuild = new ArrayList<>(builder.getSources());
for (ConfigSourceProvider sourceProvider : builder.getSourceProviders()) {
for (ConfigSource configSource : sourceProvider.getConfigSources(builder.getClassLoader())) {
sourcesToBuild.add(configSource);
}
}

if (builder.isAddDiscoveredSources()) {
sourcesToBuild.addAll(builder.discoverSources());
}
Expand All @@ -588,7 +595,7 @@ private static List<ConfigSource> buildSources(final SmallRyeConfigBuilder build
}

private static List<InterceptorWithPriority> buildInterceptors(final SmallRyeConfigBuilder builder) {
final List<InterceptorWithPriority> interceptors = new ArrayList<>(builder.getInterceptors());
List<InterceptorWithPriority> interceptors = new ArrayList<>(builder.getInterceptors());
if (builder.isAddDiscoveredInterceptors()) {
interceptors.addAll(builder.discoverInterceptors());
}
Expand All @@ -601,7 +608,7 @@ private static List<InterceptorWithPriority> buildInterceptors(final SmallRyeCon
}

private static List<ConfigSourceWithPriority> mapSources(final List<ConfigSource> sources) {
final List<ConfigSourceWithPriority> sourcesWithPriority = new ArrayList<>();
List<ConfigSourceWithPriority> sourcesWithPriority = new ArrayList<>();
for (ConfigSource source : sources) {
if (!(source instanceof ConfigurableConfigSource)) {
sourcesWithPriority.add(new ConfigSourceWithPriority(source));
Expand All @@ -613,7 +620,7 @@ private static List<ConfigSourceWithPriority> mapSources(final List<ConfigSource
}

private static List<String> getProfiles(final List<ConfigSourceInterceptor> interceptors) {
for (final ConfigSourceInterceptor interceptor : interceptors) {
for (ConfigSourceInterceptor interceptor : interceptors) {
if (interceptor instanceof ProfileConfigSourceInterceptor) {
return Arrays.asList(((ProfileConfigSourceInterceptor) interceptor).getProfiles());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ public KeyMap<String> getDefaultValues() {
return defaultValues;
}

ClassLoader getClassLoader() {
return classLoader;
}

public boolean isAddDefaultSources() {
return addDefaultSources;
}
Expand Down Expand Up @@ -518,23 +522,9 @@ public SmallRyeConfigBuilder setAddDiscoveredValidator(final boolean addDiscover

@Override
public SmallRyeConfig build() {
for (ConfigSourceProvider sourceProvider : sourceProviders) {
for (ConfigSource configSource : sourceProvider.getConfigSources(classLoader)) {
sources.add(configSource);
}
}

ConfigMappingProvider mappingProvider = mappingsBuilder.build();
defaultValues.putAll(mappingProvider.getDefaultValues());

try {
ConfigMappings configMappings = new ConfigMappings(getValidator());
SmallRyeConfig config = new SmallRyeConfig(this, configMappings);
mappingProvider.mapConfiguration(config);
return config;
} catch (ConfigValidationException e) {
throw new IllegalStateException(e);
}
SmallRyeConfig config = new SmallRyeConfig(this);
ConfigMappings.mapConfiguration(config, mappingsBuilder.build());
return config;
}

static class ConverterWithPriority {
Expand Down
Loading

0 comments on commit 62a3cea

Please sign in to comment.