I am new to Mapstruct. I have a scenario where, in my target object I have a java map with key value pair<String,String> and I have to fill this map using source objects inner object properties/data member values.
My code is something like bellow(dummy code):
public class Student {
public String name;
public String rollNo;
public Map<String, String> marks;
}
public class ExamResult{
public String stud_name;
public String Stud_rollNo;
public Marks marks;
}
public class Marks{
public Integer English;
public Integer Maths;
public Integer Science;
}
How would I manually achieve the same thing like bellow:
Student target;
ExamResult source;
target.setName(source.stud_name);
target.setRollNo(source.Stud_RollNo);
target.marks.put("ENGLISH",source.marks.english_marks);
target.marks.put("MATHS",source.marks.math_marks);
target.marks.put("SCIENCE",source.marks.science_marks);
For direct property mapping I have found code, but not sure how I can map the values to be filled in the marks
map.
I had given a thought to use java expression to fill in target map values, but didn't found any documentation or such example of expressions using for target object.
I was thinking to use like bellow but not sure it will work:
@Mapping(source = "stud_name", target = "name")
@Mapping(source = "Stud_RollNo", target = "rollNo")
@Mapping(source = "source.marks.english_marks",target = "java( marks.put(\"ENGLISH\",source.marks.english_marks )")
@Mapping(source = "source.marks.math_marks",target = "java( marks.put(\"MATHS\",source.marks.math_marks )")
@Mapping(source = "source.marks.science_marks",target = "java( marks.put(\"SCIENCE\",source.marks.science_marks )")
Student doConvert(ExamResult src)
Any help, any suggestion or any workaround is appreciated. Thanks in Advance.