有没有一种在使用多个源值时为 Mapstruct 指定默认输入参数的方法?

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

Is there a way of specifying a default input argument for Mapstruct when using multiple source values?

问题

在下面的示例中,我有两个输入对象,映射到一个输出对象。
大多数映射是直接从一个输入到一个输出,只有一个来自另一个对象。

    @Mapping(source = "input.a", target = "output.a")
    @Mapping(source = "input.b", target = "output.b")
    @Mapping(source = "input.c", target = "output.c")
    @Mapping(source = "input.d", target = "output.d")
    @Mapping(source = "extra.a", target = "output.extraa")
    Output toOutputMapper(Input input, ExtraValues extra)

是否有一种方法可以说“使用此对象作为默认对象”,这样就可以省略映射值,放弃 Mapstruct 提供的自动映射呢?

类似于:

    @Mapping(source = "extra.a", target = "output.extraa")
    Output toOutputMapper(@Default Input input, ExtraValues extra)
英文:

So in the example below, I have 2 inputs object, being mapped to one output object.
The majority of the mappings are direct from one input, to one output, with only one coming from another object.

    @Mapping(source = "input.a" target = "output.a")
    @Mapping(source = "input.b" target = "output.b")
    @Mapping(source = "input.c" target = "output.c")
    @Mapping(source = "input.d" target = "output.d")
    @Mapping(source = "extra.a" target = "output.extraa")
    Output toOutputMapper(Input input, ExtraValues extra)

Is there a way to say "use this object as default", which saves me mapping the values and forgoing the automatic mapping Mapstruct provides?

Something like:

    @Mapping(source = "extra.a" target = "output.extraa")
    Output toOutputMapper(@Default Input input, ExtraValues extra)

答案1

得分: 1

当使用MapStruct 1.4时,您可以使用映射到当前目标的方式来实现您所寻找的功能。

例如:

    @Mapping(source = "input", target = ".")
    @Mapping(source = "extra.a", target = "output.extraa")
    Output toOutputMapper(Input input, ExtraValues extra)
英文:

When using MapStruct 1.4 you can use mapping to current target to achieve what you are looking for.

e.g.

    @Mapping(source = "input" target = ".")
    @Mapping(source = "extra.a" target = "output.extraa")
    Output toOutputMapper(Input input, ExtraValues extra)

huangapple
  • 本文由 发表于 2020年9月18日 00:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63941970.html
匿名

发表评论

匿名网友

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

确定