Skip to content

Commit 67a14d3

Browse files
committed
Value API: value converter vs bean converter
1 parent 0185bcb commit 67a14d3

20 files changed

Lines changed: 67 additions & 42 deletions

jooby/src/main/java/io/jooby/BeanConverter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55
*/
66
package io.jooby;
77

8+
import javax.annotation.Nonnull;
9+
810
/**
911
* Bean value converter, works like {@link ValueConverter} except that the input value is always
1012
* a hash/object value, not a simple string like it is required by {@link ValueConverter}.
1113
*
1214
* @since 2.1.1
1315
*/
14-
public interface BeanConverter extends ValueConverter {
16+
public interface BeanConverter {
17+
/**
18+
* True if the converter applies for the given type.
19+
*
20+
* @param type Conversion type.
21+
* @return True if the converter applies for the given type.
22+
*/
23+
boolean supports(@Nonnull Class type);
24+
25+
Object convert(@Nonnull ValueNode node, @Nonnull Class type);
1526
}

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,12 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
543543
return this;
544544
}
545545

546-
@Nonnull @Override public Jooby converter(ValueConverter converter) {
546+
@Nonnull @Override public Jooby converter(@Nonnull ValueConverter converter) {
547+
router.converter(converter);
548+
return this;
549+
}
550+
551+
@Nonnull @Override public Router converter(@Nonnull BeanConverter converter) {
547552
router.converter(converter);
548553
return this;
549554
}

jooby/src/main/java/io/jooby/Router.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,15 @@ default Router error(@Nonnull Predicate<StatusCode> predicate,
682682
* @param converter Custom value converter.
683683
* @return This router.
684684
*/
685-
@Nonnull Router converter(@Nonnull ValueConverter converter);
685+
@Nonnull Router converter(@Nonnull ValueConverter converter);/**
686+
687+
/**
688+
* Add a custom bean value converter.
689+
*
690+
* @param converter Custom value converter.
691+
* @return This router.
692+
*/
693+
@Nonnull Router converter(@Nonnull BeanConverter converter);
686694

687695
/**
688696
* Get all simple/string value converters.

jooby/src/main/java/io/jooby/ValueConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import javax.annotation.Nonnull;
99

1010
/**
11-
* Value converter for string values that come from query, path, form parameters into more specific
12-
* type.
11+
* Value converter for simple values that come from query, path, form, etc... parameters into more
12+
* specific type.
1313
*
1414
* It is an extension point for {@link ValueNode#to(Class)} calls.
1515
*/
@@ -29,5 +29,5 @@ public interface ValueConverter {
2929
* @param type Requested type.
3030
* @return Converted value.
3131
*/
32-
Object convert(@Nonnull ValueNode value, @Nonnull Class type);
32+
Object convert(@Nonnull Value value, @Nonnull Class type);
3333
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,12 @@ public Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder en
314314
}
315315

316316
@Nonnull @Override public Router converter(ValueConverter converter) {
317-
if (converter instanceof BeanConverter) {
318-
beanConverters.add((BeanConverter) converter);
319-
} else {
320-
converters.add(converter);
321-
}
317+
converters.add(converter);
318+
return this;
319+
}
320+
321+
@Nonnull @Override public Router converter(@Nonnull BeanConverter converter) {
322+
beanConverters.add(converter);
322323
return this;
323324
}
324325

jooby/src/main/java/io/jooby/internal/converter/BigDecimalConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby.internal.converter;
77

8-
import io.jooby.ValueNode;
8+
import io.jooby.Value;
99
import io.jooby.ValueConverter;
1010

1111
import java.math.BigDecimal;
@@ -15,7 +15,7 @@ public class BigDecimalConverter implements ValueConverter {
1515
return type == BigDecimal.class;
1616
}
1717

18-
@Override public BigDecimal convert(ValueNode value, Class type) {
18+
@Override public Object convert(Value value, Class type) {
1919
return new BigDecimal(value.value());
2020
}
2121
}

jooby/src/main/java/io/jooby/internal/converter/BigIntegerConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby.internal.converter;
77

8-
import io.jooby.ValueNode;
8+
import io.jooby.Value;
99
import io.jooby.ValueConverter;
1010

1111
import java.math.BigInteger;
@@ -15,7 +15,7 @@ public class BigIntegerConverter implements ValueConverter {
1515
return type == BigInteger.class;
1616
}
1717

18-
@Override public BigInteger convert(ValueNode value, Class type) {
18+
@Override public Object convert(Value value, Class type) {
1919
return new BigInteger(value.value());
2020
}
2121
}

jooby/src/main/java/io/jooby/internal/converter/CharsetConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby.internal.converter;
77

8-
import io.jooby.ValueNode;
8+
import io.jooby.Value;
99
import io.jooby.ValueConverter;
1010

1111
import java.nio.charset.Charset;
@@ -16,7 +16,7 @@ public class CharsetConverter implements ValueConverter {
1616
return type == Charset.class;
1717
}
1818

19-
@Override public Charset convert(ValueNode value, Class type) {
19+
@Override public Object convert(Value value, Class type) {
2020
String charset = value.value();
2121
switch (charset.toLowerCase()) {
2222
case "utf-8":

jooby/src/main/java/io/jooby/internal/converter/DateConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby.internal.converter;
77

8-
import io.jooby.ValueNode;
8+
import io.jooby.Value;
99
import io.jooby.ValueConverter;
1010

1111
import java.time.LocalDate;
@@ -18,7 +18,7 @@ public class DateConverter implements ValueConverter {
1818
return type == Date.class;
1919
}
2020

21-
@Override public Object convert(ValueNode value, Class type) {
21+
@Override public Object convert(Value value, Class type) {
2222
try {
2323
// must be millis
2424
return new Date(Long.parseLong(value.value()));

jooby/src/main/java/io/jooby/internal/converter/DurationConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby.internal.converter;
77

8-
import io.jooby.ValueNode;
8+
import io.jooby.Value;
99
import io.jooby.ValueConverter;
1010

1111
import com.typesafe.config.ConfigException;
@@ -21,7 +21,7 @@ public class DurationConverter implements ValueConverter {
2121
return type == Duration.class;
2222
}
2323

24-
@Override public Object convert(ValueNode value, Class type) {
24+
@Override public Object convert(Value value, Class type) {
2525
try {
2626
return Duration.parse(value.value());
2727
} catch (DateTimeParseException x) {

0 commit comments

Comments
 (0)