Skip to content

Commit d4f750c

Browse files
committed
Value API: sync simple types
1 parent b319128 commit d4f750c

3 files changed

Lines changed: 57 additions & 58 deletions

File tree

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.jooby.FileUpload;
1010
import io.jooby.Formdata;
1111
import io.jooby.Multipart;
12-
import io.jooby.TypeMismatchException;
1312
import io.jooby.Value;
1413

1514
import javax.annotation.Nonnull;
@@ -202,31 +201,11 @@ public int size() {
202201
}
203202

204203
@Nonnull @Override public <T> List<T> toList(@Nonnull Class<T> type) {
205-
if (hash instanceof TreeMap) {
206-
// indexes access, treat like a list
207-
Collection<Value> values = hash.values();
208-
List<T> result = new ArrayList<>(values.size());
209-
for (Value value : values) {
210-
result.add(value.to(type));
211-
}
212-
return result;
213-
} else {
214-
return Collections.singletonList(to(type));
215-
}
204+
return toCollection(type, new ArrayList<>());
216205
}
217206

218207
@Nonnull @Override public <T> Set<T> toSet(@Nonnull Class<T> type) {
219-
if (hash instanceof TreeMap) {
220-
// indexes access, treat like a list
221-
Collection<Value> values = hash.values();
222-
Set<T> result = new LinkedHashSet<>(values.size());
223-
for (Value value : values) {
224-
result.add(value.to(type));
225-
}
226-
return result;
227-
} else {
228-
return Collections.singleton(to(type));
229-
}
208+
return toCollection(type, new LinkedHashSet<>());
230209
}
231210

232211
@Nonnull @Override public <T> Optional<T> toOptional(@Nonnull Class<T> type) {
@@ -265,4 +244,17 @@ public HashValue put(Map<String, Collection<String>> headers) {
265244
}
266245
return this;
267246
}
247+
248+
private <T, C extends Collection<T>> C toCollection(@Nonnull Class<T> type, C collection) {
249+
if (hash instanceof TreeMap) {
250+
// indexes access, treat like a list
251+
Collection<Value> values = hash.values();
252+
for (Value value : values) {
253+
collection.add(value.to(type));
254+
}
255+
} else {
256+
collection.add(to(type));
257+
}
258+
return collection;
259+
}
268260
}

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,44 +87,44 @@ public static <T> T convert(Value value, Type type, Router router) {
8787

8888
public static <T> T convert(Value value, Class type, Router router) {
8989
if (type == String.class) {
90-
return (T) first(value).valueOrNull();
90+
return (T) value.valueOrNull();
9191
}
9292
if (type == int.class) {
93-
return (T) Integer.valueOf(first(value).intValue());
93+
return (T) Integer.valueOf(value.intValue());
9494
}
9595
if (type == long.class) {
96-
return (T) Long.valueOf(first(value).longValue());
96+
return (T) Long.valueOf(value.longValue());
9797
}
9898
if (type == float.class) {
99-
return (T) Float.valueOf(first(value).intValue());
99+
return (T) Float.valueOf(value.floatValue());
100100
}
101101
if (type == double.class) {
102-
return (T) Double.valueOf(first(value).intValue());
102+
return (T) Double.valueOf(value.doubleValue());
103103
}
104104
if (type == boolean.class) {
105-
return (T) Boolean.valueOf(first(value).booleanValue());
105+
return (T) Boolean.valueOf(value.booleanValue());
106106
}
107107
if (type == byte.class) {
108-
return (T) Byte.valueOf(first(value).byteValue());
108+
return (T) Byte.valueOf(value.byteValue());
109109
}
110110
if (type.isEnum()) {
111-
return (T) enumValue(first(value), type);
111+
return (T) enumValue(value, type);
112112
}
113113
// Wrapper
114114
if (type == Integer.class) {
115-
return (T) (value.isMissing() ? null : Integer.valueOf(first(value).intValue()));
115+
return (T) (value.isMissing() ? null : Integer.valueOf(value.intValue()));
116116
}
117117
if (type == Long.class) {
118-
return (T) (value.isMissing() ? null : Long.valueOf(first(value).longValue()));
118+
return (T) (value.isMissing() ? null : Long.valueOf(value.longValue()));
119119
}
120120
if (type == Float.class) {
121-
return (T) (value.isMissing() ? null : Float.valueOf(first(value).floatValue()));
121+
return (T) (value.isMissing() ? null : Float.valueOf(value.floatValue()));
122122
}
123123
if (type == Double.class) {
124-
return (T) (value.isMissing() ? null : Double.valueOf(first(value).doubleValue()));
124+
return (T) (value.isMissing() ? null : Double.valueOf(value.doubleValue()));
125125
}
126126
if (type == Byte.class) {
127-
return (T) (value.isMissing() ? null : Byte.valueOf(first(value).byteValue()));
127+
return (T) (value.isMissing() ? null : Byte.valueOf(value.byteValue()));
128128
}
129129
/** File Upload: */
130130
if (value.isUpload()) {
@@ -159,7 +159,7 @@ private static Object enumValue(Value value, Class type) {
159159
return Enum.valueOf(type, value.value().toUpperCase());
160160
} catch (IllegalArgumentException x) {
161161
String name = value.value();
162-
// Fallback to ignore case version:
162+
// Fallback: Ignore case:
163163
Set<Enum> enums = EnumSet.allOf(type);
164164
for (Enum e : enums) {
165165
if (e.name().equalsIgnoreCase(name)) {
@@ -169,17 +169,4 @@ private static Object enumValue(Value value, Class type) {
169169
throw x;
170170
}
171171
}
172-
173-
private static Value first(Value source) {
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-
// }
183-
return source;
184-
}
185172
}

modules/jooby-apt/src/main/java/io/jooby/internal/apt/ParamDefinition.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
import javax.lang.model.util.Types;
2727
import java.lang.reflect.Method;
2828
import java.lang.reflect.Type;
29+
import java.net.URI;
30+
import java.net.URL;
2931
import java.nio.file.Path;
32+
import java.time.Duration;
33+
import java.time.Period;
34+
import java.time.ZoneId;
3035
import java.util.List;
3136
import java.util.Optional;
3237
import java.util.Set;
38+
import java.util.TimeZone;
3339
import java.util.stream.Collectors;
3440

3541
public class ParamDefinition {
@@ -128,16 +134,13 @@ private boolean isSimpleType() {
128134
return true;
129135
}
130136
}
131-
ElementKind kind = typeUtils.asElement(type.getType()).getKind();
132-
if (kind == ElementKind.ENUM
133-
|| ((is(Optional.class) || is(List.class) || is(Set.class))) && kind == ElementKind.ENUM) {
134-
return true;
135-
}
136137
return false;
137138
}
138139

139140
private Class[] builtinTypes() {
140141
return new Class[]{
142+
String.class,
143+
141144
Boolean.class,
142145
Boolean.TYPE,
143146
Byte.class,
@@ -154,14 +157,31 @@ private Class[] builtinTypes() {
154157
Float.TYPE,
155158
Double.class,
156159
Double.TYPE,
157-
String.class,
160+
158161
Enum.class,
159-
java.time.Instant.class,
162+
160163
java.util.UUID.class,
164+
165+
java.time.Instant.class,
166+
java.util.Date.class,
167+
java.time.LocalDate.class,
168+
java.time.LocalDateTime.class,
169+
161170
java.math.BigDecimal.class,
162171
java.math.BigInteger.class,
172+
173+
Duration.class,
174+
Period.class,
175+
163176
java.nio.charset.Charset.class,
164-
io.jooby.StatusCode.class
177+
178+
io.jooby.StatusCode.class,
179+
180+
TimeZone.class,
181+
ZoneId.class,
182+
183+
URI.class,
184+
URL.class
165185
};
166186
}
167187

0 commit comments

Comments
 (0)