-
-
Notifications
You must be signed in to change notification settings - Fork 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
Add support for conditions on source parameters + fix incorrect use of source parameter in presence check method #3543
Conversation
|
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) |
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 |
|
@thunderhook, I gave another go at this. Basically, there is a new |
|
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! |
|
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 |
|
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. |
|
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 :). |
|
@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 |
thunderhook
left a comment
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.
@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.
…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 |
thunderhook
left a comment
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.
@filiphr Yes, I think this is enough. Thanks for your hard work on this. 👍 Merge at your own will!
|
Thanks for the review @thunderhook |
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.
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 parameterEmployee. If you still want to do that then the method should be adaptedInstead of the
@TargetPropertyNameit can be@SourcePropertyName.Fixes #2610
Fixes #3459
Fixes #3270
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
@SourceParameterand / or@TargetProperty. This people can explicitly decide if they want to apply a conditional method only on a source parameter or only on a target property. The 2 new annotations might also be used in other places