0
votes

I am new to Mapstruct and am having issues in a particular usecase

so, if my source attribute have hotmail.com my target attribute should receive "personal" and if my source has facebook.com my target should receive "corporate".

I was trying to use expression but couldn't get my way around it. how do i do it?

@Mapping(source = "user.email", target = "emailType")
 NewDTO myMapperMethod(MyRequest req);
2
Can you post your Dto structure , You should have a two fileds in your Dto something like email and link ? Do you have those ? - Vinay Hegde
I do have these two fields. My requirement is if source field has value A in it I don't store A in target I should store Z. A gets stored as -> Z B gets stored as -> Y - pm1090

2 Answers

1
votes

You can use qualifiedByName and default interface method to qualify and define suitable mapping method for a given property:

@Mapper(componentModel = "spring")
public interface RequestMapper {

    @Mapping(source = "user.email", target = "emailType", qualifiedByName = "EmailToType")
    NewDTO myMapperMethod(MyRequest req);

    @Named("EmailToType")
    default String emailTypeResolver(String email) {
        if ("hotmail.com".equals(email)) {
            return "personal";
        } else if ("facebook.com".equals(email)) {
            return "corporate";
        } else {
            return "unknown";
        }
    }
}
0
votes

It is not something MapStruct is supposed to solve for you. It is making a run-time decision based on content. Typically a business problem, not a mapping problem