Skip to content

Commit

Permalink
Support EnumMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
marecabo committed Oct 14, 2023
1 parent a2661e3 commit 3be97c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@ private Object fromString(String value, Class<?> type, @Nullable Field paramFiel
} else if (paramField != null && paramField.getGenericType() instanceof ParameterizedType pType) {
Class<?> keyClass = TypeFactory.rawClass(pType.getActualTypeArguments()[0]);
Class<?> valueClass = TypeFactory.rawClass(pType.getActualTypeArguments()[1]);
JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(
LinkedHashMap.class, keyClass, valueClass);
Class<? extends Map> mapClass = keyClass.isEnum() ? EnumMap.class : LinkedHashMap.class;
JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(mapClass, keyClass,
valueClass);
try {
return OBJECT_MAPPER.readValue(value, mapType);
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.LocalTime;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -74,6 +75,8 @@ public void testDumpAndRead() {
dumpedModule.integerMapField = Map.of("a\"b", 1, "b", 2);
dumpedModule.localDateMapField = Map.of('d', LocalDate.of(2023, 10, 9));
dumpedModule.booleanMapField = Map.of(0.1, true);
dumpedModule.enumMapField = new EnumMap<>(MyEnum.class);
dumpedModule.enumMapField.put(MyEnum.VALUE1, "abc");
dumpedModule.setField = ImmutableSet.of("a", "b", "c");
dumpedModule.listField = List.of("1", "2", "3");
assertEqualAfterDumpAndRead(dumpedModule);
Expand Down Expand Up @@ -198,6 +201,7 @@ public void testComments() {
expectedComments.put("integerMapField", "map of Integer");
expectedComments.put("localDateMapField", "map of LocalDate");
expectedComments.put("booleanMapField", "map of Boolean");
expectedComments.put("enumMapField", "map of Enum");

assertThat(new MyModule().getComments()).isEqualTo(expectedComments);
}
Expand Down Expand Up @@ -515,6 +519,10 @@ private static class MyModule extends ReflectiveConfigGroup {
@Parameter
private Map<Double, Boolean> booleanMapField;

@Comment("map of Enum")
@Parameter
private Map<MyEnum, String> enumMapField;

// Object fields:
// Id: string representation is toString
private Id<Link> idField;
Expand Down

0 comments on commit 3be97c6

Please sign in to comment.