Skip to content

Commit

Permalink
Pass through internal modifier for Konverter interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
unshare authored and mcarleio committed Jul 2, 2024
1 parent 6a4391c commit 5d13e5d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/annotations/konverter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ You can define custom mappings and options per function using the xref:konvert.a

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`.

[source,kotlin]
----
@Konverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ object KonverterCodeGenerator {
injectors.forEach {
it.processType(typeBuilder, konverterInterface.kSClassDeclaration)
}
if (konverterInterface.isInternal) {
typeBuilder.addModifiers(KModifier.INTERNAL)
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mcarle.konvert.processor.konvert

import com.google.devtools.ksp.isInternal
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.ksp.toTypeName

Expand All @@ -9,4 +10,5 @@ data class KonverterInterface constructor(
val simpleName = kSClassDeclaration.simpleName.asString()
val packageName = kSClassDeclaration.packageName.asString()
val typeName = kSClassDeclaration.asStarProjectedType().toTypeName()
val isInternal = kSClassDeclaration.isInternal()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,32 @@ interface Mapper {
assertContains(mapperCode, "suspend fun toTarget(source: SourceClass): TargetClass")
assertContains(mapperCode, "suspend fun toTarget(source: SourceProperty): TargetProperty")
}

@Test
fun internalInterface() {
val (compilation) = compileWith(
enabledConverters = listOf(SameTypeConverter()),
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
import io.mcarle.konvert.api.Konverter
class SourceClass(val property: String)
class TargetClass(val property: String)
@Konverter
internal interface Mapper {
fun toTarget(source: SourceClass): TargetClass
}
""".trimIndent()
)
)
val mapperCode = compilation.generatedSourceFor("MapperKonverter.kt")
println(mapperCode)

assertContains(mapperCode, "internal object MapperImpl : Mapper {")
}
}

private fun Konverter.Companion.getWithClassLoader(classFQN: String, classLoader: ClassLoader): Any {
Expand Down

0 comments on commit 5d13e5d

Please sign in to comment.