Mapstruct:一个源字段到多个多个目标字段
Mapstruct: one source field to several multiple target fields
public class ExtensionTarget {
private StringType valueString;
private BooleanType valueBoolean;
private CodeableConcept valueCodeableConcept;
}
public class ExtensionSource {
private Type value;
}
其中 StringType
、BooleanType
和 CodeableConcept
继承自 Type
。
目前,我正在使用这个映射器来制作这个映射:
@Mapper(
uses = { TypeMapper.class }
)
public interface ExtensionMapper {
ExtensionTarget fhirToMpi(ExtensionSource fhirType);
}
public abstract class TypeMapper {
private final StringTypeMapper stringTypeMapper = Mappers.getMapper(StringTypeMapper.class);
private final BooleanTypeMapper booleanTypeMapper = Mappers.getMapper(BooleanTypeMapper.class);
private final CodeableConceptMapper codeableConceptMapper = Mappers.getMapper(CodeableConceptMapper.class);
public Type fhirToMpi(org.hl7.fhir.r4.model.Type fhirType) {
if (fhirType instanceof CodeableConcept) {
return this.codeableConceptMapper.fhirToMpi((CodeableConcept)fhirType);
} else if (fhirType instanceof StringType) {
return this.stringTypeMapper.fhirToMpi((StringType)fhirType);
} else if (fhirType instanceof BooleanType) {
return this.booleanTypeMapper.fhirToMpi((BooleanType)fhirType);
}
return null;
}
}
还有其他更优雅的获取方式吗?
目前没有优雅的方式来实现您正在寻找的东西。然而,很快,就像真的很快我们将发布 MapStruct 1.5.Beta2,它将作为 this PR.
的一部分实现您的请求
简而言之,一旦 1.5.Beta2 发布,您可以执行以下操作:
@Mapper(uses = {
StringTypeMapper.class,
BooleanTypeMapper.class,
CodeableConceptMapper.class,
})
public interface TypeMapper {
@SubclassMapping(target = CodeableConcept.class, source = org.hl7.fhir.r4.model.CodeableConcept.class)
@SubclassMapping(target = StringType.class, source = org.hl7.fhir.r4.model.StringType.class)
@SubclassMapping(target = BooleanType.class, source = org.hl7.fhir.r4.model.BooleanType.class)
Type fhirToMpi(org.hl7.fhir.r4.model.Type fhirType);
}
我假设你的子 类 与你的源类型在同一个包中。
除此之外,还有 SubclassExhaustiveStrategy
可以控制未定义类型之间的映射应该如何发生。
即如果目标类型没有有效的构造函数或者它是抽象的。
目前的可能性是:
- COMPILE_ERROR - 基本上,如果您尝试这样做,现在会发生什么
- RUNTIME_EXCEPTION - 会有运行时
IllegalArgumentException
抛出
在你的例子中你 return null
。这也是一个有效的选项。如果需要,我建议您在 MapStruct 跟踪器中创建一个问题以添加此功能。
public class ExtensionTarget {
private StringType valueString;
private BooleanType valueBoolean;
private CodeableConcept valueCodeableConcept;
}
public class ExtensionSource {
private Type value;
}
其中 StringType
、BooleanType
和 CodeableConcept
继承自 Type
。
目前,我正在使用这个映射器来制作这个映射:
@Mapper(
uses = { TypeMapper.class }
)
public interface ExtensionMapper {
ExtensionTarget fhirToMpi(ExtensionSource fhirType);
}
public abstract class TypeMapper {
private final StringTypeMapper stringTypeMapper = Mappers.getMapper(StringTypeMapper.class);
private final BooleanTypeMapper booleanTypeMapper = Mappers.getMapper(BooleanTypeMapper.class);
private final CodeableConceptMapper codeableConceptMapper = Mappers.getMapper(CodeableConceptMapper.class);
public Type fhirToMpi(org.hl7.fhir.r4.model.Type fhirType) {
if (fhirType instanceof CodeableConcept) {
return this.codeableConceptMapper.fhirToMpi((CodeableConcept)fhirType);
} else if (fhirType instanceof StringType) {
return this.stringTypeMapper.fhirToMpi((StringType)fhirType);
} else if (fhirType instanceof BooleanType) {
return this.booleanTypeMapper.fhirToMpi((BooleanType)fhirType);
}
return null;
}
}
还有其他更优雅的获取方式吗?
目前没有优雅的方式来实现您正在寻找的东西。然而,很快,就像真的很快我们将发布 MapStruct 1.5.Beta2,它将作为 this PR.
的一部分实现您的请求简而言之,一旦 1.5.Beta2 发布,您可以执行以下操作:
@Mapper(uses = {
StringTypeMapper.class,
BooleanTypeMapper.class,
CodeableConceptMapper.class,
})
public interface TypeMapper {
@SubclassMapping(target = CodeableConcept.class, source = org.hl7.fhir.r4.model.CodeableConcept.class)
@SubclassMapping(target = StringType.class, source = org.hl7.fhir.r4.model.StringType.class)
@SubclassMapping(target = BooleanType.class, source = org.hl7.fhir.r4.model.BooleanType.class)
Type fhirToMpi(org.hl7.fhir.r4.model.Type fhirType);
}
我假设你的子 类 与你的源类型在同一个包中。
除此之外,还有 SubclassExhaustiveStrategy
可以控制未定义类型之间的映射应该如何发生。
即如果目标类型没有有效的构造函数或者它是抽象的。 目前的可能性是:
- COMPILE_ERROR - 基本上,如果您尝试这样做,现在会发生什么
- RUNTIME_EXCEPTION - 会有运行时
IllegalArgumentException
抛出
在你的例子中你 return null
。这也是一个有效的选项。如果需要,我建议您在 MapStruct 跟踪器中创建一个问题以添加此功能。