pre-emptively fixing kxs generics on factorioPrototypes.kt
This commit is contained in:
parent
7486961fc2
commit
524aa218fc
2 changed files with 37 additions and 19 deletions
|
|
@ -12,7 +12,6 @@ import kotlinx.serialization.json.jsonPrimitive
|
||||||
@Serializable(with = FactorioObjectDataSerializer::class)
|
@Serializable(with = FactorioObjectDataSerializer::class)
|
||||||
sealed class FactorioObjectData {
|
sealed class FactorioObjectData {
|
||||||
|
|
||||||
/** @see FactorioObjectData.discriminatorKey */
|
|
||||||
@EncodeDefault
|
@EncodeDefault
|
||||||
abstract val objectName: ObjectName
|
abstract val objectName: ObjectName
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,50 @@
|
||||||
package dev.adamko.kafkatorio.events.schema
|
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.Serializable
|
||||||
import kotlinx.serialization.Transient
|
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
|
||||||
import kotlinx.serialization.json.JsonClassDiscriminator
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
import kotlinx.serialization.json.contentOrNull
|
||||||
|
import kotlinx.serialization.json.jsonObject
|
||||||
|
import kotlinx.serialization.json.jsonPrimitive
|
||||||
|
|
||||||
@Serializable
|
|
||||||
class FactorioPrototypes<T : FactorioPrototype>(
|
|
||||||
val prototypes: List<T>,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable(with = FactorioPrototypeSerializer::class)
|
||||||
@JsonClassDiscriminator(FactorioPrototype.discriminatorKey)
|
|
||||||
sealed class FactorioPrototype {
|
sealed class FactorioPrototype {
|
||||||
abstract val objectName: PrototypeObjectName
|
@EncodeDefault
|
||||||
|
abstract val objectName: ObjectName
|
||||||
|
|
||||||
enum class PrototypeObjectName {
|
enum class ObjectName {
|
||||||
LuaTilePrototype
|
LuaTilePrototype
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val discriminatorKey: String = "objectName"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object FactorioPrototypeSerializer : JsonContentPolymorphicSerializer<FactorioPrototype>(
|
||||||
|
FactorioPrototype::class
|
||||||
|
) {
|
||||||
|
private val key = FactorioPrototype::objectName.name
|
||||||
|
|
||||||
|
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out FactorioPrototype> {
|
||||||
|
|
||||||
|
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
|
@Serializable
|
||||||
@SerialName("LuaTilePrototype")
|
|
||||||
data class FactorioMapTilePrototype(
|
data class FactorioMapTilePrototype(
|
||||||
val name: String,
|
val name: String,
|
||||||
val layer: UInt,
|
val layer: UInt,
|
||||||
|
|
@ -36,6 +55,6 @@ data class FactorioMapTilePrototype(
|
||||||
/** Can the tile be mined for resources? */
|
/** Can the tile be mined for resources? */
|
||||||
val canBeMined: Boolean,
|
val canBeMined: Boolean,
|
||||||
) : FactorioPrototype() {
|
) : FactorioPrototype() {
|
||||||
@Transient
|
@EncodeDefault
|
||||||
override val objectName: PrototypeObjectName = PrototypeObjectName.LuaTilePrototype
|
override val objectName: ObjectName = ObjectName.LuaTilePrototype
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue