diff --git a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioLuaObjects.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioLuaObjects.kt index dcdce76..41d5441 100644 --- a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioLuaObjects.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioLuaObjects.kt @@ -12,7 +12,6 @@ import kotlinx.serialization.json.jsonPrimitive @Serializable(with = FactorioObjectDataSerializer::class) sealed class FactorioObjectData { - /** @see FactorioObjectData.discriminatorKey */ @EncodeDefault abstract val objectName: ObjectName diff --git a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioPrototypes.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioPrototypes.kt index 82316f3..1cc4dd0 100644 --- a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioPrototypes.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioPrototypes.kt @@ -1,31 +1,50 @@ package dev.adamko.kafkatorio.events.schema -import kotlinx.serialization.SerialName +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.EncodeDefault import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient -import kotlinx.serialization.json.JsonClassDiscriminator +import kotlinx.serialization.json.JsonContentPolymorphicSerializer +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive -@Serializable -class FactorioPrototypes( - val prototypes: List, -) -@Serializable -@JsonClassDiscriminator(FactorioPrototype.discriminatorKey) +@Serializable(with = FactorioPrototypeSerializer::class) sealed class FactorioPrototype { - abstract val objectName: PrototypeObjectName + @EncodeDefault + abstract val objectName: ObjectName - enum class PrototypeObjectName { + enum class ObjectName { LuaTilePrototype } - - companion object { - const val discriminatorKey: String = "objectName" - } } +object FactorioPrototypeSerializer : JsonContentPolymorphicSerializer( + FactorioPrototype::class +) { + private val key = FactorioPrototype::objectName.name + + override fun selectDeserializer(element: JsonElement): DeserializationStrategy { + + val type = element + .jsonObject[key] + ?.jsonPrimitive + ?.contentOrNull + ?.let { json -> + FactorioPrototype.ObjectName.values().firstOrNull { it.name == json } + } + + return when (type) { + FactorioPrototype.ObjectName.LuaTilePrototype -> FactorioMapTilePrototype.serializer() + null -> + throw Exception("Unknown FactorioPrototype $key: '$type' ") + } + } +} + + @Serializable -@SerialName("LuaTilePrototype") data class FactorioMapTilePrototype( val name: String, val layer: UInt, @@ -36,6 +55,6 @@ data class FactorioMapTilePrototype( /** Can the tile be mined for resources? */ val canBeMined: Boolean, ) : FactorioPrototype() { - @Transient - override val objectName: PrototypeObjectName = PrototypeObjectName.LuaTilePrototype + @EncodeDefault + override val objectName: ObjectName = ObjectName.LuaTilePrototype }