为什么我不能在@Mapping属性中引用@Context参数?

问题描述

使用mapstruct,我需要的是一种具有多个源的映射方法,并且将这几个源传递给其他映射方法,因此在需要这些其他源的情况下,我可以为所有映射方法使用所有多个源。

当前可以使两个功能协同工作:

  • 仅@Context可以传递给其他映射方法,但不能用作源。
  • 辅助参数(非@Context)可以用作源,但不会传递给其他映射方法

因此,该功能需要要么是允许将辅助源参数传递给其他映射方法,要么是使@Context参数能够被@Mapping(target="something",source="ctx.somethingElse")@Mapping(target="ctx.something",source="somethingElse)

引用>

示例:

// source classes : `Instant timestamp` is a field I obtain separately

Instant timestamp;

class WrapperSource
   List<NestedSource> nested;

class NestedSource
    String name;



// target classes : I want to map the nested and name field but also to insert the timestamp in both the WrapperTarget and every NestedTarget in the nested list

class WrapperTarget
   Instant timestamp;
   List<NestedTarget> nested;

class NestedTarget
    String name;
    Instant timestamp;

理想情况下,映射将类似于:

// Currently this doesn't work because we can't reference the @Context in the source attribute

@Mapping(target = "nested",source="source.nested")
@Mapping(target = "timestamp",source="timestamp")
WrapperTarget map(WrapperSource source,@Context Instant timestamp);

@Mapping(target = "name",source="source.name")
@Mapping(target = "timestamp",source="timestamp")
NestedTarget map(NestedSource source,@Context Instant timestamp);

或:

// Currently this doesn't work because the second method with 2 sources in not called by the first generated method

@Mapping(target = "nested",Instant timestamp);

@Mapping(target = "name",Instant timestamp);

唯一适用于我的(详细)解决方法是:

// @Context is passed around and I can manually use it as a source in an @AfterMapping but it requires additional code

WrapperTarget map(WrapperSource source,@Context Instant timestamp);

@AfterMapping
void map(WrapperSource source,@MappingTarget WrapperTarget target,@Context Instant timestamp) {
    target.setTimestamp(timestamp);
}

NestedTarget map(NestedSource source,@Context Instant timestamp);

@AfterMapping
void map(NestedSource source,@MappingTarget NestedTarget target,@Context Instant timestamp) {
    target.setTimestamp(timestamp);
}

这可以正常工作,但是需要附加的手动代码,因此更好的替代方法是能够在@Context中引用@Mapping's attributes。这样,我可以使用第一个“理想”的映射示例。

此问题是否有更好的解决方法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)