Mapstruct: Provide mapping into java.util.Map using enum values as key

Created on 4 Jan 2016  路  6Comments  路  Source: mapstruct/mapstruct

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:

    target.setEntry(Dict.Tag.A, source.getA());
    target.setEntry(Dict.Tag.B, source.getB());

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)

feature

Most helpful comment

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>>, ...

All 6 comments

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>>, ...

I am looking for something similar (if not the same)

1) Mapping bean property to target map with given key name
2) Mapping target map with given key name to bean property

for example

public class ClassA {
Map 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);

Similar functionality is available in Dozer: http://dozer.sourceforge.net/documentation/mapbackedproperty.html
It would be great to have it in MapStruct

actually we are using dozer now. but its performance is not good. we are considering to move to either mapstruct or other tools.

I would like to see this feature in MapStruct as well, if possible.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PFStruct picture PFStruct  路  5Comments

jrcacd picture jrcacd  路  4Comments

imtiazShakil picture imtiazShakil  路  4Comments

thiagogjt picture thiagogjt  路  7Comments

amineRS2i picture amineRS2i  路  5Comments