Replies: 2 comments 6 replies
-
I don't like to introduce something like What I am open to consider is to provide special handling for mapping from Perhaps something in the form of: @Mapper
public interface MyMapper {
List<FlattenedTarget> map(Map<String, Source> source);
@Mapping(target = "day", source = "key")
@Mapping(target = ".", source = "value")
FlattenedTarget map(Map.Entry<String, Source> entry);
} The second example will look like: @Mapper
public interface MyMapper {
List<FlattenedTarget> map(Map<String, Source> source);
@Mapping(target = "day", source = "key")
@Mapping(target = "times", source = "value")
FlattenedTarget map(Map.Entry<String, Source> entry);
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
If it's a 1 to 1 mapping, you don't have to use Map.Entry, this would be cleaner. @Mapper
public interface MapMapper {
List<Target> map(Map<SourceKey, SourceValue> map);
// mapping annotations
Target map(SourceKey key, SourceValue value);
}
@Mapper
public interface ReverseMapMapper {
Map<TargetKey, TargetValue> map(List<Source> list);
// mapping annotations
TargetKey key(Source source);
// mapping annotations
TargetValue value(Source source);
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I sometimes want to flatten a map into a list (and vice versa), e.g. when I'm converting from different representations (e.g. Avro to/from application domain). I have to do the entire mapping with a
default
method on my mapper interface, duplicating some logic that MapStruct could have taken care of for me.There are two general use cases I run into.
Flattening a multi-value map
Target:
Here I wish I could do something like
@Mapping(target = "schedule", source = "flatSchedule", flatKey = "day")
. which would also work ifsource
andtarget
were flipped.Flattening a single-value map
Source:
Target:
This example is similar, except here the value in the source map is not a bean, so we need to introduce a key for it in the target map. Here I wish I could do
@Mapping(target = "schedule", source = "flatSchedule", flatKey = "day", flatValue = "times")
.Would this be worth implementing in MapStruct?
Beta Was this translation helpful? Give feedback.
All reactions