-
-
Notifications
You must be signed in to change notification settings - Fork 924
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
Support Java 8 java.util.Optional type #674
Comments
Do you really think in addition, not instead? Anyone setting an In that light, related to the discussion around the "source value presence checking" strategy, I think there "IS_NULL" should be renamed to something like "DEFAULT" to abstract from the fact whether "is null" or "isPresent()" is invoked by the default means of value presence checking. |
it would be great if the request here is implemented in a more generic fashion. Meaning that the developer has to possiblity to provide an own isPresent(sourceObj, sourcePropertyName, targetObj, targetPropertyName), ignore(...) or skip(...) method to skip the mapping process for some of the properties. Some additional use cases:
|
Is there a way to enable mapstruct to handle java.util.Optionals? I'd love to provide a basic mapper config which is able to unwrap an optional and reuse it in my concrete mappers. I have an object which contains an optional address, which contains an optional contact, which contains an emailaddress. I would like to map this object into an object which only holds the emailaddress. Can you help me? |
I found a way by adding special getters for fields with optionals:
the mapping configuration is now: any better ideas? |
@leimer the idea would be to check |
@filiphr yes, that sounds reasonable. When source is Both are Only target is And how is this affected by the null-mapping options. Is this an easy one? |
There is a more generic workaround to handle
The resulting mapping code is something like: |
@leimer I would say that So I say we need something like: When source is Optional and target is not: Both are Optional: Only target is Optional:
For And whether it is easy, I am not sure. We already have the |
@leimer I like the "qualifiedByName" idea. For my needs I was curious about deep mapping. For example I need a field of an Optional object. So something like this with "qualifiedByName" I don't know if it will work:
|
@filiphr I am totally with you. This is bad style. But I think the generated mapper should react properly on this case. The question is: how? I haven't used MapStruct that much right now, so I don't know how normal mappings react in that situation. But to amplify "Optional over null" practices, I think we should handle nulled Optional fields and map them to
oops, I did a copy&paste mistake. Of course there is no
Mhm, not sure if I have the time in the next weeks (in two days I will start to renovate my house). But despite that: do you think this is a "good-first-ticket"? I think another ticket would be to adapt the IDE plugins to take Optional fields into account. |
@franzvezuli I don't think this gonna work.
When I try to compile this, I get the following error: I think, if you would like to do this type of mapping you would have to use my first workaround, by defining extra Java-Bean-Properties using additional getters/setters |
Handling Thanks for the clarification about only target being
It really is up to you. I think it is not that difficult to add this. We can always guide you along the way. You can start small first by creating some tests for the feature and then go from it. In any case it is completely up to you. We are always willing to accept new contributors and help them in the project 😄 |
Any plan to push this forward? Having support for Optionals would be really useful |
@cookieMr what are you trying to imply with your comment? This is an Open Source Project and anyone can start working on this issue and help us out in implementing this feature. We have already given some pointers and we are always ready to help out. The entire team works on this project in their spare time and currently we are focused on some other issues. |
@filiphr you are right, i am sorry, i removed my comment, keep up the good work and ignore me |
I would like to contribute this feature, looking for some guidance |
Hi, It gives me this error Is it a regression? or intended? is there another workaround as it seems this feature isn't available. |
@phmaes the error you are getting has nothing to do with this issue here. Please do not post the same comments twice. Either create a new issue or leave a comment |
@filiphr I would love to contribute to this project 🙂 Could you please let me know if
|
@michalkowol glad that you would like to contribute. I would say that the optional should be done more like the |
Hey @filiphr, throwing in my contender for this feature into the ring #3183 My fix takes the approach that @michalkowol mentioned, as it personally felt like the more natural place for it to go. I didn't really get too far when looking into |
Thanks for your contender @ro0sterjam. I'll need some more time to see the differences between yours (PR #3183) and the one from @ibogdanchikov (PR #2874). I appologize in advance that this will take a bit more time |
and then 20 years went by... |
The Java Optional class also has a map() method that is useful for this purpose. The map method takes a "functional interface" Function method and the StructMap mapper method can be used. Example:
It takes the burden of unpacking and repacking the optional away from MapStruct. |
still waiting :( |
It's a little bit more than adding a built-in conversion:
Optional<T> prop
source property:prop.isPresent()
in addition to the null-check (in every spot where we currently use our null-check assignment wrapper)prop.get()
to fetch the valueOptional<T> prop
target property:Optional.empty()
instead ofnull
for null-assignments, orOptional.ofNullable(..)
for other assignments.Optionally (pun intended) also support the equivalent in Guavas
com.google.common.base.Optional
, which uses different names for the factory methods.The text was updated successfully, but these errors were encountered: