Skip to content

Commit

Permalink
Make lenses genration more resilient with KSP >= 1.9.x
Browse files Browse the repository at this point in the history
Obviously the KSP based LensesAnnotationProcessor was more resilient in the past before we upgraded to Kotlin 1.9.x and appropriate KSP with RC15.

We encountered some issues with generating lenses that only came with this bad warning:
```
w: [ksp] Unable to process:dev.fritz2.lens.LensesProcessor:   SomeClass
```

This was due to some not resolvable types inside the annotated data class, like calls to functions from other libs or delegated properties. This PR fixes such issues, as we ignore everything from the annotated class but the primary constructor. The latter is the only interesting piece for our lens generation.
  • Loading branch information
christian.hausknecht committed Feb 22, 2024
1 parent b2e92f8 commit b3de2b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ class LensesProcessor(
) : SymbolProcessor {

override fun process(resolver: Resolver): List<KSAnnotated> {
val checkOnlyCtor: (KSNode?, KSNode) -> Boolean = { _, node ->
when (node) {
is KSClassDeclaration -> node.primaryConstructor?.validate() ?: false
else -> false
}
}

val lensesAnnotated = resolver.getSymbolsWithAnnotation(Lenses::class.qualifiedName!!)
val unableToProcess = lensesAnnotated.filterNot { it.validate() }

lensesAnnotated.filter { it is KSClassDeclaration && it.validate() }
val unableToProcess = lensesAnnotated.filterNot { it.validate(checkOnlyCtor) }

lensesAnnotated.filter { it is KSClassDeclaration && it.validate(checkOnlyCtor) }
.forEach { it.accept(LensesVisitor(), Unit) }

return unableToProcess.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,27 @@ class LensesProcessorTests {

@ExperimentalPathApi
@Test
fun `lenses ignore none ctor or private ctor properties`() {
fun `lenses ignore none ctor or private ctor properties, other annotations, delegated none ctor properties and functions in companion`() {
val kotlinSource = SourceFile.kotlin(
"dataClassesForLensesTests.kt", """
package dev.fritz2.lenstest
import dev.fritz2.core.Lenses
// should not disturb
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@Lenses
@Serializable // should not disturb
data class Foo(val bar: Int, private val ignoredProp: Int) {
// ^^^^^^^
// private field -> no lens possible!
companion object
companion object {
// should not disturb
fun toJson(foo: Foo) = Json.decodeFromString(serializer(), foo)
}
val ignored = bar + 1 // must not appear in lens!
val ignoredDelegated by lazy { bar + 1 } // must not appear in lens!
}
"""
)
Expand Down

0 comments on commit b3de2b3

Please sign in to comment.