Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From string to enum using mapstruct

I want to convert String to enum using mapstruct

enum TestEnum {
   NO("no");
   String code;

   TestEnum(String code) {
     this.code = code
   }

   public String getCode() {
    return code;
   }
}

I have a code that I've got from service and I want to convert this code to Enum how to do it with easier way by mapstruct

like image 735
m.zemlyanoi Avatar asked Feb 01 '18 20:02

m.zemlyanoi


3 Answers

The following code worked for me.

@Mappings({
        @Mapping(source = "genderDTO.name", target = "genderName")
})
GenderRecord dtoTogenderRecord(GenderDTO genderDTO);
  • "genderName" is the Enum
  • "genderDTO.name" is the String

The result was:

@Override
public GenderRecord dtoTogenderRecord(GenderDTO genderDTO) {
    if ( genderDTO == null ) {
        return null;
    }

    GenderRecord genderRecord = new GenderRecord();

    if ( genderDTO.getName() != null ) {
        genderRecord.setGenderName( Enum.valueOf( GenderType.class, genderDTO.getName() ) );
    }

    return genderRecord;
}

I also use the following at the interface level to ensure null checks:

@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
like image 177
jpCharger Avatar answered Oct 22 '22 09:10

jpCharger


MapStruct only calls the "setProperty(PropertyType)" function, so simply use polymorphism in your setters

For DTO:

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatus(ProjectStatus status) {
        this.status = status.toString();
    }

For Entity:

    public void setStatus(ProjectStatus status) {
        this.status = status;
    }

    public void setStatus(String status) {

        switch (status) {
        case "PENDING_WITH_RM":
            this.status = ProjectStatus.PENDING;
            break;
        case "PENDING_WITH_OPS":
            this.status = ProjectStatus.PENDING;
            break;
        case "COMPLETED":
            this.status = ProjectStatus.COMPLETED;
            break;
        default:
            this.status = ProjectStatus.DRAFT;
        }
    }
like image 21
Ryan Augustine Avatar answered Oct 22 '22 08:10

Ryan Augustine


Here is a solution with an abstract mapper, but if you want you can convert it with a default methode or a class

@Mapper
public abstract class TestMapper {

    abstract Source toSource(Target target);
    abstract Target totarget(Source source);

    String toString(TestEnum test){
        return test.getCode();
    }
    TestEnum toEnum(String code){
        for (TestEnum testEnum : TestEnum.values()) {
            if(testEnum.equals(code)){
                return testEnum;
            }
        }
        return null;
    }
}

public class Source {    
    String value;    
    public String getValue() {
        return value;
    }    
    public void setValue(String value) {
        this.value = value;
    }    
}

public class Target {
    TestEnum value;
    public TestEnum getValue() {
        return value;
    }
    public void setValue(TestEnum value) {
        this.value = value;
    }
}
like image 44
Bertrand Cedric Avatar answered Oct 22 '22 07:10

Bertrand Cedric