Skip to content

Commit 1532418

Browse files
authored
Merge pull request #357 from avaje/feature/352-toEnum
Fix #354 - Add toEnum() to PathTypeConversion, also support non-upper case
2 parents 05009d8 + f8ec513 commit 1532418

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,31 @@ public static int asInt(String value) {
7171
}
7272

7373
/** Convert to enum. */
74-
@SuppressWarnings({"unchecked", "rawtypes"})
74+
@SuppressWarnings({"rawtypes"})
7575
public static <T> Enum asEnum(Class<T> clazz, String value) {
7676
checkNull(value);
7777
try {
78-
return Enum.valueOf((Class<Enum>) clazz, value.toUpperCase());
78+
return convertEnum(clazz, value);
7979
} catch (final IllegalArgumentException e) {
8080
throw new InvalidPathArgumentException(e);
8181
}
8282
}
8383

84+
@SuppressWarnings({"unchecked", "rawtypes"})
85+
public static <T> Enum convertEnum(Class<T> clazz, String value) {
86+
try {
87+
return Enum.valueOf((Class<Enum>) clazz, value);
88+
} catch (final IllegalArgumentException e) {
89+
if (value != null) {
90+
final String asUpper = value.toUpperCase();
91+
if (!asUpper.equals(value)) {
92+
return Enum.valueOf((Class<Enum>) clazz, asUpper);
93+
}
94+
}
95+
throw e;
96+
}
97+
}
98+
8499
/**
85100
* Convert to long.
86101
*/
@@ -240,6 +255,19 @@ public static Integer asInteger(String value) {
240255
}
241256
}
242257

258+
/** Convert to enum. */
259+
@SuppressWarnings({"rawtypes"})
260+
public static <T> Enum toEnum(Class<T> clazz, String value) {
261+
if (isNullOrEmpty(value)) {
262+
return null;
263+
}
264+
try {
265+
return convertEnum(clazz, value);
266+
} catch (final IllegalArgumentException e) {
267+
throw new InvalidTypeArgumentException(e);
268+
}
269+
}
270+
243271
/**
244272
* Convert to Integer (allowing nulls).
245273
*/

http-api/src/test/java/io/avaje/http/api/PathTypeConversionTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,44 @@ void asInteger_invalid() {
233233
assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asInteger("junk"));
234234
}
235235

236+
enum Hello {
237+
ONE,
238+
TWO
239+
}
240+
241+
@SuppressWarnings("unchecked")
242+
@Test
243+
void toEnum() {
244+
assertThat(PathTypeConversion.toEnum(Hello.class, "ONE")).isEqualTo(Hello.ONE);
245+
assertThat(PathTypeConversion.toEnum(Hello.class, "one")).isEqualTo(Hello.ONE);
246+
assertThat(PathTypeConversion.toEnum(Hello.class, "Two")).isEqualTo(Hello.TWO);
247+
assertThat(PathTypeConversion.toEnum(Hello.class, "")).isNull();
248+
assertThat(PathTypeConversion.toEnum(Hello.class, null)).isNull();
249+
}
250+
251+
@Test
252+
void toEnum_invalid() {
253+
assertThrows(InvalidTypeArgumentException.class, () -> PathTypeConversion.toEnum(Hello.class, "DoesNotExist"));
254+
}
255+
256+
@SuppressWarnings("unchecked")
257+
@Test
258+
void asEnum() {
259+
assertThat(PathTypeConversion.asEnum(Hello.class, "ONE")).isEqualTo(Hello.ONE);
260+
assertThat(PathTypeConversion.asEnum(Hello.class, "One")).isEqualTo(Hello.ONE);
261+
assertThat(PathTypeConversion.asEnum(Hello.class, "one")).isEqualTo(Hello.ONE);
262+
assertThat(PathTypeConversion.asEnum(Hello.class, "Two")).isEqualTo(Hello.TWO);
263+
assertThat(PathTypeConversion.toEnum(Hello.class, "")).isNull();
264+
assertThat(PathTypeConversion.toEnum(Hello.class, null)).isNull();
265+
}
266+
267+
@Test
268+
void asEnum_invalid() {
269+
assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, ""));
270+
assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, null));
271+
assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, "DoesNotExist"));
272+
}
273+
236274
@Test
237275
void toInteger() {
238276
assertThat(PathTypeConversion.toInteger("42")).isEqualTo(42);

http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,13 @@ static class EnumHandler extends ObjectHandler {
310310
private final UType type;
311311

312312
EnumHandler(UType type) {
313-
314313
super(type.mainType(), type.shortName());
315314
this.type = type;
316315
}
317316

318317
@Override
319318
public String toMethod() {
320-
return "(" + type.shortType() + ") asEnum(" + type.shortType() + ".class, ";
319+
return "(" + type.shortType() + ") toEnum(" + type.shortType() + ".class, ";
321320
}
322321

323322
@Override

0 commit comments

Comments
 (0)