How can I map an enum to a boolean with mapstruct?

霸气de小男生 提交于 2019-12-12 17:03:43

问题


I have some auto-generated enums that I need to map to boolean values in a MapStruct mapper. They go like this:

enum YN {
    Y("Y"), N("N")
}

enum ZO {
    _0("0"), _1("1")
}

I've tried to use @ValueMappings(), but it didn't work:

@ValueMappings({
    @ValueMapping(source="Y", target=true),
    @ValueMapping(source="N", target=false)
)
Boolean map(YN value);

How can I implemente this mapping?


回答1:


ValueMappings are used for mapping between two Enums. You can't use them to map an Enum to something else. For your defined mapping you will have to write a mapping by yourself. Then MapStruct can use that one in other mappers.

abstract class Mapper {
    Boolean map(YN value) {
        return YN.Y.equals(value);
    }

    Boolean map(ZO value) {
        return ZO.O.equals(value);
    }
}


来源:https://stackoverflow.com/questions/43356232/how-can-i-map-an-enum-to-a-boolean-with-mapstruct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!