improve event-schema polymorphism with enums

This commit is contained in:
Adam 2022-01-08 23:09:27 +01:00
parent 3875b9c309
commit 51e0aff823
18 changed files with 120 additions and 67 deletions

View file

@ -1,6 +1,7 @@
package dev.adamko.kafkatorio.events.schema
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
@ -9,7 +10,10 @@ data class FactorioConfigurationUpdate(
val factorioData: FactorioGameDataUpdate,
val allMods: List<FactorioModInfo>,
val prototypes: List<FactorioPrototype>,
) : KafkatorioPacket
) : KafkatorioPacket() {
@Transient
override val packetType = PacketType.CONFIG
}
@Serializable
data class FactorioGameDataUpdate(

View file

@ -2,15 +2,18 @@ package dev.adamko.kafkatorio.events.schema
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
data class FactorioEvent<out T : FactorioObjectData>(
override val modVersion: String,
/** the initial Factorio event (`defines.events`) trigger */
val eventType: String,
/** game time */
val tick: UInt,
val data: T,
) : KafkatorioPacket
) : KafkatorioPacket() {
@Transient
override val packetType: PacketType = PacketType.EVENT
}

View file

@ -9,7 +9,8 @@ import kotlinx.serialization.json.JsonClassDiscriminator
@JsonClassDiscriminator(FactorioObjectData.discriminatorKey)
sealed class FactorioObjectData {
abstract val objectName: String
/** @see FactorioObjectData.discriminatorKey */
abstract val objectName: ObjectName
// workaround - https://github.com/Kotlin/kotlinx.serialization/issues/1664
// val objectName: String by lazy {
@ -28,12 +29,22 @@ sealed class FactorioObjectData {
// }
// }
enum class ObjectName {
LuaPlayer,
LuaEntity,
LuaSurface,
FactorioMapChunk,
LuaTile,
ConsoleChatMessage,
}
companion object {
/** The [JsonClassDiscriminator] for [FactorioEvent] */
const val discriminatorKey: String = "objectName"
}
}
@Serializable
@SerialName("LuaPlayer")
data class PlayerData(
@ -47,7 +58,7 @@ data class PlayerData(
val lastOnline: UInt,
) : FactorioObjectData() {
@Transient
override val objectName: String = "LuaPlayer"
override val objectName = ObjectName.LuaPlayer
}
@Serializable
@ -64,7 +75,7 @@ data class EntityData(
val playerIndex: UInt? = null,
) : FactorioObjectData() {
@Transient
override val objectName: String = "LuaEntity"
override val objectName = ObjectName.LuaEntity
}
@Serializable
@ -75,16 +86,17 @@ data class SurfaceData(
val name: String,
) : FactorioObjectData() {
@Transient
override val objectName: String = "LuaSurface"
override val objectName = ObjectName.LuaSurface
}
/** Quasi-object. This isn't a Factorio Lua type, but a helpful collection of [FactorioMapTile]s */
@Serializable
@SerialName("FactorioMapChunk")
data class FactorioMapChunk(
val tiles: List<FactorioMapTile>,
) : FactorioObjectData() {
@Transient
override val objectName: String = "FactorioMapChunk"
override val objectName = ObjectName.FactorioMapChunk
}
@Serializable
@ -95,7 +107,7 @@ data class FactorioMapTile(
val surfaceIndex: Int,
) : FactorioObjectData() {
@Transient
override val objectName: String = "LuaTile"
override val objectName = ObjectName.LuaTile
}
@Serializable
@ -104,5 +116,5 @@ data class ConsoleChatMessage(
val content: String,
) : FactorioObjectData() {
@Transient
override val objectName: String = "ConsoleChatMessage"
override val objectName = ObjectName.ConsoleChatMessage
}

View file

@ -7,14 +7,17 @@ import kotlinx.serialization.json.JsonClassDiscriminator
@Serializable
class FactorioPrototypes<T : FactorioPrototype>(
override val modVersion: String,
val prototypes: List<T>,
) : KafkatorioPacket
)
@Serializable
@JsonClassDiscriminator(FactorioPrototype.discriminatorKey)
sealed class FactorioPrototype {
abstract val objectName: String
abstract val objectName: PrototypeObjectName
enum class PrototypeObjectName {
LuaTilePrototype
}
companion object {
const val discriminatorKey: String = "objectName"
@ -34,5 +37,5 @@ data class FactorioMapTilePrototype(
val canBeMined: Boolean,
) : FactorioPrototype() {
@Transient
override val objectName: String = "LuaTilePrototype"
override val objectName: PrototypeObjectName = PrototypeObjectName.LuaTilePrototype
}

View file

@ -1,19 +1,22 @@
package dev.adamko.kafkatorio.events.schema;
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
//@Serializable
sealed interface KafkatorioPacket {
@Serializable
@JsonClassDiscriminator("packetType")
sealed class KafkatorioPacket {
/** Schema versioning */
val modVersion: String
abstract val modVersion: String
abstract val packetType: PacketType
enum class PacketType {
EVENT, CONFIG,
}
}
//enum class PacketDataType {
// EVENT, PROTOTYPE, CONFIG_DATA,
// ;
//}
//@Serializable
//@JsonClassDiscriminator(KafkatorioPacketData.discriminatorKey)
//sealed class KafkatorioPacketData(

View file

@ -48,7 +48,7 @@ fun main(args: Array<String>) {
.forEach { (defFile, def) ->
val outFile = outPath.resolve(defFile).toFile()
outFile.printWriter().use {
it.print(header)
it.println(header)
it.print(def)
}
println("Generated file: $outFile")
@ -65,14 +65,16 @@ private fun splitDefinitions(definitions: String): Map<String, String> {
.groupBy { def ->
when {
"extends FactorioObjectData" in def ||
"interface FactorioObjectData" in def -> "object-data.d.ts"
"interface FactorioObjectData" in def ||
"type ObjectName" in def -> "object-data.d.ts"
"extends FactorioPrototype" in def ||
"interface FactorioPrototype" in def -> "prototype.d.ts"
"interface FactorioPrototype" in def ||
"type PrototypeObjectName" in def -> "prototype.d.ts"
"extends FactorioConfigurationUpdate" in def ||
"interface FactorioConfigurationUpdate" in def ||
"FactorioGameDataUpdate" in def ||
"FactorioModInfo" in def -> "config-update.d.ts"
else -> "schema.d.ts"
"FactorioModInfo" in def -> "config-update.d.ts"
else -> "schema.d.ts"
}
}
.mapValues { (_, def) ->
@ -86,7 +88,7 @@ private fun splitDefinitions(definitions: String): Map<String, String> {
val header = """
|// Generated by TypeScriptGenerator - do not edit this file manually
|
""".trimMargin() + "\n"
""".trimMargin()
//object PropertyNameTransformer : ClassTransformer {
// override fun transformPropertyName(