-
-
Notifications
You must be signed in to change notification settings - Fork 1k
#674 Support for native Java 8 Optional mapping #3183
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
Open
ro0sterjam
wants to merge
17
commits into
mapstruct:main
Choose a base branch
from
ro0sterjam:rj/support-for-optionals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9cf813b
#674 Support for native Java 8 Optional mapping
ro0sterjam 71c87b3
fix formatting
ro0sterjam 2dd531b
Default to `Optional.empty() when null checking
ro0sterjam 966c4ad
generated tests
ro0sterjam 9309c38
optional check alongside null check
ro0sterjam 35fd4aa
public field tests
ro0sterjam 2f1cb00
Before/After mapping
ro0sterjam 49cd50c
Tests to clarify behaviour
ro0sterjam 129b07d
negate support
ro0sterjam c9b551a
map to default for all types
ro0sterjam 0e28324
more test
ro0sterjam 262b608
nested optional support
ro0sterjam 9e969e7
formatting
ro0sterjam c7e7ed9
generated sources
ro0sterjam 56cf074
formatting
ro0sterjam 563afa3
always return Optional.empty() for inner maps
ro0sterjam 087ddc8
javadoc
ro0sterjam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
processor/src/main/java/org/mapstruct/ap/internal/model/InitDefaultValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.internal.model; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.mapstruct.ap.internal.model.common.ModelElement; | ||
| import org.mapstruct.ap.internal.model.common.Type; | ||
|
|
||
| /** | ||
| * Model element to generate code that initializes a default value for a given type. | ||
| * | ||
| * <p>Uses the provided factory method if available. | ||
| * Otherwise, constructs the target object using a sensible default including but not limited to: | ||
| * - an empty array for array types | ||
| * - an empty collection for collection types | ||
| * - an empty optional for optional types | ||
| * - using the public default constructor for other types if available | ||
| * | ||
| * <p>If no default value can be constructed, a null is returned instead. | ||
| * TODO: Consider throwing an exception instead of returning null. | ||
| * | ||
| * @author Ken Wang | ||
| */ | ||
| public class InitDefaultValue extends ModelElement { | ||
|
|
||
| private final Type targetType; | ||
| private final MethodReference factoryMethod; | ||
|
|
||
| public InitDefaultValue(Type targetType, MethodReference factoryMethod) { | ||
| this.targetType = targetType; | ||
| this.factoryMethod = factoryMethod; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Type> getImportTypes() { | ||
| Set<Type> types = new HashSet<>(); | ||
| types.add( targetType ); | ||
| return types; | ||
| } | ||
|
|
||
| public Type getTargetType() { | ||
| return targetType; | ||
| } | ||
|
|
||
| public MethodReference getFactoryMethod() { | ||
| return factoryMethod; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
processor/src/main/java/org/mapstruct/ap/internal/model/OptionalMappingMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.internal.model; | ||
|
|
||
| import org.mapstruct.ap.internal.model.common.Assignment; | ||
| import org.mapstruct.ap.internal.model.common.Type; | ||
| import org.mapstruct.ap.internal.model.source.Method; | ||
| import org.mapstruct.ap.internal.model.source.SelectionParameters; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import static org.mapstruct.ap.internal.util.Collections.first; | ||
|
|
||
| /** | ||
| * Maps from a source to a target where one or the other (or both) are an {@link Optional} type. | ||
| * | ||
| * @author Ken Wang | ||
| */ | ||
| public class OptionalMappingMethod extends ContainerMappingMethod { | ||
|
|
||
| public static class Builder extends ContainerMappingMethodBuilder<Builder, OptionalMappingMethod> { | ||
|
|
||
| public Builder() { | ||
| super( Builder.class, "optional element" ); | ||
| } | ||
|
|
||
| @Override | ||
| protected Type getElementType(Type parameterType) { | ||
| if ( parameterType.isOptionalType() ) { | ||
| return parameterType.getTypeParameters().get( 0 ); | ||
| } | ||
| else { | ||
| return parameterType; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected Assignment getWrapper(Assignment assignment, Method method) { | ||
| return assignment; | ||
| } | ||
|
|
||
| @Override | ||
| protected OptionalMappingMethod instantiateMappingMethod(Method method, Collection<String> existingVariables, | ||
| Assignment assignment, MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName, | ||
| List<LifecycleCallbackMethodReference> beforeMappingMethods, | ||
| List<LifecycleCallbackMethodReference> afterMappingMethods, SelectionParameters selectionParameters) { | ||
| return new OptionalMappingMethod( | ||
| method, | ||
| getMethodAnnotations(), | ||
| existingVariables, | ||
| assignment, | ||
| factoryMethod, | ||
| mapNullToDefault, | ||
| loopVariableName, | ||
| beforeMappingMethods, | ||
| afterMappingMethods, | ||
| selectionParameters | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private OptionalMappingMethod(Method method, | ||
| List<Annotation> annotations, | ||
| Collection<String> existingVariables, | ||
| Assignment parameterAssignment, | ||
| MethodReference factoryMethod, | ||
| boolean mapNullToDefault, | ||
| String loopVariableName, | ||
| List<LifecycleCallbackMethodReference> beforeMappingReferences, | ||
| List<LifecycleCallbackMethodReference> afterMappingReferences, | ||
| SelectionParameters selectionParameters) { | ||
| super( | ||
| method, | ||
| annotations, | ||
| existingVariables, | ||
| parameterAssignment, | ||
| factoryMethod, | ||
| mapNullToDefault, | ||
| loopVariableName, | ||
| beforeMappingReferences, | ||
| afterMappingReferences, | ||
| selectionParameters | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Type> getImportTypes() { | ||
| Set<Type> types = super.getImportTypes(); | ||
|
|
||
| types.add( getSourceElementType() ); | ||
| return types; | ||
| } | ||
|
|
||
| public Type getSourceElementType() { | ||
| Type sourceParameterType = getSourceParameter().getType(); | ||
|
|
||
| if ( sourceParameterType.isOptionalType() ) { | ||
| return first( sourceParameterType.determineTypeArguments( Optional.class ) ).getTypeBound(); | ||
| } | ||
| else { | ||
| return sourceParameterType; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Type getResultElementType() { | ||
| Type resultParameterType = getResultType(); | ||
|
|
||
| if ( resultParameterType.isOptionalType() ) { | ||
| return first( resultParameterType.determineTypeArguments( Optional.class ) ); | ||
| } | ||
| else { | ||
| return resultParameterType; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
?here to represent anOptionaltypeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to see how this will look like in the error. I think that it would be something like
customer?.address?.valueif customer and address are optional. I think that looks nice. Perhaps an example as a comment here to make it easy to understand 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to admit, I'm not certain how to trigger an error that will print out this forge history. Would you have any guidance on the simplest way to do so? Once I can see what it looks like, I can add the comment.