From 34c4b194945fb9cfcdc892b0b625af82926a2c84 Mon Sep 17 00:00:00 2001 From: babyfish-ct Date: Tue, 10 Dec 2024 04:22:32 +0800 Subject: [PATCH] #822 --- project/gradle.properties | 2 +- .../org/babyfish/jimmer/kt/model/Book.kt | 4 +-- .../resources/META-INF/jimmer/code_generation | 2 +- .../generator/ValidationGenerator.kt | 36 +++++++++---------- .../ksp/immutable/meta/ImmutableProp.kt | 10 ++++++ .../jimmer/sql/kt/model/classic/book/Book.kt | 6 ++-- 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/project/gradle.properties b/project/gradle.properties index 8f776f5109..af499800f8 100644 --- a/project/gradle.properties +++ b/project/gradle.properties @@ -1,2 +1,2 @@ group=org.babyfish.jimmer -version=0.9.26 +version=0.9.27 diff --git a/project/jimmer-core-kotlin/src/test/kotlin/org/babyfish/jimmer/kt/model/Book.kt b/project/jimmer-core-kotlin/src/test/kotlin/org/babyfish/jimmer/kt/model/Book.kt index eef184caa7..6e65b41a83 100644 --- a/project/jimmer-core-kotlin/src/test/kotlin/org/babyfish/jimmer/kt/model/Book.kt +++ b/project/jimmer-core-kotlin/src/test/kotlin/org/babyfish/jimmer/kt/model/Book.kt @@ -18,7 +18,7 @@ interface Book { val edition: Int @get:Positive - val price: PriceType + val price: BigDecimal @get:JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") val createdTime: LocalDateTime @@ -27,5 +27,3 @@ interface Book { val authors: List } - -typealias PriceType = BigDecimal \ No newline at end of file diff --git a/project/jimmer-core/src/main/resources/META-INF/jimmer/code_generation b/project/jimmer-core/src/main/resources/META-INF/jimmer/code_generation index 37d8b0b7d1..9a542234e8 100644 --- a/project/jimmer-core/src/main/resources/META-INF/jimmer/code_generation +++ b/project/jimmer-core/src/main/resources/META-INF/jimmer/code_generation @@ -1 +1 @@ -0.9.24 +0.9.27 diff --git a/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/generator/ValidationGenerator.kt b/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/generator/ValidationGenerator.kt index 8d1e3c2ff5..1f469ee4aa 100644 --- a/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/generator/ValidationGenerator.kt +++ b/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/generator/ValidationGenerator.kt @@ -1,15 +1,13 @@ package org.babyfish.jimmer.ksp.immutable.generator import com.google.devtools.ksp.symbol.KSAnnotation -import com.squareup.kotlinpoet.ClassName -import com.squareup.kotlinpoet.CodeBlock -import com.squareup.kotlinpoet.ParameterizedTypeName -import com.squareup.kotlinpoet.asClassName +import com.squareup.kotlinpoet.* import org.babyfish.jimmer.ksp.fullName import org.babyfish.jimmer.ksp.get import org.babyfish.jimmer.ksp.isBuiltInType import org.babyfish.jimmer.ksp.immutable.meta.ImmutableProp import org.babyfish.jimmer.ksp.MetaException +import org.babyfish.jimmer.ksp.client.ClientProcessor.Companion.toTypeName import java.math.BigDecimal import java.math.BigInteger import java.time.LocalDate @@ -160,15 +158,20 @@ class ValidationGenerator( if (annotations.isEmpty()) { return } - if (!prop.typeName().isBuiltInType() && - !isSimpleType(BigInteger::class) && - !isSimpleType(BigDecimal::class) + if (!isSimpleType(Byte::class) && + !isSimpleType(Short::class) && + !isSimpleType(Int::class) && + !isSimpleType(Long::class) && + !isSimpleType(Float::class) && + !isSimpleType(Double::class) && + !isSimpleType(BigInteger::class) && + !isSimpleType(BigDecimal::class) ) { throw MetaException( prop.propDeclaration, "it's decorated by the annotation @" + annotations[0].fullName + - " but its type is numeric" + " but its type is not numeric" ) } var minValue: BigDecimal? = null @@ -551,17 +554,11 @@ class ValidationGenerator( private fun isSimpleType(type: KClass<*>): Boolean { val className = when (val typeName = prop.typeName()) { - is ClassName -> typeName - is ParameterizedTypeName -> typeName.rawType + is ClassName -> prop.realDeclaration.qualifiedName!!.asString() + is ParameterizedTypeName -> ClassName(typeName.rawType.packageName, typeName.rawType.simpleNames) else -> return false - }.let { - if (it.isNullable) { - it.copy(nullable = false) - } else { - it - } } - return className == type.asClassName() + return className == type.qualifiedName } private fun validateBound(bound: BigDecimal, cmp: String, message: String?) { @@ -592,8 +589,9 @@ class ValidationGenerator( }, if (bigNumLiteral != null) { arrayOf(prop.name, prop.typeName(overrideNullable = false), cmp) - } - else { + } else if (prop.typeAlias !== null) { + arrayOf(prop.name, cmp, "${prop.typeAlias.qualifiedName!!.asString()}(${bound})") + } else { arrayOf(prop.name, cmp, bound) }, message diff --git a/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/meta/ImmutableProp.kt b/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/meta/ImmutableProp.kt index 1e1e19850e..5a35834550 100644 --- a/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/meta/ImmutableProp.kt +++ b/project/jimmer-ksp/src/main/kotlin/org/babyfish/jimmer/ksp/immutable/meta/ImmutableProp.kt @@ -38,6 +38,16 @@ class ImmutableProp( private val resolvedType: KSType = propDeclaration.type.resolve() + val typeAlias: KSTypeAlias? = resolvedType.declaration as? KSTypeAlias + + val realDeclaration: KSDeclaration = resolvedType.declaration.let { + if (it is KSTypeAlias) { + it.findActualType() + } else { + it + } + } + init { if (propDeclaration.isMutable) { throw MetaException( diff --git a/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/model/classic/book/Book.kt b/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/model/classic/book/Book.kt index bfbcc2ff2a..a8e78c4845 100644 --- a/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/model/classic/book/Book.kt +++ b/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/model/classic/book/Book.kt @@ -37,7 +37,7 @@ interface Book { /** * The price property, 100% immutable */ - val price: @Positive BigDecimal + val price: @Positive PriceType /** * The store property, 100% immutable @@ -71,4 +71,6 @@ interface Book { @Formula(dependencies = ["authors.firstName", "authors.lastName"]) val authorFullNames: List get() = authors.map { "${it.firstName} ${it.lastName}" } -} \ No newline at end of file +} + +typealias PriceType = BigDecimal \ No newline at end of file