Mappie has support for reusing mappers in other mappers using the via operator. Mappie can also apply the via
operator automatically if only one mapper for the source- and target types exist.
Suppose we have a data class Person containing a reference to a data class Address
data class Person(
val name: String,
val address: Address,
)
data class Address(
val street: String,
)
and we have the data class PersonDto referencing the data class AddressDto
data class PersonDto(
val name: String,
val addressDto: AddressDto,
)
data class AddressDto(
val street: String,
)
We start by defining a mapper for Address and AddressDto
object AddressMapper : ObjectMappie<Address, AddressDto>()
we can then reuse AddressMapper to construct a mapper for Person and PersonDto by referencing AddressMapper using
the operator via
object PersonMapper : ObjectMappie<Person, PersonDto>() {
override fun map(from: Person) = mapping {
to::addressDto fromProperty from::address via AddressMapper
}
}
Do note, that in this case, if PersonDto::addressDto was named PersonDto::address the mapping does not have to
be defined explicitly. Mappie will construct an implicit mapping using the via operator.
We can also use via to map collections. See List & Sets.