Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pe-st opened this issue Jan 4, 2016 · 7 comments
Open

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

pe-st opened this issue Jan 4, 2016 · 7 comments
Labels

Comments

@pe-st
Copy link

pe-st commented Jan 4, 2016

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)

@steirer00
Copy link

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

@tomliuxd
Copy link

tomliuxd commented Aug 26, 2016

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

@apolit
Copy link

apolit commented 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
Copy link

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

@pehunka
Copy link

pehunka commented Mar 20, 2017

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

@wmelon84
Copy link

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
Copy link

TOlchawa commented Apr 7, 2022

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
Labels
Projects
None yet
Development

No branches or pull requests

8 participants