-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adding cond mapping #2148
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
Closed
Closed
Adding cond mapping #2148
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
638e132
#2139 reproducer (#2140)
sjaakd b323c36
#2135 improved messages for not able to select qualified method (#2141)
sjaakd 05977a0
Adding cond mapping
Desislav-Petrov b6dad04
Merge branch 'master' of https://github.com/mapstruct/mapstruct
Desislav-Petrov fbc1dab
Adding checks for return type
Desislav-Petrov 861943d
Add missing headers
Desislav-Petrov ec5339d
Style fixes
Desislav-Petrov 3b0e722
Merge branch 'master' of https://github.com/mapstruct/mapstruct
Desislav-Petrov f609bec
Using MethodSelectors
Desislav-Petrov 9eee14e
Merge branch 'master' of https://github.com/mapstruct/mapstruct
Desislav-Petrov 151b8b2
remove prev ignore that was needed to make the build pass
Desislav-Petrov 1670ca5
Add ignore
Desislav-Petrov 4ac8c74
Update copyright list
Desislav-Petrov e6a90e6
Update core/src/main/java/org/mapstruct/Mapping.java
Desislav-Petrov 1dd10cb
Revert ignore
Desislav-Petrov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ | |
| import static org.mapstruct.ap.internal.util.Message.GENERAL_CONSTRUCTOR_PROPERTIES_NOT_MATCHING_PARAMETERS; | ||
| import static org.mapstruct.ap.internal.util.Message.PROPERTYMAPPING_CANNOT_DETERMINE_SOURCE_PARAMETER_FROM_TARGET; | ||
| import static org.mapstruct.ap.internal.util.Message.PROPERTYMAPPING_CANNOT_DETERMINE_SOURCE_PROPERTY_FROM_TARGET; | ||
| import static org.mapstruct.ap.internal.util.Message.PROPERTYMAPPING_CANNOT_RESOLVE_CONDITION_METHOD; | ||
|
|
||
| /** | ||
| * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one bean type to another, optionally | ||
|
|
@@ -1147,6 +1148,7 @@ else if ( mapping.getJavaExpression() != null ) { | |
| .defaultJavaExpression( mapping.getDefaultJavaExpression() ) | ||
| .mirror( mapping.getMirror() ) | ||
| .options( mapping ) | ||
| .condition( mapping.getCondition() ) | ||
| .build(); | ||
| handledTargets.add( targetPropertyName ); | ||
| unprocessedSourceParameters.remove( sourceRef.getParameter() ); | ||
|
|
@@ -1181,6 +1183,27 @@ else if ( mapping.getJavaExpression() != null ) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| //check if there's a condition and if the method exists | ||
| if ( mapping.getCondition() != null ) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried to do this check in a few different places but this one seems to be the most appropriate one. Could somebody please confirm? If so, I'll add a check for the conditional method param type. |
||
| List<SelectedMethod<SourceMethod>> matchingMethods = | ||
| ConditionMethodResolver.getConditionalMappingMethods( | ||
| method, mapping.getSelectionParameters(), mapping.getCondition(), ctx ); | ||
|
|
||
| if ( matchingMethods.size() != 1 ) { | ||
| ctx.getMessager() | ||
| .printMessage( | ||
| method.getExecutable(), | ||
| mapping.getMirror(), | ||
| mapping.getTargetAnnotationValue(), | ||
| PROPERTYMAPPING_CANNOT_RESOLVE_CONDITION_METHOD, | ||
| mapping.getCondition(), | ||
| targetPropertyName | ||
| ); | ||
| errorOccured = true; | ||
| } | ||
| } | ||
|
|
||
| // remaining are the mappings without a 'source' so, 'only' a date format or qualifiers | ||
| if ( propertyMapping != null ) { | ||
| propertyMappings.add( propertyMapping ); | ||
|
|
||
82 changes: 82 additions & 0 deletions
82
processor/src/main/java/org/mapstruct/ap/internal/model/ConditionMethodResolver.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,82 @@ | ||
| /* | ||
| * 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.source.Method; | ||
| import org.mapstruct.ap.internal.model.source.ParameterProvidedMethods; | ||
| import org.mapstruct.ap.internal.model.source.SelectionParameters; | ||
| import org.mapstruct.ap.internal.model.source.SourceMethod; | ||
| import org.mapstruct.ap.internal.model.source.selector.MethodSelectors; | ||
| import org.mapstruct.ap.internal.model.source.selector.SelectedMethod; | ||
| import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| /** | ||
| * Factory for creating conditional mapping resolvers | ||
| * | ||
| * @author Desislav Petrov | ||
| */ | ||
| public final class ConditionMethodResolver { | ||
|
|
||
| private ConditionMethodResolver() { | ||
| } | ||
|
|
||
| public static List<SelectedMethod<SourceMethod>> getConditionalMappingMethods( | ||
| Method method, | ||
| SelectionParameters selectionParameters, | ||
| String conditionalMethodName, | ||
| MappingBuilderContext ctx) { | ||
|
|
||
| MethodSelectors selectors = new MethodSelectors( | ||
| ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getTypeFactory(), ctx.getMessager() ); | ||
|
|
||
| return filterByName( selectors.getMatchingMethods( | ||
| method, | ||
| getAllAvailableMethods( method, ctx.getSourceModel() ), | ||
| Arrays.asList( method.getSourceParameters().get( 0 ).getType() ), | ||
| ctx.getTypeFactory().getType( Boolean.class ), | ||
| SelectionCriteria. | ||
| forMappingMethods( selectionParameters, null, null, false ) ), conditionalMethodName ); | ||
| } | ||
|
|
||
| private static List<SelectedMethod<SourceMethod>> filterByName( | ||
| List<SelectedMethod<SourceMethod>> source, | ||
| String conditionalMethodName) { | ||
|
|
||
| if ( source == null || source.isEmpty() ) { | ||
| return source; | ||
| } | ||
| else { | ||
| String[] split = conditionalMethodName.split( "\\." ); | ||
| return source.stream().filter( e -> e.getMethod().getName().equals( split[split.length - 1] ) ) | ||
| .collect( toList() ); | ||
| } | ||
| } | ||
|
|
||
| private static List<SourceMethod> getAllAvailableMethods( Method method, List<SourceMethod> sourceModelMethods ) { | ||
| ParameterProvidedMethods contextProvidedMethods = method.getContextProvidedMethods(); | ||
| if ( contextProvidedMethods.isEmpty() ) { | ||
| return sourceModelMethods; | ||
| } | ||
|
|
||
| List<SourceMethod> methodsProvidedByParams = contextProvidedMethods | ||
| .getAllProvidedMethodsInParameterOrder( method.getContextParameters() ); | ||
|
|
||
| List<SourceMethod> availableMethods = | ||
| new ArrayList<>( methodsProvidedByParams.size() + sourceModelMethods.size() ); | ||
|
|
||
| availableMethods.addAll( methodsProvidedByParams ); | ||
| availableMethods.addAll( sourceModelMethods ); | ||
|
|
||
| return availableMethods; | ||
| } | ||
|
|
||
| } |
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
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.
The example that you are using from the
defaultExpressionis not something valid forcondition.I would suggest the following:
Example:
generates:
And the condition method should be defined as:
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'm not sure that this makes it really flexible:
target="name", condition="isNotBlack" always assumes that the condition will be based on the source's corresponding field but this might not be the case.
It would be much more flexible if the condition method is of the form:
boolean someName(SourceObject type)
Let me know what do you think?
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 can follow both ideas. I like having the flexibility @Desislav-Petrov proposes with getting the whole source object to do some checks on this one.
But keeping it strict to the value to which the check is assigned as @filiphr proposes makes it easier to reuse the condition methods. Sticking to the
isNotBlankexample: This could be used for all String source values, getting the whole source type will make it more complicated to reuse it for other source values.One idea to support both: Default to @filiphr proposals and if there is an (not annotated) argument for the method this will be the source value.
And also support if an argument was annotated with
@MappingSourcethe source object will be used (do we already have an annotation like this? tbh I dont remember, just can find@MappingTargetthat maybe could also be supported?!)