Skip to content

Commit

Permalink
Virtual type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Nov 21, 2024
1 parent 73da568 commit f40a8c9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit f40a8c9

Please sign in to comment.