【发布时间】:2021-10-27 00:03:06
【问题描述】:
看到documentation 关于如何对枚举使用自定义转换策略,我有点困惑。我在 Kotlin 中执行此操作,但 Java 解决方案也可以。
我使用的是 14.2
我有这个
package org.mapstruct.custom.spi
import org.mapstruct.ap.spi.EnumTransformationStrategy
class StripLowerCaseEnumTransformationStrategy : EnumTransformationStrategy {
companion object {
const val NAME = "stripLowerCase"
}
override fun getStrategyName(): String {
return NAME
}
override fun transform(value: String, configuration: String): String {
return value.removePrefix(configuration).toLowerCase()
}
}
但是当我尝试在我的 EnumMappers 中使用它时,它说找不到它。我错过了哪一步?
@Mapper
interface EnumMapper {
@ValueMappings(
ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.NULL),
ValueMapping(source = "PERSON_STATUS_INVALID", target = MappingConstants.NULL)
)
@EnumMapping(
nameTransformationStrategy = StripLowerCaseEnumTransformationStrategy.NAME,
configuration = "PERSON_STATUS_"
)
fun fromProto(status: PersonStatusProto): PersonStatus
@InheritInverseConfiguration
fun toProto(status: PersonStatus): PersonStatusProto
}
这是错误:error: There is no registered EnumTransformationStrategy for 'stripLowerCase'. Registered strategies are: prefix, stripPrefix, stripSuffix, suffix.
我还创建了文件resources/META-INF/services/org.mapstruct.ap.spi.EnumTransformationStrategy,其值为org.mapstruct.custom.spi.StripLowerCaseEnumTransformationStrategy。
【问题讨论】: