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

Skip to content

Using target="." Doesn't seem to handle collections #2444

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

Closed
cattox opened this issue May 12, 2021 · 4 comments
Closed

Using target="." Doesn't seem to handle collections #2444

cattox opened this issue May 12, 2021 · 4 comments

Comments

@cattox
Copy link

cattox commented May 12, 2021

My use case is the following:
target has some attributes (attr1, attr2, attr3).
source has a list of objects with key and value like:

source: {
    properties: [
        {
            key: attr1,
            value: "val1"
        },
        {
            key: attr2,
            value: "val2"
        },
        {
            key: attr3,
            value: "val3"
        },
    ]
}

I guess the mapping I'm targeting is pretty obvious by now, So what I did was:

@Mapping(source = "properties", target = ".")
public abstract Target convert(Source source);

public void mapProperties(List<Property> properties, @MappingTarget Target target) {
    (and then some mapping code here)
}

but this doesn't generate any mapping code. I took a peek on the source code and it seems to handle only single objects.

@filiphr
Copy link
Member

filiphr commented May 15, 2021

It is indeed obvious what you are trying to achieve. However, the implementation for something like this is not entirely straightforward. MapStruct does not have support for this because and it is not as easy as you might think. How can MapStruct know what should be considered as a key and value for the Target?

I would suggest following #1075 which will add support for mapping from Map into some bean. Your use case is still not covered by that, but you can convert your List<Property> into a Map and then do the mapping.

For the time being you have to do that mapping by hand.

@filiphr filiphr closed this as completed May 15, 2021
@cattox
Copy link
Author

cattox commented May 15, 2021

@filiphr , I was not expecting MapStruct to find keys and values automatically. That code would have to be implemented by the user on a method with the list as source and the target class as target (parameters), just as I stated on the example above. I believe this wouldn't be hard at all, right?

@filiphr
Copy link
Member

filiphr commented May 16, 2021

So you are looking for the code to invoke mapProperties for you?

I would actually suggest something else here. You can do the exact same thing in @AfterMapping / @BeforeMapping.

e.g.

public abstract Target convert(Source source);

@AfterMapping
public void mapProperties(Source source, @MappingTarget Target target) {
    List<Properties> properties = source.getProperties();
    //(and then some mapping code here)
}

@cattox
Copy link
Author

cattox commented May 17, 2021

I guess that works.

Thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants