From dbff05f92d359729964ee5c20d57682f5ee8151b Mon Sep 17 00:00:00 2001 From: qiaoyuang Date: Tue, 18 Jun 2024 14:27:16 +0800 Subject: [PATCH] Support kotlinx.serialization.Transient --- CHANGELOG.md | 14 ++++++++++++++ gradle.properties | 6 +++--- sqllin-dsl/doc/getting-start-cn.md | 2 +- sqllin-dsl/doc/getting-start.md | 3 ++- .../kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt | 1 - .../ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt | 2 ++ .../com/ctrip/sqllin/processor/ClauseProcessor.kt | 9 +++++++-- 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b0b00..b5aedde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ - Date format: YYYY-MM-dd +## v1.3.2 / 2024-06-18 + +### All + +* Update `Kotlin`'s version to `1.9.24` + +### sqllin-dsl + +* Now, you can annotate properties with `kotlinx.serialization.transmint` in your data classes to ignore these properties when serialization or deserialization and `Table` classes generation. + +### sqllin-processor + +* Update `KSP`'s version to `1.9.24-1.0.20` + ## v1.3.1 / 2024-04-24 ### sqllin-dsl diff --git a/gradle.properties b/gradle.properties index 1a1259c..e680c9b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,8 @@ -VERSION=1.3.1 +VERSION=1.3.2 GROUP=com.ctrip.kotlin -kotlinVersion=1.9.23 -kspVersion=1.9.23-1.0.20 +kotlinVersion=1.9.24 +kspVersion=1.9.24-1.0.20 serializationVersion=1.6.3 coroutinesVersion=1.8.0 androidxAnnotationVersion=1.7.1 diff --git a/sqllin-dsl/doc/getting-start-cn.md b/sqllin-dsl/doc/getting-start-cn.md index 7331274..88b3e63 100644 --- a/sqllin-dsl/doc/getting-start-cn.md +++ b/sqllin-dsl/doc/getting-start-cn.md @@ -154,7 +154,7 @@ data class Person( 将会使用类名作为表名,比如 `Person` 类的默认表名是"Person"。 在 _sqllin-dsl_ 中,对象序列化为 SQL 语句,或者从游标中反序列化依赖 _kotlinx.serialization_,所以你需要在你的 data class -上添加 `@Serializable` 注解。 +上添加 `@Serializable` 注解。因此,如果你想在序列化或反序列化以及 `Table` 类生成的时候忽略某些属性,你可以给你的属性添加 `kotlinx.serialization.Transient` 注解。 ## 接下来 diff --git a/sqllin-dsl/doc/getting-start.md b/sqllin-dsl/doc/getting-start.md index 74b3342..26eda6c 100644 --- a/sqllin-dsl/doc/getting-start.md +++ b/sqllin-dsl/doc/getting-start.md @@ -164,7 +164,8 @@ The `@DBRow`'s param `tableName` represents the table name in Database, please e the correct value. If you don't pass the parameter manually, _sqllin-processor_ will use the class name as table name, for example, `Person`'s default table name is "Person". -In _sqllin-dsl_, objects are serialized to SQL and deserialized from cursor depend on _kotlinx.serialization_. So, you also need to add the `@Serializable` onto your data classes. +In _sqllin-dsl_, objects are serialized to SQL and deserialized from cursor depend on _kotlinx.serialization_. So, you also need to add the `@Serializable` onto your data classes. Therefore, if +you want to ignore some properties when serialization or deserialization and `Table` classes generation, you can annotate your properties with `kotlinx.serialization.Transient`. ## Next Step diff --git a/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt b/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt index acb7ff7..3816949 100644 --- a/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt +++ b/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt @@ -24,7 +24,6 @@ import com.ctrip.sqllin.dsl.sql.clause.OrderByWay.ASC import com.ctrip.sqllin.dsl.sql.clause.OrderByWay.DESC import com.ctrip.sqllin.dsl.sql.statement.SelectStatement import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlin.test.assertEquals diff --git a/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt b/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt index 7ef58e4..d6b8516 100644 --- a/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt +++ b/sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt @@ -18,6 +18,7 @@ package com.ctrip.sqllin.dsl import com.ctrip.sqllin.dsl.annotation.DBRow import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient /** * Test whether the sqllin-processor could generate primitive type and String correctly @@ -40,4 +41,5 @@ data class TestPrimitiveTypeForKSP( val testBoolean: Boolean?, val testChar: Char?, val testString: String, + @Transient val testTransient: Int = 0, ) \ No newline at end of file diff --git a/sqllin-processor/src/main/kotlin/com/ctrip/sqllin/processor/ClauseProcessor.kt b/sqllin-processor/src/main/kotlin/com/ctrip/sqllin/processor/ClauseProcessor.kt index 222b9a1..8784114 100644 --- a/sqllin-processor/src/main/kotlin/com/ctrip/sqllin/processor/ClauseProcessor.kt +++ b/sqllin-processor/src/main/kotlin/com/ctrip/sqllin/processor/ClauseProcessor.kt @@ -16,6 +16,7 @@ package com.ctrip.sqllin.processor +import com.google.devtools.ksp.getClassDeclarationByName import com.google.devtools.ksp.processing.Dependencies import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.processing.SymbolProcessor @@ -36,6 +37,7 @@ class ClauseProcessor( private companion object { const val ANNOTATION_DATABASE_ROW_NAME = "com.ctrip.sqllin.dsl.annotation.DBRow" const val ANNOTATION_SERIALIZABLE = "kotlinx.serialization.Serializable" + const val ANNOTATION_TRANSIENT = "kotlinx.serialization.Transient" } @Suppress("UNCHECKED_CAST") @@ -44,7 +46,7 @@ class ClauseProcessor( val invalidateDBRowClasses = allDBRowClasses.filter { !it.validate() }.toList() val validateDBRowClasses = allDBRowClasses.filter { it.validate() } as Sequence - val serializableType = resolver.getClassDeclarationByName(resolver.getKSNameFromString(ANNOTATION_SERIALIZABLE))!!.asStarProjectedType() + val serializableType = resolver.getClassDeclarationByName(ANNOTATION_SERIALIZABLE)!!.asStarProjectedType() for (classDeclaration in validateDBRowClasses) { @@ -80,7 +82,10 @@ class ClauseProcessor( writer.write(" override fun kSerializer() = $className.serializer()\n\n") writer.write(" inline operator fun invoke(block: $objectName.(table: $objectName) -> R): R = this.block(this)\n\n") - classDeclaration.getAllProperties().forEachIndexed { index, property -> + val transientName = resolver.getClassDeclarationByName(ANNOTATION_TRANSIENT)!!.asStarProjectedType() + classDeclaration.getAllProperties().filter { classDeclaration -> + !classDeclaration.annotations.any { ksAnnotation -> ksAnnotation.annotationType.resolve().isAssignableFrom(transientName) } + }.forEachIndexed { index, property -> val clauseElementTypeName = getClauseElementTypeStr(property) ?: return@forEachIndexed val propertyName = property.simpleName.asString() val elementName = "$className.serializer().descriptor.getElementName($index)"