Skip to content

Commit

Permalink
fix skip property names
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Jul 8, 2024
1 parent 8bdb8a7 commit 41821a4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ internal class EntityDeserializers : SimpleDeserializers() {
parser: JsonParser,
ctx: DeserializationContext
): Map<String, KProperty1<*, *>> {
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<JsonIgnore>() == null }
.filter { prop ->
val jsonProperty = prop.findAnnotationForDeserialization<JsonProperty>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ internal class EntitySerializers : SimpleSerializers() {
}

private fun findReadableProperties(entity: Entity<*>): Map<String, KProperty1<*, *>> {
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<JsonIgnore>() == null }
.filter { prop ->
val jsonProperty = prop.findAnnotationForSerialization<JsonProperty>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunSpec> {
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]. ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -43,9 +44,10 @@ internal object PseudoConstructorFunctionGenerator {
}

internal fun buildParameters(table: TableMetadata): Sequence<ParameterSpec> {
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()
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -119,6 +120,8 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
}

private fun KSClassDeclaration.getProperties(ignoreProperties: Set<String>): Sequence<KSPropertyDeclaration> {
val skipNames = Entity::class.memberProperties.map { it.name }.toSet()

val constructorParams = HashSet<String>()
if (classKind == CLASS) {
primaryConstructor?.parameters?.mapTo(constructorParams) { it.name!!.asString() }
Expand All @@ -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 }
}

Expand Down

0 comments on commit 41821a4

Please sign in to comment.