Skip to main content Link Menu Expand (external link) Document Search Copy Copied

@Konverter

The annotation @Konverter can only be applied to interfaces and will generate an implementation object or class. Each abstract function which satisfies having exactly one parameter and a return type, will be implemented by generating the mapping code. You can define custom mappings and options per function using the @Konvert annotation.

If a member function is a suspend fun, the generated counterpart is also a suspend fun.

If a @Konverter-annotated interface is internal, the generated object or class is also internal.

@Konverter
interface PersonMapper {
    fun toDTO(person: Person): PersonDto
    fun fromDTO(dto: PersonDto): Person
}

class Person(val name: String, val age: Int)
class PersonDto(val name: String, val age: Int)

This will generate

object PersonMapperImpl: PersonMapper {
    override fun toDTO(person: Person): PersonDto {
        return PersonDto(name = person.name, age = person.age)
    }
    override fun fromDTO(dto: PersonDto): Person {
        return Person(name = dto.name, age = dto.age)
    }
}

Parameters

options

You can define custom options for this annotation processing. See here for available options.