有没有一种方法可以提取Mapstruct映射?

huangapple go评论109阅读模式
英文:

is there a way to extract Mapstruct mapping?

问题

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;
    }
}
英文:

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;
}
}

答案1

得分: 1

这在Mapstruct中是不可能的。有另一个类似于您的问题的问题:https://stackoverflow.com/q/62675889/4494577。那里的答案描述了解决问题的“某种”方法,但它非常有限 - 它仅从@Mapping注释中提取信息(不支持隐式映射)。

尽管如此,如果能够实现这个功能将会非常有用 - 您应该向Mapstruct团队提出这个建议。

英文:

It's impossible with Mapstruct. There's another question similar to yours: https://stackoverflow.com/q/62675889/4494577. The answer there describes "some" approach to the problem, but it's very limited - it simply scrapes the information from the @Mapping annotations (no support for implicit mappings).

It would be quite useful to have this though - you should propose this feature to the Mapstruct team.

huangapple
  • 本文由 发表于 2020年8月27日 18:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定