Skip to content

Commit

Permalink
GH-80 Support NULL_VALUE const val to represent raw NULL values (Resolve
Browse files Browse the repository at this point in the history
 #80)
  • Loading branch information
dzikoysk committed Sep 1, 2024
1 parent 255b7d8 commit 439244b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dzikoysk.sqiffy.processor.generators

import com.dzikoysk.sqiffy.definition.DataType
import com.dzikoysk.sqiffy.definition.NULL_VALUE
import com.dzikoysk.sqiffy.definition.ParsedDefinition
import com.dzikoysk.sqiffy.definition.PropertyData
import com.dzikoysk.sqiffy.dsl.Row
Expand Down Expand Up @@ -145,8 +146,12 @@ class EntityGenerator(private val context: KspContext) {
.build()
)

private fun String.toKotlinCode(dataType: DataType, typeName: TypeName): String =
when (dataType) {
private fun String.toKotlinCode(dataType: DataType, typeName: TypeName): String {
if (this == NULL_VALUE) {
return "null"
}

return when (dataType) {
/* Special types */
DataType.UUID_TYPE -> "UUID.fromString(\"$this\")"
DataType.ENUM -> "$typeName.$this"
Expand All @@ -165,5 +170,5 @@ class EntityGenerator(private val context: KspContext) {
DataType.TIMESTAMP -> "Instant.parse(\"$this\")"
else -> this
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.dzikoysk.sqiffy.definition.DataType.SERIAL
import com.dzikoysk.sqiffy.definition.DataType.TEXT
import com.dzikoysk.sqiffy.definition.DataType.UUID_TYPE
import com.dzikoysk.sqiffy.definition.DataType.VARCHAR
import com.dzikoysk.sqiffy.definition.NULL_VALUE
import com.dzikoysk.sqiffy.definition.PropertyData

abstract class GenericSqlSchemeGenerator : SqlSchemeGenerator {
Expand Down Expand Up @@ -98,15 +99,19 @@ abstract class GenericSqlSchemeGenerator : SqlSchemeGenerator {
.let { dataType -> "$dataType ${property.default?.let { default -> "DEFAULT ${default.toSqlDefault(property)} " } ?: ""}" }
.let { if (!property.nullable) "$it NOT NULL" else it }

private fun String.toSqlDefault(property: PropertyData): String =
when (property.rawDefault) {
true -> property.default!!
else ->
createSqlDefault(this, property)
?: createRegularDefault(this, property)
?: throw UnsupportedOperationException("Cannot create default value based on $this")
private fun String.toSqlDefault(property: PropertyData): String {
if (property.rawDefault) {
return property.default!!
}

if (property.default == NULL_VALUE) {
return "NULL"
}

return createSqlDefault(this, property)
?: createRegularDefault(this, property)
?: throw UnsupportedOperationException("Cannot create default value based on $this")
}

abstract fun createSqlDefault(rawDefault: String, property: PropertyData, dataType: DataType = property.type!!): String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import java.util.UUID
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.reflect.KClass

const val NULL_STRING = "~NULL-STRING~"
object NULL_CLASS
const val NULL_STRING = "~NULL-STRING~"
const val NULL_VALUE = "~NULL-VALUE~"

enum class Kind {
DIRECT,
Expand Down

0 comments on commit 439244b

Please sign in to comment.