Kotlin 中的 MapStruct:如何将多个源映射到一个模型中?
MapStruct in Kotlin: How to map from multiple sources into one model?
假设我有模型:
data class MyModel(
var firstName: String,
var lastName: String,
)
假设我正在尝试将一些属性从我的来源 PersonInfo
映射到这个模型中
public final data class PersonInfo(
val firstName: String,
val lastName: String,
)
这很简单;我只是 运行
fun persontoToModel(personInfo: PersonInfo): MyModel
现在假设 MyModal 现在是:
data class MyModel(
var firstName: String,
var lastName: String,
var dateOfBirth: String,
var licenseNumber: String,
)
我们还假设 PersonInfo
中并非所有这些信息都可用。相反,dateOfBirth
在 PersonDetails
中可用,licenseNumber
在 PersonDocuments
中可用,这两者还包括一堆我不一定需要映射的其他属性在这种情况下。
PersonDetails 和 PersonDocuments:
public final data class PersonDetails(
val dateOfBirth: LocalDate,
val weight: String,
val height: String,
val eyeColor: String,
)
public final data class PersonDocuments(
val licenseNumber: String,
val passportNumber: String,
val socialSecurityNumber: String,
)
根据我对文档的理解,我可以通过在 Mapping
注释中使用源指定它们来手动映射这些字段,如下所示:
@Mapping(source="personDetails.dateOfBirth", target="dateOfBirth")
@Mapping(source="personDocuments.licenseNumber", target="licenseNumber")
@BeanMapping(ignoreUnmappedSourceProperties = [ all the other stuff I don't need])
fun persontoToModel(personInfo: PersonInfo, personDetails: PersonDetails, personDocuments: PersonDocuments): MyModel
除非这不起作用。首先,第二个映射注释被突出显示为错误,原因是“此注释不可重复”。我不确定这是否是 Kotlin 的问题,因为 MapStruct 上的文档似乎只使用 Java 个示例。
@Mappings(
Mapping(source="personDetails.dateOfBirth", target="dateOfBirth"),
Mapping(source="personDocuments.licenseNumber", target="licenseNumber")
)
@BeanMapping(ignoreUnmappedSourceProperties = [ all the other stuff I don't need])
fun persontoToModel(personInfo: PersonInfo, personDetails: PersonDetails, personDocuments: PersonDocuments): MyModel
As of Kotlin 1.1, repeated annotations are not supported (see
KT-12794). You have to wrap the Mapping-Annotation in a
Mappings-Annotation.
假设我有模型:
data class MyModel(
var firstName: String,
var lastName: String,
)
假设我正在尝试将一些属性从我的来源 PersonInfo
public final data class PersonInfo(
val firstName: String,
val lastName: String,
)
这很简单;我只是 运行
fun persontoToModel(personInfo: PersonInfo): MyModel
现在假设 MyModal 现在是:
data class MyModel(
var firstName: String,
var lastName: String,
var dateOfBirth: String,
var licenseNumber: String,
)
我们还假设 PersonInfo
中并非所有这些信息都可用。相反,dateOfBirth
在 PersonDetails
中可用,licenseNumber
在 PersonDocuments
中可用,这两者还包括一堆我不一定需要映射的其他属性在这种情况下。
PersonDetails 和 PersonDocuments:
public final data class PersonDetails(
val dateOfBirth: LocalDate,
val weight: String,
val height: String,
val eyeColor: String,
)
public final data class PersonDocuments(
val licenseNumber: String,
val passportNumber: String,
val socialSecurityNumber: String,
)
根据我对文档的理解,我可以通过在 Mapping
注释中使用源指定它们来手动映射这些字段,如下所示:
@Mapping(source="personDetails.dateOfBirth", target="dateOfBirth")
@Mapping(source="personDocuments.licenseNumber", target="licenseNumber")
@BeanMapping(ignoreUnmappedSourceProperties = [ all the other stuff I don't need])
fun persontoToModel(personInfo: PersonInfo, personDetails: PersonDetails, personDocuments: PersonDocuments): MyModel
除非这不起作用。首先,第二个映射注释被突出显示为错误,原因是“此注释不可重复”。我不确定这是否是 Kotlin 的问题,因为 MapStruct 上的文档似乎只使用 Java 个示例。
@Mappings(
Mapping(source="personDetails.dateOfBirth", target="dateOfBirth"),
Mapping(source="personDocuments.licenseNumber", target="licenseNumber")
)
@BeanMapping(ignoreUnmappedSourceProperties = [ all the other stuff I don't need])
fun persontoToModel(personInfo: PersonInfo, personDetails: PersonDetails, personDocuments: PersonDocuments): MyModel
As of Kotlin 1.1, repeated annotations are not supported (see KT-12794). You have to wrap the Mapping-Annotation in a Mappings-Annotation.