0

I am carrying our a POC with mapstruct for my current kotlin project

api 'org.mapstruct:mapstruct:1.5.2.Final'
kapt 'org.mapstruct:mapstruct-processor:1.5.2.Final'

mapstruct is working fine in the majority of cases however i have encountered one issue when attempting to map between two classes and add an additional generated value as shown below:-

source class

data class MyNetworkData(
    val id: String,
    val isLeaf: Boolean,
    val name: String,
    val parentId: String?
)

target class

data class MyDataDO(
    @ColumnInfo(name = "id") val id: String,
    @ColumnInfo(name = "is_leaf") val isLeaf: Boolean,
    @ColumnInfo(name = "level") val level: Long,
    @ColumnInfo(name = "name") val name: String,
    @ColumnInfo(name = "parent_id") val parentId: String?
)

My Mapstruct mapper resembles this

@Mapper
interface MyDataDoMapper {
    @InheritInverseConfiguration(name = "map")
    @Mapping(target = "level", ignore = true)
    @Mapping(target = "parentId", source = "parentID")
    @Mapping(target = "isLeaf", source = "leaf")
    fun map(data: MyNetworkData): MyDataDO
}

the generated field is level in MyDataDO, i have had to ignore it when mapping with mapstruct then use copy method to set the level field as follows

rows.add(MY_DATA_DO_MAPPER.map(myNetworkdata).copy(level = level))

is there any way i can pass the generated level field to the mapper and have it assign the value to the target class during the mapping process?

Hector
  • 4,016
  • 21
  • 112
  • 211

0 Answers0