GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing github.com — Add support for conditions on source parameters + fix incorrect use of source parameter in presence check method by filiphr · Pull Request #3543 · mapstruct/mapstruct Conversation
Member
This commit does a backwards incompatible change in regard to how presence check methods are used when only the source parameter is needed for the presence check. e.g. @Condition public static boolean shouldMap(Employee employee) { // ... } is now only applied to source parameters or a property of type @Condition public static boolean shouldMap(Employee employee, @TargetPropertyName String targetPropertyName) { // ... } Instead of the Fixes #2610 NB: Perhaps we need to add some more tests for #3459 and #3270, I wanted to first see if this idea is worth pursuing.An alternative that we could do about the use of the right parameter would be if we introduce something like
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
|
Sorry, I haven't had the time and mental capacity to think this through. Users would now have to provide the So I would therefore prefer the second approach with the specific annotation, as you said "something like Whichever way we go, users will still wonder why this is needed (e.g. "why do I have to add something extra?"). So this should be well documented in the docs and release notes. I just played with your branch and saw that a condition written in the old way is simply not used. Is it possible to throw a compile error (or a warning)? (You have better insight into the underlying code) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
No rush, I've been working on this one for a while, it took me a bit of time to wrap my head properly around it as well. Thanks a lot for going through it.
That was my gut feeling as well, or perhaps have a different annotation that would be applicable for source parameters condition check.
I didn't like that at all as well. I'll look into an alternative for this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@thunderhook, I gave another go at this. Basically, there is a new |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I really like this approach. One thing I noticed while playing with the branch: Is there anything against a condition method being a With the current implementation, the method won't be used if you add a I haven't tried it explicitly, but I don't think it would be picked up as a presence check method either. Otherwise, I really like it! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
You are indeed right. It won't be caught due to the I'll make sure that it works in that case as well. I should also add some error handlings:
An alternative to the public enum ConditionStrategy { PROPERTIES, SOURCE_PARAMETERS, } then the condition would be @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.CLASS) public @interface Condition { ConditionStrategy[] appliesTo() default ConditionStrategy.PROPERTIES; } it can then be used
I am not sure which approach I like more. What do you think @thunderhook |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I haven't fully thought this through, but I think the second approach might be more flexible when introducing conditions for other scenarios. Like collection items #1610 or If there are other strategies added, then it might make sense to provide a Another idea might be to support both approaches. Where @Condition( appliesTo = ConditionStrategy.SOURCE_PARAMETERS ) public @interface SourceCondition { } This would allow users to write their own Condition meta annotations, like: @Condition( appliesTo = { ConditionStrategy.SOURCE_PARAMETERS, ... } ) public @interface MyOwnEntityCondition { } However, this may cause problems if multiple annotations are set. Should it cause a compile error? If not, should they be combined? What has higher priority? @SourceCondition @Condition(appliesTo = { ConditionStrategy.PROPERTIES, ConditionStrategy.SOURCE_PARAMETERS}) Target map(Source source); So maybe it's better to stick to one of these approaches rather than a combination of the two. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Really good points @thunderhook. It would indeed work nicely for #1610 as well. That would basically be an additional
I liked this even more. We could indeed provide
I don't think that this would cause problems. We would basically just merge all of the applicable Thanks a lot for the feedback @thunderhook, I'll try to rework this in that direction :). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@thunderhook I finally found the time to go over what we discussed last time. Can you please have a look at this and let me know what you think |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@filiphr I really like it. Just some small nitpicks.
The documentation can also be improved in another branch, if you want to merge it sooner than later.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…f source parameter in presence check method This commit does a backwards incompatible change in regard to how presence check methods are used when only the source parameter is needed for the presence check. e.g. ```java @condition public static boolean shouldMap(Employee employee) { // ... } ``` is now only applied to source parameters or a property of type `Employee`. The method is no longer applied to all the parameters of a mapping method with a source parameter `Employee`. If you still want to do that then the method should be adapted ```java @condition public static boolean shouldMap(Employee employee, @TargetPropertyName String targetPropertyName) { // ... } ``` Instead of the `@TargetPropertyName` it can be `@SourcePropertyName`.
c82d218 to
64862c3
Compare
|
Thanks a lot for the review @thunderhook. All your comments make a lot of sense. I've added some docs, added a new test and also added the class to the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@filiphr Yes, I think this is enough. Thanks for your hard work on this. 👍 Merge at your own will!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for the review @thunderhook |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Method with@Condition acts strange when applying to source
@Condition badly implemented
Support conditional mapping for source parameters
There was an error while loading. Please reload this page.