I need to map from an open banking class to my core class. I am working with mapstruct
. I need to map multiple enum fields to one on my core class. But i think mapstruct doesn’t support mapping multiple fields. Any tricky way or a solution for that?
Thanks in advance!
Here is my enum values from open banking:
public enum Category { TRANS_AND_SAVINGS_ACCOUNTS("TRANS_AND_SAVINGS_ACCOUNTS"), REGULATED_TRUST_ACCOUNTS("REGULATED_TRUST_ACCOUNTS"), CRED_AND_CHRG_CARDS("CRED_AND_CHRG_CARDS"), PERS_LOANS("PERS_LOANS"), MARGIN_LOANS("MARGIN_LOANS"), PROPERTY_LOANS("PROPERTY_LOANS"), TRADE_FINANCE("TRADE_FINANCE"), OVERDRAFTS("OVERDRAFTS"), BUSINESS_LOANS("BUSINESS_LOANS"), RESIDENTIAL_MORTGAGES("RESIDENTIAL_MORTGAGES"); ... }
And here is my core class:
public enum Type{ CHARGES( "Charges" ), CREDITS( "Credits" ), ALL_LOANS( "AllLoans" ), MORTGAGE( "Mortgage" ), OTHER( "Other" ), PREPAIDCARD( "PrePaidCard" ), SAVINGS( "Savings" ); ... }
And i have a valueMapping
as following(which is wrong):
public interface TypeMapper { @ValueMappings( { @ValueMapping(source = "CHARGES", target= "CRED_AND_CHRG_CARDS"), @ValueMapping(source = "CREDITS", target= "CRED_AND_CHRG_CARDS"), @ValueMapping(source = "ALL_LOANS", target = "PERS_LOANS"), @ValueMapping(source = "ALL_LOANS", target = "MARGIN_LOANS"), @ValueMapping(source = "ALL_LOANS", target = "BUSINESS_LOANS"), @ValueMapping(source = "ALL_LOANS", target = "PROPERTY_LOANS"), @ValueMapping(source = "MORTGAGE", target= "RESIDENTIAL_MORTGAGES"), @ValueMapping(source = "OTHER", target= "OVERDRAFTS"), @ValueMapping(source = "PREPAIDCARD", target = MappingConstants.NULL), @ValueMapping(source = "SAVINGS", target = "TRANS_AND_SAVINGS_ACCOUNTS") } ) ... }
It does not accept to map PERS_LOANS("PERS_LOANS"), MARGIN_LOANS("MARGIN_LOANS"), PROPERTY_LOANS("PROPERTY_LOANS")
to ALL_LOANS
. I wonder if there’s a way to map all 3 to one field. Maybe can it be covered in @AfterMapping
, any suggestions? And i want to map all the unmapped values to OTHER
in my core class.
Advertisement
Answer
In your question you are saying that you can’t map PERS_LOANS
, MARGIN_LOANS
, etc. to ALL_LOANS
. However, your value mappings are reversed. You are mapping ALL_LOANS
to PERS_LOANS
.
It is not possible to map one enum (source) value to multiple enum (target) values. However, it is possible to map multiple enum (source) values to the same enum (target) value.