Skip to content

Commit

Permalink
Filter classes properly #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Bethibande committed Apr 14, 2024
1 parent 3e43988 commit d86e240
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,47 @@ package com.bethibande.actors.collectors

import com.bethibande.actors.annotations.ActorState
import com.bethibande.actors.struct.ActorStateType
import com.google.devtools.ksp.isAbstract
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.validate
import com.google.devtools.ksp.symbol.Modifier
import com.squareup.kotlinpoet.ksp.toTypeName

class ActorStateCollector {

fun collect(resolver: Resolver): List<ActorStateType> {
return resolver.getSymbolsWithAnnotation(ActorState::class.java.canonicalName)
.filterIsInstance<KSClassDeclaration>()
//.filter { it.validate() }
//.filter { !it.isExpect }
//.filter { !it.isCompanionObject }
//.filter { it.parentDeclaration is KSFile } // inner classes are not supported
.map { toActorStateType(it) }
.toList()
fun collect(resolver: Resolver, environment: SymbolProcessorEnvironment): List<ActorStateType> {
val annotated = resolver.getSymbolsWithAnnotation(ActorState::class.java.canonicalName).toList()
val valid = filterValid(annotated)

if (valid.size != annotated.size) {
// report errors
val logger = environment.logger
val diff = annotated - valid.toSet()

diff.forEach { symbol ->
logger.error(
"@ActorState may only be applied to data classes that are top-level declarations.",
symbol
)
}
}

return valid.map { toActorStateType(it) }
}

private fun filterValid(annotated: List<KSAnnotated>): List<KSClassDeclaration> = annotated
.asSequence()
.filterIsInstance<KSClassDeclaration>()
.filter { it.classKind == ClassKind.CLASS }
.filter { !it.isAbstract() }
.filter { it.modifiers.contains(Modifier.DATA) }
.filter { it.parent is KSFile }
.toList()

private fun toActorStateType(declaration: KSClassDeclaration): ActorStateType {
val simpleName = declaration.simpleName.asString()
val packageName = declaration.packageName.asString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
class ActorGenerator {

fun generate(resolver: Resolver, environment: SymbolProcessorEnvironment) {
val types = ActorStateCollector().collect(resolver)
val types = ActorStateCollector().collect(resolver, environment)
types.forEach { type ->
ActorStateFieldCollector(resolver).collectFields(type)
generateActor(type, environment)
Expand Down

0 comments on commit d86e240

Please sign in to comment.