0

Have a Maapstruct mapping interface and DTO with various enums. I have two different enums both of the format:

public enum TxAccTypeDto  {
    FINANCE("Finance")
}

public enum TxStatusDto  {
    PENDING("Pending"),
}

public enum TxStatusDto  {
    PENDING("Pending"),
}

public enum ContactMethod {
    TELEPHONE,

Issue I have is when converting from Entity fields of type String (not enum) how do I get the right enum used and matching on the Code (value in brackets)?

If used only one enum, then could use this:

 default TxAccTypeDto toEnum(final String code) {
    return Arrays.stream(TxAccTypeDto.values()).filter(
            testEnum -> testEnum.getCode().equals(code))
            .findFirst()
            .orElse(null);
}

But it obviously does not know the enum to map to with param String code. Was hoping I could:

  1. just do toEntity(fromDto) and toDto(fromStringCode, forDtoTypeAndFile)
  2. only add code per type and not each source/target
DSEyers
  • 39
  • 4

1 Answers1

-1

This post helps: Map custom method mapper to Mapstruct

But means specifing qualifier/individual mapping. Want it to do it by type and describe mapping once. Note the problem is from StringToEnum with both having single String param so not qualified

@Mappings({
  @Mapping(source = "sourceAccountType", target = "sourceAccountType", qualifiedByName = "txAccTypeDto"),
  @Mapping(source = "targetAccountType", target = "targetAccountType", qualifiedByName = "txAccTypeDto"),
  @Mapping(source = "transactionStatus", target = "transactionStatus", qualifiedByName = "txStatusDto"),
})
DSEyers
  • 39
  • 4