From f8ec5133b9c03f043937a827f765c8a6d009d36b Mon Sep 17 00:00:00 2001 From: "robin.bygrave" Date: Wed, 13 Dec 2023 12:18:03 +1300 Subject: [PATCH] Fix #354 - Add toEnum() to PathTypeConversion, also support non-upper case Also support enums that are not all upper case (if people choose to do that). --- .../io/avaje/http/api/PathTypeConversion.java | 32 +++++++++++++++- .../http/api/PathTypeConversionTest.java | 38 +++++++++++++++++++ .../io/avaje/http/generator/core/TypeMap.java | 3 +- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java b/http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java index 1cd272f20..9bd7568cc 100644 --- a/http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java +++ b/http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java @@ -71,16 +71,31 @@ public static int asInt(String value) { } /** Convert to enum. */ - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({"rawtypes"}) public static Enum asEnum(Class clazz, String value) { checkNull(value); try { - return Enum.valueOf((Class) clazz, value.toUpperCase()); + return convertEnum(clazz, value); } catch (final IllegalArgumentException e) { throw new InvalidPathArgumentException(e); } } + @SuppressWarnings({"unchecked", "rawtypes"}) + public static Enum convertEnum(Class clazz, String value) { + try { + return Enum.valueOf((Class) clazz, value); + } catch (final IllegalArgumentException e) { + if (value != null) { + final String asUpper = value.toUpperCase(); + if (!asUpper.equals(value)) { + return Enum.valueOf((Class) clazz, asUpper); + } + } + throw e; + } + } + /** * Convert to long. */ @@ -240,6 +255,19 @@ public static Integer asInteger(String value) { } } + /** Convert to enum. */ + @SuppressWarnings({"rawtypes"}) + public static Enum toEnum(Class clazz, String value) { + if (isNullOrEmpty(value)) { + return null; + } + try { + return convertEnum(clazz, value); + } catch (final IllegalArgumentException e) { + throw new InvalidTypeArgumentException(e); + } + } + /** * Convert to Integer (allowing nulls). */ diff --git a/http-api/src/test/java/io/avaje/http/api/PathTypeConversionTest.java b/http-api/src/test/java/io/avaje/http/api/PathTypeConversionTest.java index f682d09e7..d9c692cff 100644 --- a/http-api/src/test/java/io/avaje/http/api/PathTypeConversionTest.java +++ b/http-api/src/test/java/io/avaje/http/api/PathTypeConversionTest.java @@ -233,6 +233,44 @@ void asInteger_invalid() { assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asInteger("junk")); } + enum Hello { + ONE, + TWO + } + + @SuppressWarnings("unchecked") + @Test + void toEnum() { + assertThat(PathTypeConversion.toEnum(Hello.class, "ONE")).isEqualTo(Hello.ONE); + assertThat(PathTypeConversion.toEnum(Hello.class, "one")).isEqualTo(Hello.ONE); + assertThat(PathTypeConversion.toEnum(Hello.class, "Two")).isEqualTo(Hello.TWO); + assertThat(PathTypeConversion.toEnum(Hello.class, "")).isNull(); + assertThat(PathTypeConversion.toEnum(Hello.class, null)).isNull(); + } + + @Test + void toEnum_invalid() { + assertThrows(InvalidTypeArgumentException.class, () -> PathTypeConversion.toEnum(Hello.class, "DoesNotExist")); + } + + @SuppressWarnings("unchecked") + @Test + void asEnum() { + assertThat(PathTypeConversion.asEnum(Hello.class, "ONE")).isEqualTo(Hello.ONE); + assertThat(PathTypeConversion.asEnum(Hello.class, "One")).isEqualTo(Hello.ONE); + assertThat(PathTypeConversion.asEnum(Hello.class, "one")).isEqualTo(Hello.ONE); + assertThat(PathTypeConversion.asEnum(Hello.class, "Two")).isEqualTo(Hello.TWO); + assertThat(PathTypeConversion.toEnum(Hello.class, "")).isNull(); + assertThat(PathTypeConversion.toEnum(Hello.class, null)).isNull(); + } + + @Test + void asEnum_invalid() { + assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, "")); + assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, null)); + assertThrows(InvalidPathArgumentException.class, () -> PathTypeConversion.asEnum(Hello.class, "DoesNotExist")); + } + @Test void toInteger() { assertThat(PathTypeConversion.toInteger("42")).isEqualTo(42); diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java index c81938cf8..1594766f2 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java @@ -310,14 +310,13 @@ static class EnumHandler extends ObjectHandler { private final UType type; EnumHandler(UType type) { - super(type.mainType(), type.shortName()); this.type = type; } @Override public String toMethod() { - return "(" + type.shortType() + ") asEnum(" + type.shortType() + ".class, "; + return "(" + type.shortType() + ") toEnum(" + type.shortType() + ".class, "; } @Override