Skip to content

Commit

Permalink
fix: InnerSerializer not working for primitive descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Mar 27, 2024
1 parent 77a247a commit ffebadb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class PrefabLoader {
val decoded = runCatching {
val serializer = PolymorphicListAsMapSerializer.of(PolymorphicSerializer(GearyComponent::class))
val ext = path.name.substringAfterLast('.')
logger.d("Loading prefab at $path")
formats[ext]?.decodeFromFile(serializer, path)
?: throw IllegalArgumentException("Unknown file format $ext")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mineinabyss.geary.serialization.serializers

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
Expand All @@ -11,7 +13,10 @@ abstract class InnerSerializer<I, O>(
val transform: (I) -> O,
val inverseTransform: (O) -> I,
) : KSerializer<O> {
override val descriptor = SerialDescriptor(serialName, inner.descriptor)
override val descriptor =
if (inner.descriptor.kind is PrimitiveKind)
PrimitiveSerialDescriptor(serialName, inner.descriptor.kind as PrimitiveKind)
else SerialDescriptor(serialName, inner.descriptor)

override fun deserialize(decoder: Decoder): O {
return transform(inner.deserialize(decoder))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ open class PolymorphicListAsMapSerializer<T : Any>(
compositeDecoder.decodeMapValue(componentSerializer)
}.onSuccess { components += it }.onFailure {
if (skipMalformedComponents) {
geary.logger.w("Malformed component $key, ignoring")
it.stackTraceToString()
.lineSequence()
.joinToString("\n", limit = 10, truncated = "...")
.let(geary.logger::w)
geary.logger.w(
"Malformed component $key, ignoring:\n" +
it.stackTraceToString()
.lineSequence()
.joinToString("\n", limit = 10, truncated = "...")
)
} else throw it
}
}.onFailure {
Expand Down

0 comments on commit ffebadb

Please sign in to comment.