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

View file

@ -1,11 +1,9 @@
package dev.adamko.kafkatorio.events.schema
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.findAnnotations
import kotlin.reflect.full.starProjectedType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.JsonClassDiscriminator
@ -34,14 +32,20 @@ const val FactorioObjectDataDiscriminatorKey: String = "object_name"
@Serializable
@JsonClassDiscriminator(FactorioObjectDataDiscriminatorKey)
sealed class FactorioObjectData private constructor() {
sealed class FactorioObjectData {
// workaround - https://github.com/Kotlin/kotlinx.serialization/issues/1664
val objectName : String by lazy {
requireNotNull(this::class.findAnnotation<SerialName>()?.value) {
"Couldn't find @SerialName for ${this::class}!"
}
val objectName: String by lazy {
kotlinx.serialization.serializer(this::class.starProjectedType).descriptor.serialName
}
// 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