Skip to content

Commit

Permalink
Fix #354 - Add toEnum() to PathTypeConversion, also support non-upper…
Browse files Browse the repository at this point in the history
… case

Also support enums that are not all upper case (if people choose to do that).
  • Loading branch information
rob-bygrave committed Dec 12, 2023
1 parent 05009d8 commit f8ec513
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
32 changes: 30 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,31 @@ public static int asInt(String value) {
}

/** Convert to enum. */
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"rawtypes"})
public static <T> Enum asEnum(Class<T> clazz, String value) {
checkNull(value);
try {
return Enum.valueOf((Class<Enum>) clazz, value.toUpperCase());
return convertEnum(clazz, value);
} catch (final IllegalArgumentException e) {
throw new InvalidPathArgumentException(e);
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> Enum convertEnum(Class<T> clazz, String value) {
try {
return Enum.valueOf((Class<Enum>) clazz, value);
} catch (final IllegalArgumentException e) {
if (value != null) {
final String asUpper = value.toUpperCase();
if (!asUpper.equals(value)) {
return Enum.valueOf((Class<Enum>) clazz, asUpper);
}
}
throw e;
}
}

/**
* Convert to long.
*/
Expand Down Expand Up @@ -240,6 +255,19 @@ public static Integer asInteger(String value) {
}
}

/** Convert to enum. */
@SuppressWarnings({"rawtypes"})
public static <T> Enum toEnum(Class<T> 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).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8ec513

Please sign in to comment.