-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
I think I already saw some questions to this topic here or on stackoverflow and in our projects I sometimes missed a feature like this as well:
Mapping iterables will right now always map all iterables from A to B. It is often helpful to remove some entries in the result B. Right now this can be achieved with e. g. an @AfterMapping that removes these entries or create an own mapping method.
Allowing the user specifying some conditions would be helpful to let mapstruct still do most of the magic.
Some examples:
// E1
@IterableMapping(condition = "java( sourceValue != null && sourceValue.startsWith(\"1\") )")
List<Integer> mapIntegersFilteredBySourceValue(List<String> source);
// E2
@IterableMapping(condition = "java( targetValue != null && targetValue < 5 )")
List<Integer> mapIntegersFilteredByTargetValue(List<String> source);
// E3
@IterableMapping(conditionQualifiedByName = "filterString")
List<Integer> mapIntegersFilteredBySourceValueInExtraMethod(List<String> source);
@Named("filterString")
boolean filterString(String sourceValue) {
return sourceValue != null && sourceValue.startsWith("1");
}
// E4
@IterableMapping(conditionQualifiedByName = "filterInteger")
List<Integer> mapIntegersFilteredByTargetValueInExtraMethod(List<String> source);
@Named("filterInteger")
boolean filterInteger(Integer targetValue) {
return targetValue != null && targetValue < 5;
}conditionQualifiedBy annotation should be supported as well. And same things should be available for @MapMapping - here it should be possible to filter by key or value.
Things that should be clarified:
- When providing a condition as an inline expression, what variable names should be available?
- There should be a way to filter before and after mapping the value, how to determine at which point the user would like to have his condition? Rely on variable names? Dividing the attributes in something like
sourceConditionandtargetCondition? Some other idea? - Name it
condition(thus add entry if result istrue) orfilter(thus remove entry if result istrue)?
WDYT?
(I wasn't able to find an issue with the same topic.. even if I think I read something like this somewhere...)