fetch serialName with kxs, minor formatting/refactoring

This commit is contained in:
Adam 2021-12-03 22:18:17 +01:00
parent 42b60ac0c3
commit 29d190b8f4
2 changed files with 15 additions and 12 deletions

View file

@ -21,7 +21,8 @@ import org.http4k.format.ConfigurableKotlinxSerialization
import org.http4k.format.asConfigurable import org.http4k.format.asConfigurable
import org.http4k.format.withStandardMappings import org.http4k.format.withStandardMappings
object KSX : ConfigurableKotlinxSerialization({
object KXS : ConfigurableKotlinxSerialization({
ignoreUnknownKeys = true ignoreUnknownKeys = true
prettyPrint = true prettyPrint = true
prettyPrintIndent = " " prettyPrintIndent = " "
@ -37,7 +38,6 @@ object JsonSerdes : Serde<FactorioEvent<FactorioObjectData>> {
override fun deserializer() = Deserializer<FactorioEvent<FactorioObjectData>> { _, bytes -> override fun deserializer() = Deserializer<FactorioEvent<FactorioObjectData>> { _, bytes ->
jsonMapper.decodeFromString(bytes.decodeToString()) jsonMapper.decodeFromString(bytes.decodeToString())
} }
} }
fun main() { fun main() {
@ -53,7 +53,7 @@ fun main() {
.mapValues { readOnlyKey, value -> .mapValues { readOnlyKey, value ->
println("Mapping $readOnlyKey:$value") println("Mapping $readOnlyKey:$value")
jsonMapper.decodeFromString<FactorioEvent<FactorioObjectData>>(value) jsonMapper.decodeFromString<FactorioEvent<FactorioObjectData>>(value)
// KSX.asA<FactorioEvent<FactorioObjectData>>(value) // KXS.asA<FactorioEvent<FactorioObjectData>>(value)
} }
.to( .to(
{ key, value, recordContext -> value.data.objectName }, { key, value, recordContext -> value.data.objectName },
@ -63,7 +63,6 @@ fun main() {
val topology = builder.build() val topology = builder.build()
val properties = appProps.kafkaConfig val properties = appProps.kafkaConfig
val streams = KafkaStreams(topology, properties) val streams = KafkaStreams(topology, properties)
streams.setUncaughtExceptionHandler(StreamsExceptionHandler()) streams.setUncaughtExceptionHandler(StreamsExceptionHandler())

View file

@ -1,11 +1,9 @@
package dev.adamko.kafkatorio.events.schema package dev.adamko.kafkatorio.events.schema
import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.starProjectedType
import kotlin.reflect.full.findAnnotations
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.JsonClassDiscriminator import kotlinx.serialization.json.JsonClassDiscriminator
@ -34,14 +32,20 @@ const val FactorioObjectDataDiscriminatorKey: String = "object_name"
@Serializable @Serializable
@JsonClassDiscriminator(FactorioObjectDataDiscriminatorKey) @JsonClassDiscriminator(FactorioObjectDataDiscriminatorKey)
sealed class FactorioObjectData private constructor() { sealed class FactorioObjectData {
// workaround - https://github.com/Kotlin/kotlinx.serialization/issues/1664 // workaround - https://github.com/Kotlin/kotlinx.serialization/issues/1664
val objectName : String by lazy { val objectName: String by lazy {
requireNotNull(this::class.findAnnotation<SerialName>()?.value) { kotlinx.serialization.serializer(this::class.starProjectedType).descriptor.serialName
"Couldn't find @SerialName for ${this::class}!"
}
} }
// alternative: find the @SerialName annotation
// Again it must be delegated so there's no backing field and kxs ignores it
// val objectNameByAnnotation: String by lazy {
// requireNotNull(this::class.findAnnotation<SerialName>()?.value) {
// "Couldn't find @SerialName for ${this::class}!"
// }
// }
} }
@Serializable @Serializable