-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for conditions on source parameters + fix incorrect use of source parameter in presence check method #3543
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
Merged
filiphr
merged 7 commits into
mapstruct:main
from
filiphr:2610-source-parameter-presenc-check
Apr 29, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2630ea5
Add support for conditions on source parameters + fix incorrect use o…
filiphr 50f089b
Use new SourceCondition
filiphr 3bb3735
Use ConditionStrategy
filiphr b98ac32
Cleanups
filiphr ebf7362
Polishing
filiphr 64862c3
Apply review feedback
filiphr 4834ca5
Remove unused import
filiphr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct; | ||
|
|
||
| /** | ||
| * Strategy for defining what to what a condition (check) method is applied to | ||
| * | ||
| * @author Filip Hrisafov | ||
| * @since 1.6 | ||
| */ | ||
| public enum ConditionStrategy { | ||
| /** | ||
| * The condition method should be applied whether a property should be mapped. | ||
| */ | ||
| PROPERTIES, | ||
| /** | ||
| * The condition method should be applied to check if a source parameters should be mapped. | ||
| */ | ||
| SOURCE_PARAMETERS, | ||
| } |
74 changes: 74 additions & 0 deletions
74
core/src/main/java/org/mapstruct/SourceParameterCondition.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,74 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation marks a method as a <em>check method</em> to check if a source parameter needs to be mapped. | ||
| * <p> | ||
| * By default, source parameters are checked against {@code null}, unless they are primitives. | ||
| * <p> | ||
| * Check methods have to return {@code boolean}. | ||
| * The following parameters are accepted for the presence check methods: | ||
| * <ul> | ||
| * <li>The mapping source parameter</li> | ||
| * <li>{@code @}{@link Context} parameter</li> | ||
| * </ul> | ||
| * | ||
| * <strong>Note:</strong> The usage of this annotation is <em>mandatory</em> | ||
| * for a method to be considered as a source check method. | ||
| * | ||
| * <pre><code class='java'> | ||
| * public class PresenceCheckUtils { | ||
| * | ||
| * @SourceParameterCondition | ||
| * public static boolean isDefined(Car car) { | ||
| * return car != null && car.getId() != null; | ||
| * } | ||
| * } | ||
| * | ||
| * @Mapper(uses = PresenceCheckUtils.class) | ||
| * public interface CarMapper { | ||
| * | ||
| * CarDto map(Car car); | ||
| * } | ||
| * </code></pre> | ||
| * | ||
| * The following implementation of {@code CarMapper} will be generated: | ||
| * | ||
| * <pre><code class='java'> | ||
| * public class CarMapperImpl implements CarMapper { | ||
| * | ||
| * @Override | ||
| * public CarDto map(Car car) { | ||
| * if ( !PresenceCheckUtils.isDefined( car ) ) { | ||
| * return null; | ||
| * } | ||
| * | ||
| * CarDto carDto = new CarDto(); | ||
| * | ||
| * carDto.setId( car.getId() ); | ||
| * // ... | ||
| * | ||
| * return carDto; | ||
| * } | ||
| * } | ||
| * </code></pre> | ||
| * | ||
| * @author Filip Hrisafov | ||
| * @since 1.6 | ||
| * @see Condition @Condition | ||
| */ | ||
| @Target({ ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.CLASS) | ||
| @Condition(appliesTo = ConditionStrategy.SOURCE_PARAMETERS) | ||
| public @interface SourceParameterCondition { | ||
|
filiphr marked this conversation as resolved.
|
||
|
|
||
| } | ||
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
15 changes: 15 additions & 0 deletions
15
processor/src/main/java/org/mapstruct/ap/internal/gem/ConditionStrategyGem.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,15 @@ | ||
| /* | ||
| * 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.gem; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public enum ConditionStrategyGem { | ||
|
|
||
| PROPERTIES, | ||
| SOURCE_PARAMETERS | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.