You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide mapping into java.util.Map using enum values as key #717
I'd like to map the fields of a bean class into a dictionary-like class, using MapStruct. My source class is a standard bean (simplified example):
public class Bean {
private String a;
private String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
Now I want to map these fields into a Map-like container:
public class Dict {
public enum Tag {
A,
B
}
private Map<Tag, String> dict = new HashMap<>();
public String getEntry(Tag tag) {
return dict.get(tag);
}
public void setEntry(Tag tag, String s) {
dict.put(tag, s);
}
}
In other words, I'd like MapStruct to generate something along the lines of:
I couldn't find anything similar in the MapStruct documentation. There is much flexibility for getting at mapping sources (nested sources, expressions), but for targets I can see only the target = "propertyname" notation which doesn't leave much room for flexibility.
(original question from stackoverflow, added as issue as requested by Gunnar)
If this feature is added I would suggest that the other way around should also be done. Meaning: Map -> Bean , Set -> Bean
java.util.Map<String, String> myMap(); with entry {"last_name","some name"}
public class Bean{
String firstName;
String lastName;
// getters + setters
}
@Mapper
public interface Mapper{
@Mapping(source="last_name", target="lastName")
public Bean maptoBean (java.util.Map<String,String> myMap)
}
additionally would be very usefull to use for HttpServletRequest e.g. Map<String, String[]> parameters = request.getParameterMap();
or other combinations like Map<String, List<String>>, ...
Reacted by Artur Polit, carlosgarbiatti, tong, buwoyouwo, Anastasia Krasavina, tinnapat and Gabriel Rodriguez
People are supposed to be using https://github.com/FasterXML/jackson for that purpose. Never tested it!
It would be great to have MapStruct builtin feature.
Activity
steirer00 commentedon Mar 16, 2016
If this feature is added I would suggest that the other way around should also be done. Meaning: Map -> Bean , Set -> Bean
java.util.Map<String, String> myMap();with entry {"last_name","some name"}additionally would be very usefull to use for HttpServletRequest e.g.
Map<String, String[]> parameters = request.getParameterMap();or other combinations like
Map<String, List<String>>, ...tomliuxd commentedon Aug 26, 2016
I am looking for something similar (if not the same)
for example
public class ClassA {
Map<String, String> properties;
}
public class ClassB {
String serverName;
}
@mapping(target="properties(key='server name')", source="serverName")
public ClassA b2a (ClassB b);
@mapping(target="serverName", source="properties(key='server name'")
public ClassB a2b (ClassA a);
apolit commentedon Aug 29, 2016
Similar functionality is available in Dozer: http://dozer.sourceforge.net/documentation/mapbackedproperty.html
It would be great to have it in MapStruct
tomliuxd commentedon Aug 29, 2016
actually we are using dozer now. but its performance is not good. we are considering to move to either mapstruct or other tools.
pehunka commentedon Mar 20, 2017
I would like to see this feature in MapStruct as well, if possible.
wmelon84 commentedon Mar 13, 2019
People are supposed to be using https://github.com/FasterXML/jackson for that purpose. Never tested it!
It would be great to have MapStruct builtin feature.
TOlchawa commentedon Apr 7, 2022
Instead of
targetuse (new)key(by defaultString)- something like:where
FooisIgnore duplicated keys / add flag
duplicatesto not ignore: