jmapper-framework / jmapper-core

Elegance, high performance and robustness all in one java bean mapper
http://jmapper-framework.github.io/jmapper-core
Apache License 2.0
231 stars 41 forks source link

[Question] Map several source field to single destination object #68

Open petromir opened 7 years ago

petromir commented 7 years ago

Hi,

I read about similar case here https://github.com/jmapper-framework/jmapper-core/wiki/Flatten-And-Encapsulate, but I couldn't match it to my case. So here it is:

public class Dto {
   private String data;
   private String text;
   private List<Long> ids;
}

public class Entity {
   private List<NestedEntity> nestedEntities;
}

public class NestedEntity {
   private String data;
   private String text;
   private Long id;
}

Dto dto = new Dto();
dto.setData("data");
dto.setText("text");
List<Long> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
dto.setIds(ids);

Entity entity = // JMapper getDestination method
entity.getNestedEntities().forEach(System.out::println)
// NestedEntity(data=data, text=text, id=1)
// NestedEntity(data=data, text=text, id=2)

What annotation should be placed in the Dtoin order to have a list of NestedEntity objects, which have same data located in data and text fields and different id taken from each item in ids

avurro commented 7 years ago

Hi @petromir, There is currently no automation, default values are on the roadmap, meanwhile the only option is to use a static method only to encapsulate the mapping.

petromir commented 7 years ago

@avurro Could you please provide an example?

avurro commented 7 years ago

Hi @petromir, Actually looking better there is no way to encapsulate the mapping, but you gave me a good idea, that is to add a new placeholder that allows access to the root of the field, something like that: ${source.root} and ${destination.root} would be useful. What do you think about it ?

petromir commented 7 years ago

Hi @avurro, Whatever you think will help us. Just provide few examples and probably a wiki article.

netmikey commented 7 years ago

I ran across a similar situation where I wanted to map two fields firstName and lastName onto one destination field fullName. I've been looking at global mappings and converters but nothing seems to fit the bill.

I'd like to do something like:

<!-- This is not working of course -->
<attribute name="fullName">
  <value>return ${source.root.firstName} + " " + ${source.root.lastName};</value>
</attribute>

Would that head into the same direction?