From 87f82325007e86d1723077c2e8f2920aa7d170dd Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Sun, 11 Feb 2024 01:44:59 -0500 Subject: [PATCH] Add option to skip malformed components individually --- .../serializers/PolymorphicListAsMapSerializer.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt index 82fd8fb7..9445b660 100644 --- a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt +++ b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/serializers/PolymorphicListAsMapSerializer.kt @@ -22,6 +22,7 @@ open class PolymorphicListAsMapSerializer( // We need primary constructor to be a single serializer for generic serialization to work, use of() if manually creating private var prefix: String = "" private var onMissingSerializer: OnMissing = OnMissing.WARN + private var skipMalformedComponents: Boolean = true val polymorphicSerializer = serializer as? PolymorphicSerializer ?: error("Serializer is not polymorphic") @@ -53,7 +54,17 @@ open class PolymorphicListAsMapSerializer( key ) }.onSuccess { componentSerializer -> - components += compositeDecoder.decodeMapValue(componentSerializer) + runCatching { + 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) + } else throw it + } }.onFailure { when (onMissingSerializer) { OnMissing.ERROR -> throw it