Skip to content

Commit

Permalink
Merge pull request #855 from jwstegemann/chausknecht/fix-ksp
Browse files Browse the repository at this point in the history
Make lenses genration more resilient with KSP >= 1.9.x
  • Loading branch information
jamowei committed Feb 23, 2024
2 parents b2e92f8 + b3de2b3 commit 7669d2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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
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 7669d2a

Please sign in to comment.