Skip to content

Commit

Permalink
refactor(SuperTableClass): rename
Browse files Browse the repository at this point in the history
  • Loading branch information
qumn committed Jun 17, 2024
1 parent 878526a commit 1be3b09
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn

_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: $className")
val table = cls.getAnnotationsByType(Table::class).first()
val (superClass, superTableClasses) = parseSuperTableClass(cls)
val allPropertyNamesOfSuperTables = superTableClasses.flatMap { it.getProperties(emptySet()) }.map { it.simpleName.asString() }

val (finalSuperClass, allSuperTableClasses) = parseSuperTableClass(cls)
val shouldIgnorePropertyNames = allSuperTableClasses.flatMap { it.getProperties(emptySet()) }.map { it.simpleName.asString() }

val tableMetadata = TableMetadata(
entityClass = cls,
Expand All @@ -108,9 +109,9 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
schema = table.schema.ifEmpty { _options["ktorm.schema"] }?.takeIf { it.isNotEmpty() },
tableClassName = table.className.ifEmpty { _codingNamingStrategy.getTableClassName(cls) },
entitySequenceName = table.entitySequenceName.ifEmpty { _codingNamingStrategy.getEntitySequenceName(cls) },
ignoreProperties = table.ignoreProperties.toSet() + allPropertyNamesOfSuperTables, // ignore properties of super tables
ignoreProperties = table.ignoreProperties.toSet() + shouldIgnorePropertyNames,
columns = ArrayList(),
superClass = superClass
superClass = finalSuperClass
)

val columns = tableMetadata.columns as MutableList
Expand Down Expand Up @@ -283,14 +284,14 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
}

/**
* @return the super table class and all class be annotated with [SuperTableClass] in the inheritance hierarchy.
* @return the final super table class and all super table classes in the inheritance hierarchy.
*/
@OptIn(KotlinPoetKspPreview::class)
private fun parseSuperTableClass(cls: KSClassDeclaration): Pair<ClassName, Set<KSClassDeclaration>> {
val superTableClassAnnPair = cls.findAllAnnotationsInInheritanceHierarchy(SuperTableClass::class.qualifiedName!!)
val entityAnnPairs = cls.findAnnotationsInHierarchy(SuperTableClass::class.qualifiedName!!)

// if there is no SuperTableClass annotation, return the default super table class based on the class kind.
if (superTableClassAnnPair.isEmpty()) {
if (entityAnnPairs.isEmpty()) {
return if (cls.classKind == INTERFACE) {
org.ktorm.schema.Table::class.asClassName() to emptySet()
} else {
Expand All @@ -299,13 +300,13 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
}

// SuperTableClass annotation can only be used on interface
if (superTableClassAnnPair.map { it.first }.any { it.classKind != INTERFACE }) {
if (entityAnnPairs.map { it.first }.any { it.classKind != INTERFACE }) {
val msg = "SuperTableClass annotation can only be used on interface."
throw IllegalArgumentException(msg)
}

// find the last annotation in the inheritance hierarchy
val superTableClasses = superTableClassAnnPair
val superTableClasses = entityAnnPairs
.map { it.second }
.map { it.arguments.single { it.name?.asString() == SuperTableClass::value.name } }
.map { it.value as KSType }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ internal fun KSClassDeclaration.findSuperTypeReference(name: String): KSTypeRefe
* Find all annotations with the given name in the inheritance hierarchy of this class.
*
* @param name the qualified name of the annotation.
* @return a list of pairs, each pair contains the class declaration and the annotation.
*/
internal fun KSClassDeclaration.findAllAnnotationsInInheritanceHierarchy(name: String): List<Pair<KSClassDeclaration, KSAnnotation>> {
val rst = mutableListOf<Pair<KSClassDeclaration, KSAnnotation>>()
internal fun KSClassDeclaration.findAnnotationsInHierarchy(name: String): List<Pair<KSClassDeclaration, KSAnnotation>> {
val pairs = mutableListOf<Pair<KSClassDeclaration, KSAnnotation>>()

fun KSClassDeclaration.collectAnnotations() {
rst += annotations.filter { it.annotationType.resolve().declaration.qualifiedName?.asString() == name }.map { this to it }
pairs += annotations.filter { it.annotationType.resolve().declaration.qualifiedName?.asString() == name }.map { this to it }
superTypes.forEach { (it.resolve().declaration as KSClassDeclaration).collectAnnotations() }
}

collectAnnotations()
return rst
return pairs
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ class SuperTableClassTest : BaseKspTest() {
interface CEntity : AEntity<CEntity>, BEntity<CEntity> {
var c: Int
}
fun run() {
assert(CTable::class.isSubclassOf(ATable::class))
}
""".trimIndent()
)

Expand Down

0 comments on commit 1be3b09

Please sign in to comment.