From 41821a4a897cf5a616a6a868b47173a1fb08fc12 Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 8 Jul 2024 11:19:17 +0800 Subject: [PATCH] fix skip property names --- .../main/kotlin/org/ktorm/jackson/EntityDeserializers.kt | 3 ++- .../src/main/kotlin/org/ktorm/jackson/EntitySerializers.kt | 3 ++- .../ksp/compiler/generator/ComponentFunctionGenerator.kt | 5 ++++- .../generator/PseudoConstructorFunctionGenerator.kt | 7 +++++-- .../kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt | 5 ++++- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntityDeserializers.kt b/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntityDeserializers.kt index 6776f776..00474bad 100644 --- a/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntityDeserializers.kt +++ b/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntityDeserializers.kt @@ -63,10 +63,11 @@ internal class EntityDeserializers : SimpleDeserializers() { parser: JsonParser, ctx: DeserializationContext ): Map> { + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() return entityClass.memberProperties .asSequence() .filter { it.isAbstract } - .filter { it.name != "entityClass" && it.name != "properties" } + .filter { it.name !in skipNames } .filter { it.findAnnotationForDeserialization() == null } .filter { prop -> val jsonProperty = prop.findAnnotationForDeserialization() diff --git a/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntitySerializers.kt b/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntitySerializers.kt index aeab57ec..59fce3cc 100644 --- a/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntitySerializers.kt +++ b/ktorm-jackson/src/main/kotlin/org/ktorm/jackson/EntitySerializers.kt @@ -62,10 +62,11 @@ internal class EntitySerializers : SimpleSerializers() { } private fun findReadableProperties(entity: Entity<*>): Map> { + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() return entity.entityClass.memberProperties .asSequence() .filter { it.isAbstract } - .filter { it.name != "entityClass" && it.name != "properties" } + .filter { it.name !in skipNames } .filter { it.findAnnotationForSerialization() == null } .filter { prop -> val jsonProperty = prop.findAnnotationForSerialization() diff --git a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/ComponentFunctionGenerator.kt b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/ComponentFunctionGenerator.kt index 0846cd73..dd2317fc 100644 --- a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/ComponentFunctionGenerator.kt +++ b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/ComponentFunctionGenerator.kt @@ -22,16 +22,19 @@ import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview import com.squareup.kotlinpoet.ksp.toClassName import com.squareup.kotlinpoet.ksp.toTypeName +import org.ktorm.entity.Entity import org.ktorm.ksp.compiler.util._type import org.ktorm.ksp.spi.TableMetadata +import kotlin.reflect.full.memberProperties @OptIn(KotlinPoetKspPreview::class) internal object ComponentFunctionGenerator { fun generate(table: TableMetadata): Sequence { + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() return table.entityClass.getAllProperties() .filter { it.isAbstract() } - .filterNot { it.simpleName.asString() in setOf("entityClass", "properties") } + .filterNot { it.simpleName.asString() in skipNames } .mapIndexed { i, prop -> FunSpec.builder("component${i + 1}") .addKdoc("Return the value of [%L.%L]. ", diff --git a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/PseudoConstructorFunctionGenerator.kt b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/PseudoConstructorFunctionGenerator.kt index 046467ce..d48c7b94 100644 --- a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/PseudoConstructorFunctionGenerator.kt +++ b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/generator/PseudoConstructorFunctionGenerator.kt @@ -25,6 +25,7 @@ import org.ktorm.entity.Entity import org.ktorm.ksp.annotation.Undefined import org.ktorm.ksp.compiler.util.* import org.ktorm.ksp.spi.TableMetadata +import kotlin.reflect.full.memberProperties @OptIn(KotlinPoetKspPreview::class) internal object PseudoConstructorFunctionGenerator { @@ -43,9 +44,10 @@ internal object PseudoConstructorFunctionGenerator { } internal fun buildParameters(table: TableMetadata): Sequence { + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() return table.entityClass.getAllProperties() .filter { it.isAbstract() } - .filterNot { it.simpleName.asString() in setOf("entityClass", "properties") } + .filterNot { it.simpleName.asString() in skipNames } .map { prop -> val propName = prop.simpleName.asString() val propType = prop._type.makeNullable().toTypeName() @@ -63,8 +65,9 @@ internal object PseudoConstructorFunctionGenerator { addStatement("val·entity·=·%T.create<%T>()", Entity::class.asClassName(), table.entityClass.toClassName()) } + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() for (prop in table.entityClass.getAllProperties()) { - if (!prop.isAbstract() || prop.simpleName.asString() in setOf("entityClass", "properties")) { + if (!prop.isAbstract() || prop.simpleName.asString() in skipNames) { continue } diff --git a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt index 55b94489..872b1802 100644 --- a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt +++ b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt @@ -31,6 +31,7 @@ import org.ktorm.ksp.spi.TableMetadata import org.ktorm.schema.TypeReference import java.lang.reflect.InvocationTargetException import java.util.* +import kotlin.reflect.full.memberProperties import kotlin.reflect.jvm.jvmName @OptIn(KspExperimental::class) @@ -119,6 +120,8 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn } private fun KSClassDeclaration.getProperties(ignoreProperties: Set): Sequence { + val skipNames = Entity::class.memberProperties.map { it.name }.toSet() + val constructorParams = HashSet() if (classKind == CLASS) { primaryConstructor?.parameters?.mapTo(constructorParams) { it.name!!.asString() } @@ -129,7 +132,7 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn .filterNot { it.isAnnotationPresent(Ignore::class) } .filterNot { classKind == CLASS && !it.hasBackingField } .filterNot { classKind == INTERFACE && !it.isAbstract() } - .filterNot { classKind == INTERFACE && it.simpleName.asString() in setOf("entityClass", "properties") } + .filterNot { classKind == INTERFACE && it.simpleName.asString() in skipNames } .sortedByDescending { it.simpleName.asString() in constructorParams } }