Skip to content

Provide mapping into java.util.Map using enum values as key #717

Open

Description

@pe-st

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)

Activity

  1. steirer00 commented on Mar 16, 2016

    @steirer00

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

  2. tomliuxd commented on Aug 26, 2016

    @tomliuxd

    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<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);

  3. apolit commented on Aug 29, 2016

    @apolit

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

  4. tomliuxd commented on Aug 29, 2016

    @tomliuxd

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

  5. pehunka commented on Mar 20, 2017

    @pehunka

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

  6. wmelon84 commented on Mar 13, 2019

    @wmelon84

    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.

  7. TOlchawa commented on Apr 7, 2022

    @TOlchawa

    Instead of target use (new) key (by default String)- something like:

    @Mapper
    public interface Mapper{
    
    @Mapping(key="key1", source="bar")
    @Mapping(key="key2", source="i") // Boxing 
    @Mapping(key="key3", expression="java(obj.toString())")
    public java.util.Map<String,String> beanToMap(Foo foo);
    
    }
    

    where Foo is

    class Foo {
        String bar;
        int i;
        Object obj;
    }
    

    Ignore duplicated keys / add flag duplicates to not ignore:

    @Mapper(duplicates = MapKeysDuplicatedStrategy.ERROR)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions