11package io .jooby .internal ;
22
3+ import io .jooby .BeanConverter ;
34import io .jooby .FileUpload ;
5+ import io .jooby .Router ;
46import io .jooby .TypeMismatchException ;
57import io .jooby .Value ;
68import io .jooby .internal .converter .BigDecimalConverter ;
@@ -35,7 +37,6 @@ public class ValueConverters {
3537
3638 static List <ValueConverter > defaultConverters () {
3739 List <ValueConverter > result = new ArrayList <>();
38- // result.add(new EnumConverter());
3940 result .add (new UUIDConverter ());
4041
4142 result .add (new InstantConverter ());
@@ -63,25 +64,28 @@ static List<ValueConverter> defaultConverters() {
6364
6465 static void addFallbackConverters (List <ValueConverter > converters ) {
6566 converters .add (new ValueOfConverter ());
67+ }
68+
69+ static void addFallbackBeanConverters (List <BeanConverter > converters ) {
6670 converters .add (new ReflectiveBeanConverter ());
6771 }
6872
69- public static <T > T convert (Value value , Type type , List < ValueConverter > converters ) {
73+ public static <T > T convert (Value value , Type type , Router router ) {
7074 Class rawType = $Types .getRawType (type );
7175 if (List .class .isAssignableFrom (rawType )) {
7276 return (T ) Collections
73- .singletonList (convert (value , $Types .parameterizedType0 (type ), converters ));
77+ .singletonList (convert (value , $Types .parameterizedType0 (type ), router ));
7478 }
7579 if (Set .class .isAssignableFrom (rawType )) {
76- return (T ) Collections .singleton (convert (value , $Types .parameterizedType0 (type ), converters ));
80+ return (T ) Collections .singleton (convert (value , $Types .parameterizedType0 (type ), router ));
7781 }
7882 if (Optional .class .isAssignableFrom (rawType )) {
79- return (T ) Optional .ofNullable (convert (value , $Types .parameterizedType0 (type ), converters ));
83+ return (T ) Optional .ofNullable (convert (value , $Types .parameterizedType0 (type ), router ));
8084 }
81- return convert (value , rawType , converters );
85+ return convert (value , rawType , router );
8286 }
8387
84- public static <T > T convert (Value value , Class type , List < ValueConverter > converters ) {
88+ public static <T > T convert (Value value , Class type , Router router ) {
8589 if (type == String .class ) {
8690 return (T ) first (value ).valueOrNull ();
8791 }
@@ -123,23 +127,28 @@ public static <T> T convert(Value value, Class type, List<ValueConverter> conver
123127 return (T ) (value .isMissing () ? null : Byte .valueOf (first (value ).byteValue ()));
124128 }
125129 /** File Upload: */
126- if (Path .class == type ) {
127- if (value .isUpload ()) {
130+ if (value .isUpload ()) {
131+ if (Path .class == type ) {
132+
128133 FileUpload upload = (FileUpload ) value ;
129134 return (T ) upload .path ();
130- }
131- throw new TypeMismatchException (value .name (), Path .class );
132- }
133- if (FileUpload .class == type ) {
134- if (value .isUpload ()) {
135+ } else if (FileUpload .class == type ) {
135136 return (T ) value ;
136137 }
137138 throw new TypeMismatchException (value .name (), FileUpload .class );
138139 }
139140
140- for (ValueConverter converter : converters ) {
141- if (converter .supports (type )) {
142- return (T ) converter .convert (value , type );
141+ if (value .isSingle ()) {
142+ for (ValueConverter converter : router .getConverters ()) {
143+ if (converter .supports (type )) {
144+ return (T ) converter .convert (value , type );
145+ }
146+ }
147+ } else if (value .isObject ()) {
148+ for (BeanConverter converter : router .getBeanConverters ()) {
149+ if (converter .supports (type )) {
150+ return (T ) converter .convert (value , type );
151+ }
143152 }
144153 }
145154 return null ;
@@ -162,15 +171,15 @@ private static Object enumValue(Value value, Class type) {
162171 }
163172
164173 private static Value first (Value source ) {
165- if (source .isSingle ()) {
166- return source ;
167- }
168- if (source .isArray ()) {
169- return source .get (0 );
170- }
171- if (source .isObject () && source .size () > 0 ) {
172- return source .iterator ().next ();
173- }
174+ // if (source.isSingle()) {
175+ // return source;
176+ // }
177+ // if (source.isArray()) {
178+ // return source.get(0);
179+ // }
180+ // if (source.isObject() && source.size() > 0) {
181+ // return source.iterator().next();
182+ // }
174183 return source ;
175184 }
176185}
0 commit comments