It took some time for me to figure out how to get mapping from strings to enums to work, but I got it working and thought I'd share my experience.
First off, I did have some difficulty getting MapStruct working within IntelliJ but I eventually got it working simply by upgrading to the latest build (13.1.2). After that, the IDE had no problems locating the annotations and generating the mapper implementations.
MapStruct by convention mapped Strings to Enums if the String value exactly matched the Enum value as a string. But in my case, the source string is different than the enum value, like:
source.color = "Light Blue"
target.color = ColorEnum.LT_BLUE
So, my first attempt was to use the Mappings annotation to map the source string to the destination enum's string value, like this:
@Mappings({
@Mapping(source = "Light Blue", target = "LT_BLUE"),
...
})
ColorEnum colorToColor(String color);
But the compiler complained saying "Can't generate mapping method from non-enum type to enum type."
So, next I tried creating my own implementation. I didn't see any documentation on creating custom mapper implementations, but I thought I'd try it out. I made the enum mapper a class instead of an interface and wrote the code to map from the string to enum. I took out the @Mapper and @Mappings annotations and the INSTANCE member variable, and added to the parent class the 'uses' annotation parameter. And it worked! The parent mapper was able to locate the enum mapper class and use it.
This required me to define the mappings in a non-annotation way. I chose to put them in separate class. It would have been nice for MapStruct to support string to enum by specifying the @Mappings annotation like above. But I'm not sure how often my use case comes up. Another possible use case which I think would come up often is mapping numerics to enums, and not simply numerics representing an ordinal position but numbers that require an explicit mapping, like:
@Mappings({
@Mapping(source = "Light Blue", target = 1),
...
})
ColorEnum colorToColor(Integer colorId);
We almost went with our DTO using numerics but decided to use human readable strings instead since the source is actually json. I didn't try this but I assume this would also generate the "Can't generate..to enum type" message. Just a few suggestions, but being able to define and reference explicit mappers is a reasonable solution.
Thanks!
Charlie