Skip to content

Commit

Permalink
Create columnTypeClassName extension property
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Oct 25, 2024
1 parent 8a7e9d2 commit 46644f9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/main/kotlin/ch/icken/processor/ColumnType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ import kotlin.reflect.KClass
@Target(PROPERTY)
annotation class ColumnType(
val type: KClass<*>
)
) {
companion object {
internal const val TYPE = "type"
}
}
8 changes: 7 additions & 1 deletion src/main/kotlin/ch/icken/processor/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package ch.icken.processor

import ch.icken.processor.QualifiedNames.ProcessorColumnType
import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.Annotatable
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ksp.toClassName

internal fun KSDeclaration.isClass(qualifiedClassName: String): Boolean =
qualifiedName?.asString() == qualifiedClassName
Expand All @@ -36,13 +39,16 @@ internal fun KSAnnotated.hasAnnotation(qualifiedAnnotationClassName: String): Bo
internal fun KSAnnotated.annotation(qualifiedAnnotationClassName: String): KSAnnotation? =
annotations.filter { it.isClass(qualifiedAnnotationClassName) }.singleOrNull()

internal operator fun List<KSValueArgument>.get(name: String): Any? = find { it.name?.asString() == name }?.value
private operator fun List<KSValueArgument>.get(name: String): Any? = find { it.name?.asString() == name }?.value

internal fun KSAnnotation?.hasNonDefaultParameter(parameterName: String): Boolean =
this != null && defaultArguments[parameterName] != arguments[parameterName]

internal val KSPropertyDeclaration.typeName: String
get() = type.resolve().declaration.simpleName.asString()

internal val KSPropertyDeclaration.columnTypeClassName: ClassName?
get() = (annotation(ProcessorColumnType)?.arguments?.get(ColumnType.TYPE) as? KSType)?.toClassName()

fun <T : Annotatable.Builder<T>> T.addAnnotationIf(annotationSpec: AnnotationSpec, condition: Boolean) =
apply { if (condition) addAnnotation(annotationSpec) }
10 changes: 2 additions & 8 deletions src/main/kotlin/ch/icken/processor/PanacheEntityBaseProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import ch.icken.processor.QualifiedNames.JakartaPersistenceManyToMany
import ch.icken.processor.QualifiedNames.JakartaPersistenceOneToMany
import ch.icken.processor.QualifiedNames.JakartaPersistenceOneToOne
import ch.icken.processor.QualifiedNames.JakartaPersistenceTransient
import ch.icken.processor.QualifiedNames.ProcessorColumnType
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter
Expand Down Expand Up @@ -105,11 +103,8 @@ class PanacheEntityBaseProcessor(
.initializer("%T(%S)", joinBaseClass, "$propertyName.")
} else {
val ksPropertyType = ksProperty.type.resolve()
val columnTypeParameter = (
(ksProperty.annotation(ProcessorColumnType)
?.arguments?.get(TYPE) as? KSType)
?.toClassName() ?: ksPropertyType.toClassName()
).copy(nullable = ksPropertyType.isMarkedNullable)
val columnTypeParameter = (ksProperty.columnTypeClassName ?: ksPropertyType.toClassName())
.copy(nullable = ksPropertyType.isMarkedNullable)

PropertySpec.builder(propertyName, ColumnClassName.plusParameter(columnTypeParameter))
.initializer("%T(%P)", ColumnClassName,
Expand Down Expand Up @@ -137,7 +132,6 @@ class PanacheEntityBaseProcessor(

companion object {
internal const val MAPPED_BY = "mappedBy"
internal const val TYPE = "type"

private val GeneratedAnnotation = generatedAnnotation(PanacheEntityBaseProcessor::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import ch.icken.processor.ClassNames.ListClassName
import ch.icken.processor.ClassNames.StringClassName
import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION
import ch.icken.processor.PanacheEntityBaseProcessor.Companion.MAPPED_BY
import ch.icken.processor.PanacheEntityBaseProcessor.Companion.TYPE
import ch.icken.processor.QualifiedNames.HibernatePanacheEntityBase
import ch.icken.processor.QualifiedNames.JakartaPersistenceEntity
import ch.icken.processor.QualifiedNames.JakartaPersistenceJoinColumn
Expand Down Expand Up @@ -251,7 +250,7 @@ class PanacheEntityBaseProcessorMockTests : ProcessorMockTestCommon() {
every { lastNameTypeReference.resolve() } returns lastNameType

val lastNameColumnTypeParameterName = mockk<KSName>()
every { lastNameColumnTypeParameterName.asString() } returns TYPE
every { lastNameColumnTypeParameterName.asString() } returns ColumnType.TYPE

val lastNameColumnTypeParameter = mockk<KSValueArgument>()
every { lastNameColumnTypeParameter.name } returns lastNameColumnTypeParameterName
Expand Down

0 comments on commit 46644f9

Please sign in to comment.