[英]mapstruct qualifiedByName without parameters simplify expression
I would like to set a constant on the field, but with method call, i do not want to create an expression it looks terrible, i would like to simplify this call我想在字段上设置一个常量,但是通过方法调用,我不想创建一个看起来很糟糕的表达式,我想简化这个调用
@Mapping(target = "channelNotification", expression= "java(new ChannelNotification[]{ " +
"new ChannelNotification(\"email\", 10)})")
to get something like this:得到这样的东西:
@Mapping(target = "channel", qualifiedByName = "getChannel")
Notification convert(Email emailEntity);
@Named("getChannel")
default Channel[] getChannel() {//with empty params
return new Channel[]{new Channel("email", 10)};
}
Source entity doesn't have field channelNotification, and i don't need to use it.源实体没有字段channelNotification,我不需要使用它。 I just want to set a constant like constant = *, but with method call
我只想设置一个常量,如常量 = *,但使用方法调用
This is currently not possible.目前这是不可能的。 However, what you can do is to use a combination of
constant
and qualifiedByName
.但是,您可以做的是结合使用
constant
和qualifiedByName
。
eg例如
@Mapping(target = "channel", constant = "email" qualifiedByName = "getChannel")
Notification convert(Email emailEntity);
@Named("getChannel")
default Channel[] getChannel(String channelType) {
Channel channel;
if ("email".equals(channelType)) {
channel = new Channel("email", 10);
} else {
throw new IllegalArgumentException("unknown channel type " + channelType);
}
return new Channel[]{channel};
}
What is not known that much is that constant
works in a similar way with qualifiers as other Mapping#source
.鲜为人知的是,
constant
与限定符的工作方式与其他Mapping#source
类似。 Since 1.4 custom mappings between String
(what is in constant
) and the target type will be looked for in order to convert the constant value.从 1.4 开始,将查找
String
( constant
中的内容)和目标类型之间的自定义映射,以便转换常量值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.