Skip to content

Commit

Permalink
Upgrade to smallrye-config 1.3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Aug 22, 2019
1 parent f08030c commit c2e7ba0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 138 deletions.
2 changes: 1 addition & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<microprofile-opentracing-api.version>1.3.1</microprofile-opentracing-api.version>
<microprofile-reactive-streams-operators.version>1.0</microprofile-reactive-streams-operators.version>
<microprofile-rest-client.version>1.3.3</microprofile-rest-client.version>
<smallrye-config.version>1.3.5</smallrye-config.version>
<smallrye-config.version>1.3.9</smallrye-config.version>
<smallrye-health.version>1.0.3</smallrye-health.version>
<smallrye-metrics.version>1.1.0</smallrye-metrics.version>
<smallrye-open-api.version>1.1.3</smallrye-open-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public JavaInfo getTarget() {
)
// read config as properties
.read(config.getPropertyNames(), (String name) -> {
return new PropertyLine(name, config.getValue(name, String.class));
return new PropertyLine(name, config.getOptionalValue(name, String.class).orElse(""));
});

final User expectedUser = new User();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,15 @@ public static void loadConfiguration(final ExpandingConfigSource.Cache cache, Sm
boolean old = ExpandingConfigSource.setExpanding(false);
try {
leafType.acceptConfigurationValue(name, cache, config);
definition.loadedProperties.put(nameString, config.getValue(nameString, String.class));
definition.loadedProperties.put(nameString,
config.getOptionalValue(nameString, String.class).orElse(""));
} finally {
ExpandingConfigSource.setExpanding(old);
}
} else {
leafType.acceptConfigurationValue(name, cache, config);
definition.loadedProperties.put(nameString, config.getValue(nameString, String.class));
definition.loadedProperties.put(nameString,
config.getOptionalValue(nameString, String.class).orElse(""));
}
continue outer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import io.quarkus.runtime.configuration.ApplicationPropertiesConfigSource;
import io.quarkus.runtime.configuration.BuildTimeConfigFactory;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.quarkus.runtime.configuration.ConverterFactory;
import io.quarkus.runtime.configuration.ConverterSupport;
import io.quarkus.runtime.configuration.DefaultConfigSource;
import io.quarkus.runtime.configuration.DeploymentProfileConfigSource;
Expand All @@ -76,6 +75,7 @@
import io.quarkus.runtime.configuration.NameIterator;
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.runtime.configuration.SimpleConfigurationProviderResolver;
import io.smallrye.config.Converters;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;

Expand Down Expand Up @@ -111,7 +111,7 @@ public class ConfigurationSetup {
private static final MethodDescriptor NI_NEXT = MethodDescriptor.ofMethod(NameIterator.class, "next", void.class);
private static final MethodDescriptor ITR_HAS_NEXT = MethodDescriptor.ofMethod(Iterator.class, "hasNext", boolean.class);
private static final MethodDescriptor ITR_NEXT = MethodDescriptor.ofMethod(Iterator.class, "next", Object.class);
private static final MethodDescriptor CF_GET_IMPLICIT_CONVERTER = MethodDescriptor.ofMethod(ConverterFactory.class,
private static final MethodDescriptor C_GET_IMPLICIT_CONVERTER = MethodDescriptor.ofMethod(Converters.class,
"getImplicitConverter", Converter.class, Class.class);
private static final MethodDescriptor CPR_SET_INSTANCE = MethodDescriptor.ofMethod(ConfigProviderResolver.class,
"setInstance", void.class, ConfigProviderResolver.class);
Expand Down Expand Up @@ -222,7 +222,8 @@ public void initializeConfiguration(
try {
for (String propName : unmatched) {
runTimeDefaultConsumer
.accept(new RunTimeConfigurationDefaultBuildItem(propName, src.getValue(propName, String.class)));
.accept(new RunTimeConfigurationDefaultBuildItem(propName,
src.getOptionalValue(propName, String.class).orElse("")));
}
} finally {
ExpandingConfigSource.setExpanding(old);
Expand Down Expand Up @@ -345,7 +346,7 @@ public void write(final String name, final byte[] data) {
for (LeafConfigType item : runTimePatterns) {
final Class<?> typeClass = item.getItemClass();
if (!typeClass.isPrimitive() && encountered.add(typeClass)
&& ConverterFactory.getImplicitConverter(typeClass) != null) {
&& Converters.getImplicitConverter(typeClass) != null) {
configTypes.add(typeClass);
}
}
Expand Down Expand Up @@ -376,7 +377,7 @@ public void write(final String name, final byte[] data) {
final ResultHandle array = yes.newArray(Converter.class, yes.load(converterCnt));
for (int i = 0; i < converterCnt; i++) {
yes.writeArrayValue(array, i,
yes.invokeStaticMethod(CF_GET_IMPLICIT_CONVERTER, yes.loadClass(configTypes.get(i))));
yes.invokeStaticMethod(C_GET_IMPLICIT_CONVERTER, yes.loadClass(configTypes.get(i))));
}
yes.writeStaticField(CONVERTERS_FIELD, array);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -41,8 +42,9 @@ private static void handleObject(String prefix, Object o, SmallRyeConfig config)
}
for (Field field : cls.getDeclaredFields()) {
ConfigItem configItem = field.getDeclaredAnnotation(ConfigItem.class);
if (configItem == null || field.getType().isAnnotationPresent(ConfigGroup.class)) {
Object newInstance = field.getType().newInstance();
final Class<?> fieldClass = field.getType();
if (configItem == null || fieldClass.isAnnotationPresent(ConfigGroup.class)) {
Object newInstance = fieldClass.getConstructor().newInstance();
field.set(o, newInstance);
handleObject(prefix + "." + dashify(field.getName()), newInstance, config);
} else {
Expand All @@ -55,33 +57,47 @@ private static void handleObject(String prefix, Object o, SmallRyeConfig config)
if (defaultValue.equals(ConfigItem.NO_DEFAULT)) {
defaultValue = null;
}
Optional<?> val = config.getOptionalValue(fullName, field.getType());
final Type genericType = field.getGenericType();
Optional<?> val;
final boolean fieldIsOptional = fieldClass.equals(Optional.class);
final boolean fieldIsList = fieldClass.equals(List.class);
if (fieldIsOptional) {
Class<?> actualType = (Class<?>) ((ParameterizedType) genericType)
.getActualTypeArguments()[0];
val = config.getOptionalValue(fullName, actualType);
} else if (fieldIsList) {
Class<?> actualType = (Class<?>) ((ParameterizedType) genericType)
.getActualTypeArguments()[0];
val = config.getOptionalValues(fullName, actualType, ArrayList::new);
} else {
val = config.getOptionalValue(fullName, fieldClass);
}
if (val.isPresent()) {
field.set(o, val.get());
field.set(o, fieldIsOptional ? val : val.get());
} else if (defaultValue != null) {
if (field.getType().equals(List.class)) {
Class<?> listType = (Class<?>) ((ParameterizedType) field.getGenericType())
if (fieldClass.equals(List.class)) {
Class<?> listType = (Class<?>) ((ParameterizedType) genericType)
.getActualTypeArguments()[0];
String[] parts = defaultValue.split(",");
List<Object> list = new ArrayList<>();
for (String i : parts) {
list.add(config.convert(i, listType));
}
field.set(o, list);
} else if (field.getType().equals(Optional.class)) {
Class<?> optionalType = (Class<?>) ((ParameterizedType) field.getGenericType())
} else if (fieldIsOptional) {
Class<?> optionalType = (Class<?>) ((ParameterizedType) genericType)
.getActualTypeArguments()[0];
field.set(o, Optional.of(config.convert(defaultValue, optionalType)));
} else {
field.set(o, config.convert(defaultValue, field.getType()));
field.set(o, config.convert(defaultValue, fieldClass));
}
} else if (field.getType().equals(Optional.class)) {
} else if (fieldIsOptional) {
field.set(o, Optional.empty());
} else if (field.getType().equals(OptionalInt.class)) {
} else if (fieldClass.equals(OptionalInt.class)) {
field.set(o, OptionalInt.empty());
} else if (field.getType().equals(OptionalDouble.class)) {
} else if (fieldClass.equals(OptionalDouble.class)) {
field.set(o, OptionalDouble.empty());
} else if (field.getType().equals(OptionalLong.class)) {
} else if (fieldClass.equals(OptionalLong.class)) {
field.set(o, OptionalLong.empty());
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.runtime.configuration;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -12,6 +13,8 @@
import org.eclipse.microprofile.config.spi.Converter;
import org.jboss.logging.Logger;

import io.smallrye.config.Converters;

/**
* This small utility class is a tool which helps populating SmallRye {@link ConfigBuilder} with
* {@link Converter} implementations loaded from {@link ServiceLoader}.
Expand Down Expand Up @@ -80,7 +83,11 @@ static int getConverterPriority(final Class<? extends Converter<?>> converterCla
@SuppressWarnings("unchecked")
private static <T> ConverterItem<?> converterToItem(final Converter<T> converter) {
final Class<? extends Converter<T>> converterClass = (Class<? extends Converter<T>>) converter.getClass();
final Class<T> convertedType = ConverterFactory.getConverterType(converter);
final Type genericType = Converters.getConverterType(converterClass);
if (!(genericType instanceof Class<?>)) {
throw new IllegalArgumentException("General converters may not convert generic types");
}
final Class<T> convertedType = (Class<T>) genericType;
final int priority = getConverterPriority(converterClass);
return new ConverterItem<T>(convertedType, converter, priority);
}
Expand Down

This file was deleted.

0 comments on commit c2e7ba0

Please sign in to comment.