Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Dec 9, 2024
1 parent 137f811 commit 34c4b19
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.26
version=0.9.27
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,5 +27,3 @@ interface Book {

val authors: List<Author>
}

typealias PriceType = BigDecimal
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.24
0.9.27
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface Book {
/**
* The price property, 100% immutable
*/
val price: @Positive BigDecimal
val price: @Positive PriceType

/**
* The store property, 100% immutable
Expand Down Expand Up @@ -71,4 +71,6 @@ interface Book {
@Formula(dependencies = ["authors.firstName", "authors.lastName"])
val authorFullNames: List<String>
get() = authors.map { "${it.firstName} ${it.lastName}" }
}
}

typealias PriceType = BigDecimal

0 comments on commit 34c4b19

Please sign in to comment.