Open TWiStErRob opened 1 year ago
Caused by original implementation: https://github.com/jknack/handlebars.java/issues/237
Also affects {{lookup statuses lineStatus.line}}
syntax.
Workaround:
handlebars.registerHelpers(new HelperSource());
public static class HelperSource {
/**
* Workaround for `myEnumMap.[Foo]` and `lookup myEnumMap Foo` not working.
* Usage: Replace {@code (lookup someEnumMap someEnumKey)} with {@code (lookupEnumMap someEnumMap someEnumKey)}.
* @see https://github.com/jknack/handlebars.java/issues/1083
*/
public static <E extends Enum<E>> Object lookupEnumMap(EnumMap<E, ?> map, E key) {
return map.get(key);
}
}
I have a model property
Map<Line, StatusChange> statuses = new EnumMap(Line.class);
where these are the enum definitions:and this template:
{{statuses.[lineStatus.line].cssClass}}
, which now I understand is wrong, I think I needlookup
.However I did not get that understanding from the error message, since it was:
I had a look at the sources:
https://github.com/jknack/handlebars.java/blob/683c5e885d5dcdf3d17b33e9667f3fb153952016/handlebars/src/main/java/com/github/jknack/handlebars/context/MapValueResolver.java#L52
and debugged, where I confirmed that the code is passing in the wrong class:
then the error becomes a more meaningful:
Sadly this not only affects the error cases, the feature to be able to index into an
EnumMap
via aString
in[]
is broken too, because it won't find thevalueOf
given the wrong class. I tried{{statuses.[Bakerloo].cssClass}}
and got the sameis not an enum class
exception, even though according to theMapValueResolver
it should've "just worked".