I found this example about Iterable to Non-Iterable mapping using Qualifier :
https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable
But how to make this mapping able to map nested properties (using dot annotation)?
E.g. mapping the field xyz of first element of a collection in the source object to a plain field on the target object?
The example define a Qualifier
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface FirstElement {
}
then define a Custom Mapper
public class MapperUtils {
@FirstElement
public <T> T first(List<T> in) {
if (in != null && !in.isEmpty()) {
return in.get(0);
}
else {
return null;
}
}
}
and, finally, the mapping is defined as
@Mapping(target = "emailaddress", source = "emails", qualifiedBy = FirstElement.class )
But if I would like to extract from the first element of emails collection a specific field, e.g. like I would have done with code emails.get(0).getEmailAddress?
For example, I expect to write a mapping like this :
@Mapping(target = "emailaddress", source = "emails[0].emailAddress")