GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — #674 Support Java 8 java.util.Optional type by ibogdanchikov · Pull Request #2874 · mapstruct/mapstruct

Conversation

@ibogdanchikov

@ibogdanchikov ibogdanchikov commented Jun 6, 2022

Copy link
Copy Markdown
Contributor

Adds support for mapping between java.util.Optional<T> and T. See #674 for details.

Asking the maintainers for a code review. Thank you for your time.

This is my first time ever trying to contribute to an open source project, so I apologize in advance for anything I might have done wrong.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rethab and peter-mghendi reacted with heart emoji
All reactions
  • ❤️ 2 reactions
@filiphr

filiphr commented Jun 6, 2022

Copy link
Copy Markdown
Member

Thanks a lot for the PR @ibogdanchikov. On first sight it looks OK.

I saw that you want for the build in method approach. However, I don't think that this is needed. I think that this would be more suited to work with inline conversions. And do things like Optional.ofNullable( ... ) and optional.get() for the mapping for from and to.

In addition to this, for Optional#get to be able to be used I think that we need to add some out of the box conditional method that would do the check if the optional is present.

Basically the code that is generated to look something like:

targetOptionalProperty.setToOptionalProp( Optional.ofNullable( source.getToOptionalProp() ) );
if ( source.getFromOptionalProp().isPresent() ) {
    targetOptionalProperty.setFromOptionalProp( source.getFromOptionalProp().get() );
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented Jun 6, 2022

Copy link
Copy Markdown
Member

This is my first time ever trying to contribute to an open source project, so I apologize in advance for anything I might have done wrong.

By the way. Congrats on your first open source contribution. We are happy to see that you chose MapStruct 😄

ibogdanchikov, govi20, Gabriele-Tomberli, mamadaliev, and peter-mghendi reacted with heart emoji
All reactions
  • ❤️ 5 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ibogdanchikov

ibogdanchikov commented Jun 6, 2022

Copy link
Copy Markdown
Contributor Author

I saw that you want for the build in method approach. However, I don't think that this is needed. I think that this would be more suited to work with inline conversions.

I totally agree. I think this would be a much better solution. May I try and implement inline conversion instead? Would you be OK with that?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented Jun 6, 2022

Copy link
Copy Markdown
Member

Yes of course @ibogdanchikov. Feel free to try the other approach. Let us know if you have any questions

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ibogdanchikov
ibogdanchikov force-pushed the support-java-optional branch 2 times, most recently from 9c4b87e to 95725b1 Compare June 7, 2022 10:54
@ibogdanchikov

ibogdanchikov commented Jun 7, 2022

Copy link
Copy Markdown
Contributor Author

@filiphr I've reimplemented Optional mapping using inline conversion. Could you please take a look?

jlamb1 and peter-mghendi reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jlamb1

jlamb1 commented Jul 21, 2022

Copy link
Copy Markdown

thank you for taking this on! @filiphr do you mind looking over this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented Jul 24, 2022

Copy link
Copy Markdown
Member

I've started looking into this and we want and will have this in 1.6. One thing I want to fix is the null checks that are being done on Optional properties. We shouldn't be doing that, and I am not sure how easy it would be to achieve this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ibogdanchikov

ibogdanchikov commented Aug 13, 2022

Copy link
Copy Markdown
Contributor Author

@filiphr Could you please elaborate on how would you like to fix the null checks? I would be very grateful if you could provide trivial examples showing in what ways the generated code would look wrong and how would you like it to look. Thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented Aug 20, 2022

Copy link
Copy Markdown
Member

Sorry for taking so long time @ibogdanchikov.

What I basically would like to see is instead of:

if ( source.getFromOptionalProp() != null ) {
    target.setFromOptionalProp( source.getFromOptionalProp().orElse( null ) );
}

I would like to see

if ( source.getFromOptionalProp().isPresent() ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}

or if we had some out-of-the-box presence check method for optionals:

if ( isPresent( source.getFromOptionalProp() ) ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ibogdanchikov
mapstruct#674 Support Java 8 java.util.Optional type
765de64
@ibogdanchikov
ibogdanchikov force-pushed the support-java-optional branch from 95725b1 to 765de64 Compare August 27, 2022 14:09
@ibogdanchikov

ibogdanchikov commented Aug 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@filiphr Thank you very much for the examples, they were very helpful! I adjusted the implementation according to these examples.

Specifically, I added the following condition to CommonMacros.ftl:

<#if getSourceType().isOptionalType()>.isPresent()<#else> != null</#if>

and changed OptionalToObjectConversion.getToExpression to "<SOURCE>.get()", so now it generates the following code:

if ( source.getFromOptionalProp().isPresent() ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}

Please let me know if this solution is acceptable. Thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented Aug 28, 2022

Copy link
Copy Markdown
Member

This looks better @ibogdanchikov. Thanks a lot. I'll get your branch and play around with it before merging it

ibogdanchikov, Gabriele-Tomberli, daviian, and yufooi reacted with thumbs up emoji
All reactions
  • 👍 4 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Tristan107

Tristan107 commented Apr 5, 2023

Copy link
Copy Markdown

Hi @filiphr, did you find something blocking the merge of this feature ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr filiphr mentioned this pull request Apr 15, 2023
Support Java 8 java.util.Optional type #674
Closed
@filiphr

filiphr commented Apr 22, 2023

Copy link
Copy Markdown
Member

Hi @filiphr, did you find something blocking the merge of this feature ?

Unfortunately, I haven't had the time I want to look better into this one. Recently there was another PR (#3183) for this functionality. So I'll need to analyze both and see what works best for MapStruct

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr

filiphr commented May 1, 2023

Copy link
Copy Markdown
Member

I had some time to look into this and #3183. The approach which is taken in #3183 is more flexible and allows mapping between different types e.g. Optional<Integer> -> Optional<String>. However, in this PR that doesn't work. Therefore, I am going to close this in favour of #3183. I am really sorry for this @ibogdanchikov, I hope you understand.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@filiphr filiphr closed this May 1, 2023
@MakraGitHub

MakraGitHub commented Jun 1, 2024

Copy link
Copy Markdown

Optional selectByEmail(String email) does not support constructor in spring boot myBatis query
How to fixed it>

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ibogdanchikov @filiphr @jlamb1 @Tristan107 @MakraGitHub
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.