Skip to content

Commit b0f5678

Browse files
committed
Value API: replace hardcoded string -> type conversion with ValueConverter
1 parent 491e863 commit b0f5678

20 files changed

Lines changed: 394 additions & 142 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.jooby.converter;
2+
3+
import java.math.BigDecimal;
4+
5+
public class BigDecimalConverter implements ValueConverter {
6+
@Override public boolean supports(Class type) {
7+
return type == BigDecimal.class;
8+
}
9+
10+
@Override public BigDecimal convert(Class type, String value) {
11+
return new BigDecimal(value);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.jooby.converter;
2+
3+
import java.math.BigInteger;
4+
5+
public class BigIntegerConverter implements ValueConverter {
6+
@Override public boolean supports(Class type) {
7+
return type == BigInteger.class;
8+
}
9+
10+
@Override public BigInteger convert(Class type, String value) {
11+
return new BigInteger(value);
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.jooby.converter;
2+
3+
import java.nio.charset.Charset;
4+
import java.nio.charset.StandardCharsets;
5+
6+
public class CharsetConverter implements ValueConverter {
7+
@Override public boolean supports(Class type) {
8+
return type == Charset.class;
9+
}
10+
11+
@Override public Charset convert(Class type, String value) {
12+
switch (value.toLowerCase()) {
13+
case "utf-8":
14+
return StandardCharsets.UTF_8;
15+
case "us-ascii":
16+
return StandardCharsets.US_ASCII;
17+
case "iso-8859-1":
18+
return StandardCharsets.ISO_8859_1;
19+
case "utf-16":
20+
return StandardCharsets.UTF_16;
21+
case "utf-16be":
22+
return StandardCharsets.UTF_16BE;
23+
case "utf-16le":
24+
return StandardCharsets.UTF_16LE;
25+
default:
26+
return Charset.forName(value);
27+
}
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.jooby.converter;
2+
3+
import java.time.LocalDate;
4+
import java.time.ZoneId;
5+
import java.time.format.DateTimeFormatter;
6+
import java.util.Date;
7+
8+
public class DateConverter implements ValueConverter {
9+
@Override public boolean supports(Class type) {
10+
return type == Date.class;
11+
}
12+
13+
@Override public Object convert(Class type, String value) {
14+
try {
15+
// must be millis
16+
return new Date(Long.parseLong(value));
17+
} catch (NumberFormatException x) {
18+
// must be YYYY-MM-dd
19+
LocalDate date = LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE);
20+
return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
21+
}
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.jooby.converter;
2+
3+
import com.typesafe.config.ConfigException;
4+
import com.typesafe.config.ConfigFactory;
5+
import com.typesafe.config.ConfigValueFactory;
6+
7+
import java.time.Duration;
8+
import java.time.format.DateTimeParseException;
9+
import java.util.concurrent.TimeUnit;
10+
11+
public class DurationConverter implements ValueConverter {
12+
@Override public boolean supports(Class type) {
13+
return type == Duration.class;
14+
}
15+
16+
@Override public Object convert(Class type, String value) {
17+
try {
18+
return Duration.parse(value);
19+
} catch (DateTimeParseException x) {
20+
try {
21+
long duration = ConfigFactory.empty().withValue("d", ConfigValueFactory.fromAnyRef(value))
22+
.getDuration("d", TimeUnit.MILLISECONDS);
23+
return Duration.ofMillis(duration);
24+
} catch (ConfigException ignored) {
25+
throw x;
26+
}
27+
}
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.jooby.converter;
2+
3+
public class EnumConverter implements ValueConverter {
4+
@Override public boolean supports(Class type) {
5+
return type.isEnum();
6+
}
7+
8+
@Override public Enum convert(Class type, String value) {
9+
try {
10+
return Enum.valueOf(type, value);
11+
} catch (IllegalArgumentException x) {
12+
try {
13+
return Enum.valueOf(type, value.toUpperCase());
14+
} catch (IllegalArgumentException x1) {
15+
throw x;
16+
}
17+
}
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.jooby.converter;
2+
3+
import java.time.Instant;
4+
5+
public class InstantConverter implements ValueConverter {
6+
@Override public boolean supports(Class type) {
7+
return type == Instant.class;
8+
}
9+
10+
@Override public Instant convert(Class type, String value) {
11+
return Instant.ofEpochMilli(Long.parseLong(value));
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.jooby.converter;
2+
3+
import java.time.Instant;
4+
import java.time.LocalDate;
5+
import java.time.ZoneId;
6+
import java.time.format.DateTimeFormatter;
7+
8+
public class LocalDateConverter implements ValueConverter {
9+
@Override public boolean supports(Class type) {
10+
return type == LocalDate.class;
11+
}
12+
13+
@Override public Object convert(Class type, String value) {
14+
try {
15+
// must be millis
16+
Instant instant = Instant.ofEpochMilli(Long.parseLong(value));
17+
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
18+
} catch (NumberFormatException x) {
19+
// must be YYYY-MM-dd
20+
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE);
21+
}
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.jooby.converter;
2+
3+
import java.time.Instant;
4+
import java.time.LocalDateTime;
5+
import java.time.ZoneId;
6+
import java.time.format.DateTimeFormatter;
7+
8+
public class LocalDateTimeConverter implements ValueConverter {
9+
@Override public boolean supports(Class type) {
10+
return type == LocalDateTime.class;
11+
}
12+
13+
@Override public Object convert(Class type, String value) {
14+
try {
15+
// must be millis
16+
Instant instant = Instant.ofEpochMilli(Long.parseLong(value));
17+
return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
18+
} catch (NumberFormatException x) {
19+
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
20+
}
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.jooby.converter;
2+
3+
import java.time.Duration;
4+
import java.time.Period;
5+
6+
public class PeriodConverter implements ValueConverter {
7+
private final DurationConverter converter = new DurationConverter();
8+
9+
@Override public boolean supports(Class type) {
10+
return type == Period.class;
11+
}
12+
13+
@Override public Object convert(Class type, String value) {
14+
return Period.from((Duration) converter.convert(type, value));
15+
}
16+
}

0 commit comments

Comments
 (0)