From f40a8c9ab3bb2a9d6436af697f870c5114f01555 Mon Sep 17 00:00:00 2001 From: Joseph Ivie Date: Thu, 21 Nov 2024 09:05:47 -0700 Subject: [PATCH] Virtual type fix --- .../com/lightningkite/lightningdb/AnnotationProcessor.kt | 2 +- .../main/kotlin/com/lightningkite/lightningdb/Field.kt | 9 ++++++--- .../lightningdb/testing/VirtualTypesTest.kt | 5 ++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/processor/src/main/kotlin/com/lightningkite/lightningdb/AnnotationProcessor.kt b/processor/src/main/kotlin/com/lightningkite/lightningdb/AnnotationProcessor.kt index 9e3d65c1..2d7de038 100644 --- a/processor/src/main/kotlin/com/lightningkite/lightningdb/AnnotationProcessor.kt +++ b/processor/src/main/kotlin/com/lightningkite/lightningdb/AnnotationProcessor.kt @@ -16,7 +16,7 @@ lateinit var comparable: KSClassDeclaration class TableGenerator( val codeGenerator: CodeGenerator, val logger: KSPLogger, -) : CommonSymbolProcessor2(codeGenerator, "lightningdb", 11) { +) : CommonSymbolProcessor2(codeGenerator, "lightningdb", 16) { fun KSClassDeclaration.isSerializable(): Boolean = this.annotation("Serializable", "kotlinx.serialization") != null fun KSClassDeclaration.isPlainSerializable(): Boolean = this.annotation("Serializable", "kotlinx.serialization")?.let { diff --git a/processor/src/main/kotlin/com/lightningkite/lightningdb/Field.kt b/processor/src/main/kotlin/com/lightningkite/lightningdb/Field.kt index 9d985c07..6502f122 100644 --- a/processor/src/main/kotlin/com/lightningkite/lightningdb/Field.kt +++ b/processor/src/main/kotlin/com/lightningkite/lightningdb/Field.kt @@ -12,15 +12,18 @@ data class Field( } private fun toField(owner: KSClassDeclaration, param: KSValueParameter, property: KSPropertyDeclaration): Field { + val regexes = owner.primaryConstructor?.parameters?.map { other -> + Regex("([^a-zA-Z0-9]|^)${other.name?.asString() ?: "xxx"}([^a-zA-Z0-9]|$)") + } ?: listOf() return Field( name = property.simpleName.getShortName(), kotlinType = property.type, annotations = property.annotations.map { it.resolve() }.toList(), default = param.defaultText?.takeUnless { - owner.primaryConstructor?.parameters?.any { other -> - it.contains(other.name?.asString() ?: "???") + regexes.any { r -> + r.find(it) != null } == true - }, + }?.plus(" // ${regexes.joinToString { it.pattern }}"), ) } diff --git a/shared/src/commonTest/kotlin/com/lightningkite/lightningdb/testing/VirtualTypesTest.kt b/shared/src/commonTest/kotlin/com/lightningkite/lightningdb/testing/VirtualTypesTest.kt index 7660c5b0..be94a444 100644 --- a/shared/src/commonTest/kotlin/com/lightningkite/lightningdb/testing/VirtualTypesTest.kt +++ b/shared/src/commonTest/kotlin/com/lightningkite/lightningdb/testing/VirtualTypesTest.kt @@ -36,7 +36,7 @@ class VirtualTypesTest { println("Schema: ${json.encodeToString(vtype)}") val original = instance // forward - json.decodeFromString(vtypeSerializer, json.encodeToString(serializer, original)) + json.decodeFromString(vtypeSerializer, json.encodeToString(serializer, original).also { println(it) }) val string = json.encodeToString(serializer, original) println(string) @@ -76,7 +76,10 @@ class VirtualTypesTest { ) } @Test fun testStructure() { + LargeTestModel_list testVirtualVersion(LargeTestModel.serializer(), LargeTestModel()) + } + @Test fun testStructureWithoutDefaults() { testVirtualVersion(LargeTestModel.serializer(), LargeTestModel()) { encodeDefaults = false } } @Test fun testGeneric() {