-
-
Notifications
You must be signed in to change notification settings - Fork 979
[Feature] Conditional mapping #2051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the suggestion @pherklotz. We also just received the same request in #2107. Their Option 1 is identical to yours. I like the proposal here, with one difference instead of using If you are still interested in providing a PR can you please sync with @Desislav-Petrov? Looking at this I got an idea that could be something interesting is as well. We can add a new annotation named e.g.
We can even look for a single This might even solve #2084 |
Hi @filiphr, I think I like your proposal more because in addition to what you said, this way we will have proper java code versus "java as a string" as in condition. Thanks, |
I don't mind having both options ( |
Yes, I think the |
For #2084 it can even work out of the box by defining some method like:
In theory if we add the support for |
Thanks for the good ideas, I've just started working on a PR and would have an initial impl shortly. |
That's great. Looking forward to reviewing it. |
Thanks for all your comments. I really like your idea @filiphr. This allows more complex checks with good readability. @Desislav-Petrov can I help you with the implementation? |
@pherklotz why not divide and conq on this one - I'm working on introducing the condition = method name bit. Once i'm done with this you can finish off the second part about introducing the @condition ? |
Thanks for the work on this @pherklotz and @Desislav-Petrov. Let us know if you need some help. One a side note something like this might help us with the presence check for #1075. We'll need a containsKey check as a presence check there. |
Hi, PR: #2148 Haven't implemented the @condition bit as I haven't heard if @pherklotz is interested to finish it off. I'm happy to do it as an extension if I don't hear back. PR feedback would be appreciated. Thanks, |
Hi, Cool, if this feature could be done. We require this feature for our use case as well and I also like this solution. Bg, |
Hi, the solution part from @Desislav-Petrov works already for me. How about the part for @condition annotation? Will it still be implemented. If not, I'd like to have a try. @filiphr Which parts should I change, if I wanna implement this new @condition annotation? Bg, |
I'm doing the @condition bit too, was waiting to hear if @pherklotz still wants to do part of the work or not. Since I haven't heard from him for a while I will continue with adding it. |
Hi, BR |
…stom presence check methods
…stom presence check methods
…stom presence check methods
This has now been implemented in the same style as the other functionality of MapStruct. The new Methods annotated with |
Hello filiphr, |
@filiphr : Could you please help me , I have been using @condition method as suggested below link But am not seeing this method has been used/invoked in the generated impl class. Could you please assist. |
@HemalathaGowdar you created #2688, there is no need to ping people and comment the some things in multiple issues. I've replied to you in #2688 |
Feature proposal
Specify a condition (similar to the expression) for a target. If the condition evaluates to true the mapping will be done. Otherwise nothing happens or the default will be used if specified.
Motivation
Whether a null check nor a presence checker method are sometimes mighty enough to express whether a property should be mapped or not. We use MapStruct to map Protobuf v3 Messages. These can contain oneof fields which can contain only exact one or zero values. If one of the specified values is a primitive datatype like int or long there is no hasXYZ method generated and the default value will be returned from the getter (e.g. zero) this is also the case if another value type is the in the oneof! You have to check the case-Field of the protobuf message to find out which of the oneofs is set. See protobuf documentation about that:
Example
Protobuf:
Generated mapper code for this:
But only one should be set. With the proposed conditional mapping I could write a condition to check the case-Field of the source to find out which property should be set.
Example for the usage of the proposed feature
Mapper interface
Generated mapping code for property "baz"
If you are open to adopt this feature I would provide a PR.
The text was updated successfully, but these errors were encountered: