Conditional mapping

4,426 views
Skip to first unread message

Ritchie Nithoo

unread,
Jul 21, 2016, 4:58:35 AM7/21/16
to mapstruct-users
Hello , 
I want to map an object A to an Object B. 
Lets say there are two attributes in A that we call attribC and attribD. 
I need to do the following : if attribC is not null then map attribC , else map attriD only. 
Is there a way to do a conditional mapping in mapstruct ? Or do i have to write the code "by hand" ?
Best regards , 
Ritchie

Andreas Gudian

unread,
Jul 21, 2016, 4:12:11 PM7/21/16
to Ritchie Nithoo, mapstruct-users
Hi Ritchie,

there are several options for doing this.

I would expect that your mapping method is declared as:
B toB(A a);

1) Use expressions, e.g.: @Mapping( target = "cOrD", expression = "java( a.getAttribC() ? a.getAttribC() : a.getAttribD() )" )

But this wouldn't be refactoring-safe.

2) Use a manual mapping method:
protected String selectCOrD(A a) {
   return a.getAttribC() ? a.getAttribC() : a.getAttribD();
}

and this annotation on your mapping method: @Mapping( target = "cOrD", source = "a" )

2) Use an @AfterMapping method to add the propery manually and ignore it in the automatic mapping using @Mapping( target = "cOrD", ignore = true )

Hope that helps,
Andreas


--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ritchie Nithoo

unread,
Jul 22, 2016, 9:24:42 AM7/22/16
to mapstruct-users
Hello Andreas , 
Thanks for your answer ! 

Concerning your two first methods , i might not have been accurate enough on my needs. The problem is as follows : i need to map C or D to a corresponding C or D. 
Here is a peusdo - code of what i would want : 

if (a.getAttribC != null) {
    b.setC = a.getAttribC)
} else {
   b.setD = a.getAttribD
}

We work with an environment that guarantees that if attribC is null then attribD is not (the interface specifications says so) so i don't have to worry on that part.
In fact the current code (code legacy) that we have does the trick just like the pseudo-code i wrote previously. 

Concerning your third answer : Indeed that might help me , but then i would bypass the use of mapstruct alltogether correct ? 

Thanks for your answers!
Ritchie

Andreas Gudian

unread,
Jul 22, 2016, 11:57:18 AM7/22/16
to Ritchie Nithoo, mapstruct-users
It's totally okay to handle special cases manually. We aim for helping users to do 80% or more of their mapping stuff automatically and give them the means to do the rest either by configuration or with manually created property mapping methods, decorators or before-/after mapping methods or the expressions (last resort). That configuration should never be harder or more complicated than writing the mapping method by hand.
It just isn't feasible to try supporting every use case there is in the wild out there out of the box or with special annotation configs. Who would wanna use such a monster anyway ;).

So to sum up: you're good and doing it just right when you add your custom code with one of the suggested approaches.

Andreas

--

Ritchie Nithoo

unread,
Jul 25, 2016, 10:34:18 AM7/25/16
to mapstruct-users
Hello Andreas , 
I ended up leaving the custom legacy code right where it was. I simply ignore the mapping using ignore = "true" in the @mapping config. 
Thanks for your help.
Ritchie

Le jeudi 21 juillet 2016 10:58:35 UTC+2, Ritchie Nithoo a écrit :
Reply all
Reply to author
Forward
0 new messages