Let says I have a basic case where I map an object to an other, I need to declare the mapping in the Mapper class.
For validation I may want to remember the original field name and his corresponding mapping.
Is there a way to do it with mapstruct without having to do a mapping "manually"
here is an example of what I would expect.
public class PersonDemo {
public static void main(String[] args) {
final PersonMapper mapper = Mappers.getMapper(PersonMapper.class);
Person person = mapper
.toPerson(new PersonDto()
.setFirstName("John")
.setName("Doe"));
System.out.println(person);
System.out.println("Expected " + toPersonDesc("firstName") + " to be firstName");
System.out.println("Expected " + toPersonDesc("name") + " to be lastName");
}
@Mapper
public interface PersonMapper {
@Mapping(target = "lastName", source = "name")
Person toPerson(PersonDto dto);
}
//expected method. to be generated somehow from the mapping.
static String toPersonDesc(String fieldName) {
switch (fieldName) {
case "name": return "lastName";
case "firstName": return "firstName";
}
return null;
}
@Data
@Accessors(chain = true)
public static class Person {
private String lastName;
private String firstName;
}
@Data
@Accessors(chain = true)
public static class PersonDto {
private String name;
private String firstName;
}
}