MapStruct 中的自定义源存在检查方法名称 [英] Custom Source presence checking method name in MapStruct

查看:135
本文介绍了MapStruct 中的自定义源存在检查方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以生成自定义的存在检查"方法名称,作为属性本身的方法而不是拥有对象?

is it posible to generate a custom "presence checking" method name, being a method of the property itself rather the owning object?

我知道我可以使用 hasProperty() 方法来检查值是否存在...https://mapstruct.org/documentation/stable/reference/html/#source-presence-check

I know I can use hasProperty() methods to check for presence of a value... https://mapstruct.org/documentation/stable/reference/html/#source-presence-check

但是对于 Optional 或 JsonNullable(来自 OpenApi nonullable),检查方法是在属性本身上,而不是在拥有对象上... :-(

but with Optional or JsonNullable (from OpenApi nonullable) that checking method is on the property itself, not on the owning object... :-(

我可以轻松地使用"或扩展一个简单的自定义映射器来映射 JsonNullable 或 Optional

I can map JsonNullable or Optional easyly 'using' or extending a simple custom Mapper

@Mapper
public class JsonNullableMapper {

    public <T> T fromJsonNullable(final JsonNullable<T> jsonNullable) {
        return jsonNullable.orElse(null);
    }

    public <T> JsonNullable<T> asJsonNullable(final T nullable) {
        return nullable != null ? JsonNullable.of(nullable) : JsonNullable.undefined();
    }

}

我想要实现的是存在检查":

what I would like to achieve is something like this as "presence check":

if(source.getProperty().isPresent()) {
    target.set(customMapper.map(source.getProperty()));
}

有人为此找到了解决方案吗?

Any one found a solution for this?

感谢和问候

推荐答案

我已经成功实现了自定义 lombok 扩展程序 生成存在检查";方法.

I have managed to implement custom lombok extension which generates "presence checknig" methods.

这是一个示例项目.简而言之,我添加了 @PresenceChecker 注释并实现了 Lombok Javac 注释处理程序.

Here is an example project. In short I added @PresenceChecker annotation and implemented Lombok Javac Annotation handler.

可以将其与其他 Lombok 注释一起使用:

It's possible to use it together with other Lombok annotations:

@Getter
@Setter
public class User {
    private String name;
}

@Getter
@Setter
@PresenceChecker
public class UserUpdateDto {
    private String name;
}

//MapStruct Mapper interface declaration
@Mapper
public interface UserMapper {
    void updateUser(UserUpdateDto dto, @MappingTarget User user);
}

生成的代码:

public class User {
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class UserUpdateDto {
    private boolean hasName;
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
        this.hasName = true;
    }

    public boolean hasName() {
        return this.hasName;
    }
}

//MapStruct Mapper implementation
public class UserMapperImpl implements UserMapper {
    @Override
    public void updateUser(UserUpdateDto dto, User user) {
        if ( dto == null ) {
            return;
        }

        if ( dto.hasName() ) {
            user.setName( dto.getName() );
        }
    }
}

这篇关于MapStruct 中的自定义源存在检查方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆