Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @WithConverter in Optional #864

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public static final class LeafProperty extends MayBeOptionalProperty {
super(method, propertyName);
this.valueType = valueType;
this.convertWith = convertWith;
rawType = rawTypeOf(valueType);
this.rawType = rawTypeOf(valueType);
this.defaultValue = defaultValue;
}

Expand Down Expand Up @@ -718,7 +718,7 @@ private static Property getPropertyDef(Method method, AnnotatedType type) {
}

// now figure out what kind it is
Class<? extends Converter<?>> convertWith = getConvertWith(type);
Class<? extends Converter<?>> convertWith = getConvertWith(type, method);
String propertyName = getPropertyName(method);
Class<?> rawType = rawTypeOf(type.getType());
if (rawType.isPrimitive()) {
Expand All @@ -739,7 +739,7 @@ private static Property getPropertyDef(Method method, AnnotatedType type) {
if (rawType == Map.class) {
// it's a map...
AnnotatedType keyType = typeOfParameter(type, 0);
Class<? extends Converter<?>> keyConvertWith = getConvertWith(keyType);
Class<? extends Converter<?>> keyConvertWith = getConvertWith(keyType, method);
AnnotatedType valueType = typeOfParameter(type, 1);
return new MapProperty(method, propertyName, keyType.getType(), keyConvertWith,
getPropertyDef(method, valueType));
Expand Down Expand Up @@ -768,18 +768,19 @@ private static Property getPropertyDef(Method method, AnnotatedType type) {
// fall out (leaf)
}

WithDefault annotation = method.getAnnotation(WithDefault.class);
String defaultValue = annotation == null ? null : annotation.value();
if (rawType == List.class || rawType == Set.class) {
Type elementType = typeOfParameter(type.getType(), 0);
WithDefault annotation = method.getAnnotation(WithDefault.class);
return new CollectionProperty(rawType,
new LeafProperty(method, propertyName, elementType, convertWith,
annotation == null ? null : annotation.value()));
new LeafProperty(method, propertyName, elementType, convertWith, defaultValue));
} else if (rawType == Optional.class) {
return new OptionalProperty(method, propertyName,
new LeafProperty(method, propertyName, type.getType(), convertWith, defaultValue));
}

// otherwise it's a leaf
WithDefault annotation = method.getAnnotation(WithDefault.class);
return new LeafProperty(method, propertyName, type.getType(), convertWith,
annotation == null ? null : annotation.value());
return new LeafProperty(method, propertyName, type.getType(), convertWith, defaultValue);
}

private static boolean isToStringMethod(Method method) {
Expand Down Expand Up @@ -810,8 +811,12 @@ private static Method hasDefaultMethodImplementation(Method method) {
return null;
}

private static Class<? extends Converter<?>> getConvertWith(final AnnotatedType type) {
private static Class<? extends Converter<?>> getConvertWith(final AnnotatedType type, final Method method) {
WithConverter annotation = type.getAnnotation(WithConverter.class);
// fallback to method
if (annotation == null) {
annotation = method.getAnnotation(WithConverter.class);
}
if (annotation != null) {
Class<? extends Converter<?>> value = annotation.value();
validateConverter(type.getType(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
Expand Down Expand Up @@ -1788,4 +1789,73 @@ interface Value {
String value();
}
}

@Test
void optionalWithConverter() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withMapping(OptionalWithConverter.class)
.withSources(config("optional.converter.value", "value"))
.withSources(config("optional.converter.primitive", "1"))
.withSources(config("optional.converter.wrapper-int", "1"))
.withSources(config("optional.converter.primitive-array", "dummy"))
.build();

OptionalWithConverter mapping = config.getConfigMapping(OptionalWithConverter.class);

assertEquals("value", mapping.value().value);
assertTrue(mapping.optionalValue().isPresent());
assertEquals("value", mapping.optionalValue().get().value);
assertEquals(0, mapping.primitive());
assertTrue(mapping.wrapperInt().isPresent());
assertEquals(0, mapping.wrapperInt().get());
}

@ConfigMapping(prefix = "optional.converter")
interface OptionalWithConverter {
@WithConverter(ValueConverter.class)
Value value();

@WithName("value")
@WithConverter(ValueConverter.class)
Optional<Value> optionalValue();

@WithConverter(IntConverter.class)
int primitive();

@WithConverter(IntConverter.class)
Optional<Integer> wrapperInt();

@WithConverter(ByteArrayConverter.class)
byte[] primitiveArray();
}

public static class ValueConverter implements Converter<Value> {
@Override
public Value convert(final String value) throws IllegalArgumentException, NullPointerException {
return new Value(value);
}
}

public static class IntConverter implements Converter<Integer> {
@Override
public Integer convert(final String value) throws IllegalArgumentException, NullPointerException {
return 0;
}
}

public static class ByteArrayConverter implements Converter<byte[]> {
@Override
public byte[] convert(String value) throws IllegalArgumentException, NullPointerException {
return value.getBytes(StandardCharsets.UTF_8);
}
}

static class Value {
String value;

Value(final String value) {
this.value = value;
}
}
}