I am working with more complex classes so I try put there simple example what I need.
I have this interface:
interface BaseUser {
val firstName: String
val lastName: String
}
and these 2 data classes:
data class UserA(
override val firstName: String,
override val lastName: String,
): BaseUser
data class UserB(
override val firstName: String,
override val lastName: String,
val someComputedProperty: Int
): BaseUser
I already wrote mapper to map a database entity User into UserA, something like that:
fun UserEntity.toDto(): UserA {
return UserA(firstName = firstName, lastName = lastName)
}
I also need have similar method which map database entity to UserB and also compute value for aditional property. In real code I have a lot of properties in classes not just 2, so I am thinking how to reuse code, and use this mapping for UserB in some way, and also be able compute then from entity additional fields.
Is in Kotlin any elegant way to covert UserA into UserB thanks to interface or something else? Thank you.