mcarleio / konvert

This kotlin compiler plugin is using KSP API and generates kotlin code to map one class to another
https://mcarleio.github.io/konvert/
Apache License 2.0
93 stars 8 forks source link

`private` interface member extension functions #30

Closed shrralis closed 1 year ago

shrralis commented 1 year ago

Having the following code

@Konverter
@KComponent
interface EntityConverter {

    fun convertToEntity(dto: DTO): Entity

    fun convertToEntityWithList(dto: DTO): Entity = convertToEntity(dto).apply {
        fillCollections(dto)
    }

    private fun Entity.fillCollections(dto: DTO) {
        list.addAll(dto.list)
        set.addAll(dto.set)
    }
}

data class DTO(
    val a: String,
    val b: String,
    val list: List<String>,
    val set: Set<String>,
)

class Entity(
    val a: String,
    val b: String,
) {

    val list: MutableList<String> = mutableListOf()
    val set: MutableSet<String> = mutableSetOf()
}

leads to an error: 'fillCollections' overrides nothing

The result implementation of the Konverter looks like this:

@Component
public object EntityConverterImpl : EntityConverter {
  @GeneratedKonverter(priority = 5_000)
  override fun convertToEntity(dto: DTO): Entity = Entity(
    a = dto.a,
    b = dto.b
  )

  @GeneratedKonverter(priority = 2_000)
  override fun convertToEntityWithList(dto: DTO): Entity = super.convertToEntityWithList(dto)

  @GeneratedKonverter(priority = 2_000)
  override fun fillCollections(dto: DTO): Unit = super.fillCollections(dto) // <-- here are problems
}

Problem 1: it should not override private functions Problem 2: even if the function isn't private, it still should not try to override a member extension function or it should use a proper workaround somehow, please see https://youtrack.jetbrains.com/issue/KT-11488