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

Skip to content

[Feature] Mapping hashmap from a field #1218

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
DavidWylie opened this issue Jun 1, 2017 · 13 comments
Open

[Feature] Mapping hashmap from a field #1218

DavidWylie opened this issue Jun 1, 2017 · 13 comments

Comments

@DavidWylie
Copy link

When using the mapstruct library for mapping dto objects to domain objects a common problem encountered is the ability to map Hashmaps to object properties.
Existing code to acheive this is to use a custom function which will get a hashmap index from the map and return it.

  @Mapper()
public abstract class OrderMapper {

@Mappings({
        @Mapping(target="id", expression="java( getIdFlag(order, \"id\"))"),
})
abstract Order toDomainOrder order);

}

proposed syntax.

 @Mapper()
public abstract class OrderMapper {

@Mappings({
        @Mapping(target="id", expression="ids[id]"),
})
abstract Order toDomainOrder order);

}

is this something already being investigated or considered?

@filiphr filiphr added the feature label Jun 1, 2017
@filiphr filiphr added this to the Future planning milestone Jun 1, 2017
@filiphr
Copy link
Member

filiphr commented Jun 1, 2017

Thanks for the feature request @DavidWylie. I think that this one is similar to #1075 and #717. I think we had one other feature request that was requesting to provide the index of a list.

I think that it is an interesting feature for MapStruct. If you are up for it you can try contributing it 😄. We are here of course for guidance and help.

@gunnarmorling
Copy link
Member

@DavidWylie, your source listing seems to miss some characters, so just to double check: you mean to have a Map of some kind as the source parameter in these examples?

@slukes
Copy link

slukes commented Feb 20, 2019

I would like this feature too and as such I am willing to donate some time to do implement it. Any objections or can I get stuck straight in?

@sjaakd
Copy link
Contributor

sjaakd commented Feb 22, 2019

@filiphr .. I think this is is interesting.. @slukes .. What did you have in mind?

@filiphr
Copy link
Member

filiphr commented Feb 23, 2019

I also think that this is interesting. I am only not sure about which approach is good to take. In any case @slukes I think that you can work on this if you want to. However, before starting I would suggest that you outline how the API would look like.

If you ask me doing something like:

@Mapping(target = "target", source = "map[id]")

might be the way to go. The only thing is do we do this only for Map<String, T> or do we allow to do something else as well. Not sure if something else apart from a map would make sense.

What did you have in mind @slukes?

@chris922
Copy link
Member

@filiphr: Do you have some special cases in mind where it make sense to write map[id] instead of just map.id like we are always doing it? Hum... while writing this it comes into a mind that a key could have whitespaces.
map[foo bar] looks probably better than map.foo bar ... but I don't find any other reasons to prevent using whitespaces in the dot-notation then this one.

As just proposed in #1731 we may find a general way with a consistent approach for different, but similar, kind of mappings.

@slukes
Copy link

slukes commented Feb 25, 2019

The main use case I had in mind was actually mapping from a bean to a Map<String, Object> in essence to have the same effect as BeanUtils.describe(). I don't think this would require any special syntax as such? (I think I incorrectly jumped on this issue as being the same as what I wanted).

It makes sense to implement the case @filiphr raises at the same time though. Regarding the syntax for that - In a map key you can literally any character in the key including ., [, ] what would we expect to happen here? Should there be some kind of escape sequence to allow for this in either case?

@ft0907
Copy link

ft0907 commented Dec 14, 2019

Sorry, I want to ask, where does this feature go now? When can they be merged?

@elangochordz
Copy link

Does mapstruct supports HashMap with value as List of objects ? as shown below ,

private Map<String, List> issues = null;

with version as --> 1.3.0.Final

@sjaakd
Copy link
Contributor

sjaakd commented Apr 12, 2020

I'm working on a pr for this. Not much progress last 2 months

@gursahibsahni
Copy link

I would love to see this in action. But I see it is still open, have we reached anywhere near to the proposed solution?

@filiphr filiphr changed the title [Feature] Mapping hashmap to and from a field [Feature] Mapping hashmap from a field Aug 14, 2021
@filiphr
Copy link
Member

filiphr commented Aug 14, 2021

I've changed the title of this to reflect the functionality for mapping a Map from a Field. Since the functionality for mapping from a Map into a Field is part of 1.5.0.Beta1 done as part of #1075.

@ReparoZ
Copy link

ReparoZ commented Jun 8, 2022

I would love to see this in action. any progress on this feature?

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

10 participants