Skip to content

Commit

Permalink
chore: Bump geary with changes to PolymorphicListAsMapSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Apr 10, 2024
1 parent 4ad211e commit a520827
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.mineinabyss.geary.serialization.dsl.serializableComponents
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PolymorphicSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
Expand All @@ -17,21 +16,23 @@ import kotlinx.serialization.descriptors.buildSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable(with=EventComponent.Serializer::class)
@Serializable(with = EventComponent.Serializer::class)
class EventComponent(val type: ComponentId, val data: GearyComponent?) {
class Serializer: KSerializer<EventComponent> {
class Serializer : KSerializer<EventComponent> {
@OptIn(InternalSerializationApi::class)
override val descriptor: SerialDescriptor =
buildSerialDescriptor("geary:event", SerialKind.CONTEXTUAL)

private val polymorphicListAsMapSerializer = PolymorphicListAsMapSerializer.ofComponents()

override fun deserialize(decoder: Decoder): EventComponent {
val composite = runCatching { (decoder as YamlInput).beginStructure(String.serializer().descriptor) }.getOrNull()
val composite =
runCatching { (decoder as YamlInput).beginStructure(String.serializer().descriptor) }.getOrNull()
if (composite != null) {
val type = composite.decodeStringElement(String.serializer().descriptor, 0)
composite.endStructure(String.serializer().descriptor)
val namespaces = polymorphicListAsMapSerializer.getNamespaces(decoder.serializersModule)
val namespaces = polymorphicListAsMapSerializer.getParentConfig(decoder.serializersModule)?.namespaces
?: emptyList()
val typeComponentId = componentId(serializableComponents.serializers.getClassFor(type, namespaces))
return EventComponent(typeComponentId, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class Skill(
"run" -> run = compositeDecoder.decodeMapValue(Skills.serializer())
"onFail" -> onFail = compositeDecoder.decodeMapValue(Skills.serializer())
else -> execute += compositeDecoder.decodeMapValue(
polymorphic.findSerializerFor(module, polymorphic.getNamespaces(module), key)
polymorphic.findSerializerFor(
serializersModule = module,
namespaces = polymorphic.getParentConfig(module)?.namespaces ?: emptyList(),
key = key
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Variables(

override fun deserialize(decoder: Decoder): Variables {
val module = decoder.serializersModule
val namespaces = polymorphic.getNamespaces(module)
val namespaces = polymorphic.getParentConfig(module)?.namespaces ?: emptyList()
val inputs = mutableMapOf<String, Input<*>>()
var using: SetTarget? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mineinabyss.geary.papermc.config

import com.mineinabyss.geary.annotations.optin.UnsafeAccessors
import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.TestEngineModule
Expand All @@ -11,14 +10,17 @@ import com.mineinabyss.geary.papermc.bridge.config.Skills
import com.mineinabyss.geary.papermc.bridge.config.inputs.Input
import com.mineinabyss.geary.papermc.bridge.config.inputs.Variables
import com.mineinabyss.geary.papermc.bridge.events.EventHelpers
import com.mineinabyss.geary.prefabs.PrefabLoader
import com.mineinabyss.geary.prefabs.Prefabs
import com.mineinabyss.geary.prefabs.configuration.components.ChildrenOnPrefab
import com.mineinabyss.geary.serialization.dsl.serializableComponents
import com.mineinabyss.geary.serialization.dsl.serialization
import com.mineinabyss.geary.serialization.formats.Format
import com.mineinabyss.geary.serialization.formats.YamlFormat
import com.mineinabyss.geary.serialization.serializers.GearyEntitySerializer
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer.Companion.provideConfig
import com.mineinabyss.geary.serialization.serializers.SerializableGearyEntity
import com.mineinabyss.geary.systems.GearyListener
import com.mineinabyss.geary.systems.builders.listener
import com.mineinabyss.geary.systems.query.ListenerQuery
import com.mineinabyss.idofront.di.DI
Expand All @@ -27,6 +29,7 @@ import io.kotest.matchers.shouldBe
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.modules.SerializersModule
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -72,12 +75,18 @@ class VariablesConfigTest {
@SerialName("geary:generate_int")
class GenerateInt(val generate: Int)

@OptIn(UnsafeAccessors::class)
fun testReadCorrectly(
@Language("yaml") config: String
) {
// arrange
val prefab = yamlFormat.decodeFromString(serializer, config)
val props = yamlFormat.decodeFromString(
PrefabLoader.PrefabFileProperties.serializer(),
config,
configType = Format.ConfigType.NON_STRICT
)
val prefab = yamlFormat.decodeFromString(serializer, config, overrideSerializersModule = SerializersModule {
provideConfig(PolymorphicListAsMapSerializer.Config(namespaces = props.namespaces))
})
var result: Int? = null

geary.listener(object : ListenerQuery() {
Expand Down Expand Up @@ -194,7 +203,6 @@ class VariablesConfigTest {
@Test
fun `should read entity variable reference correctly`() {
val config = """
namespaces: [ geary ]
skills:
- event: on.test
vars:
Expand All @@ -203,7 +211,9 @@ class VariablesConfigTest {
entity: ${'$'}test
""".trimIndent()

val prefab = yamlFormat.decodeFromString(serializer, config)
val prefab = yamlFormat.decodeFromString(serializer, config, overrideSerializersModule = SerializersModule {
provideConfig(PolymorphicListAsMapSerializer.Config(namespaces = listOf("geary")))
})

var result: GearyEntity? = null

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ kotlin.code.style=official
group=com.mineinabyss
version=0.30
idofrontVersion=0.23.0
gearyVersion=0.26.0-dev.15
gearyVersion=0.26.0-dev.18

0 comments on commit a520827

Please sign in to comment.