|
| 1 | +package de.quati.pgen.core.util |
| 2 | + |
| 3 | +import de.quati.pgen.core.column.DomainColumnType |
| 4 | +import kotlinx.datetime.LocalDate |
| 5 | +import kotlinx.datetime.LocalTime |
| 6 | +import kotlinx.serialization.json.JsonArray |
| 7 | +import kotlinx.serialization.json.JsonElement |
| 8 | +import kotlinx.serialization.json.JsonNull |
| 9 | +import kotlinx.serialization.json.JsonObject |
| 10 | +import kotlinx.serialization.json.JsonPrimitive |
| 11 | +import org.jetbrains.exposed.v1.core.Column |
| 12 | +import org.jetbrains.exposed.v1.core.CustomEnumerationColumnType |
| 13 | +import org.jetbrains.exposed.v1.core.IColumnType |
| 14 | +import org.jetbrains.exposed.v1.core.StringColumnType |
| 15 | +import org.jetbrains.exposed.v1.core.UUIDColumnType |
| 16 | +import org.jetbrains.exposed.v1.datetime.KotlinInstantColumnType |
| 17 | +import org.jetbrains.exposed.v1.datetime.KotlinLocalDateColumnType |
| 18 | +import org.jetbrains.exposed.v1.datetime.KotlinLocalTimeColumnType |
| 19 | +import org.jetbrains.exposed.v1.datetime.KotlinOffsetDateTimeColumnType |
| 20 | +import java.time.Instant |
| 21 | +import java.time.LocalDateTime |
| 22 | +import java.time.OffsetDateTime |
| 23 | +import java.time.ZoneId |
| 24 | +import java.time.ZoneOffset |
| 25 | +import kotlin.time.ExperimentalTime |
| 26 | +import kotlin.time.toKotlinInstant |
| 27 | + |
| 28 | + |
| 29 | +public fun <T : Any> parseColumnNullable(data: JsonObject, column: Column<T?>): T? { |
| 30 | + val data = data[column.name] ?: throw NoSuchElementException("Column ${column.name} not found in JSON") |
| 31 | + val result = parse(data, column.columnType) |
| 32 | + return result |
| 33 | +} |
| 34 | + |
| 35 | +public fun <T : Any> parseColumn(data: JsonObject, column: Column<T>): T { |
| 36 | + val data = data[column.name] ?: throw NoSuchElementException("Column ${column.name} not found in JSON") |
| 37 | + val result = parse(data, column.columnType) |
| 38 | + return result ?: error( |
| 39 | + "Column '${column.name}' is required but was null or invalid" |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +private fun <T : Any> parse(value: JsonElement, type: IColumnType<T>): T? = when (value) { |
| 44 | + JsonNull -> null |
| 45 | + is JsonArray, is JsonObject -> error("Unsupported field value '$value' for column type: ${type::class.simpleName}") |
| 46 | + is JsonPrimitive -> when { |
| 47 | + value.isString -> parseString(value.content, type) |
| 48 | + else -> error("Unsupported field value '$value' for column type: ${type::class.simpleName}") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +private fun <T : Any> parseString(value: String, type: IColumnType<T>): T = when (type) { |
| 54 | + is StringColumnType -> type.valueFromDB(value) |
| 55 | + is UUIDColumnType -> type.valueFromDB(value) |
| 56 | + is CustomEnumerationColumnType<*> -> type.valueFromDB(value) |
| 57 | + is DomainColumnType<T, *> -> { |
| 58 | + @Suppress("UNCHECKED_CAST") |
| 59 | + val type = type as DomainColumnType<T, Any> |
| 60 | + val inner = parseString(value, type.originType) |
| 61 | + type.builder(inner) |
| 62 | + } |
| 63 | + |
| 64 | + is KotlinOffsetDateTimeColumnType -> { |
| 65 | + @Suppress("UNCHECKED_CAST") |
| 66 | + OffsetDateTime.parse(value, pgenTimestampTzFormatter) as T |
| 67 | + } |
| 68 | + |
| 69 | + is KotlinInstantColumnType -> { |
| 70 | + @Suppress("UNCHECKED_CAST") |
| 71 | + @OptIn(ExperimentalTime::class) |
| 72 | + LocalDateTime.parse(value, pgenTimestampFormatter) |
| 73 | + .atZone(ZoneId.systemDefault()) |
| 74 | + .toInstant() |
| 75 | + .toKotlinInstant() as T |
| 76 | + } |
| 77 | + |
| 78 | + is KotlinLocalDateColumnType -> { |
| 79 | + @Suppress("UNCHECKED_CAST") |
| 80 | + LocalDate.parse(value) as T |
| 81 | + } |
| 82 | + |
| 83 | + is KotlinLocalTimeColumnType -> { |
| 84 | + @Suppress("UNCHECKED_CAST") |
| 85 | + LocalTime.parse(value) as T |
| 86 | + } |
| 87 | + |
| 88 | + else -> error("Unsupported column type: ${type::class.simpleName}") |
| 89 | +} |
0 commit comments