Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Decoder does not represent the current node correctly #617

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlListInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.charleskorn.kaml

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.CompositeDecoder
Expand Down Expand Up @@ -65,6 +66,13 @@ internal class YamlListInput(val list: YamlList, yaml: Yaml, context: Serializer
override fun decodeChar(): Char = currentElementDecoder.decodeChar()
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = currentElementDecoder.decodeEnum(enumDescriptor)

override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
if (!haveStartedReadingElements) {
return super.decodeSerializableValue(deserializer)
}
return currentElementDecoder.decodeSerializableValue(deserializer)
}

private val haveStartedReadingElements: Boolean
get() = nextElementIndex > 0

Expand Down
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlMapLikeInputBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.charleskorn.kaml

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.modules.SerializersModule

Expand Down Expand Up @@ -45,6 +46,15 @@ internal sealed class YamlMapLikeInputBase(map: YamlMap, yaml: Yaml, context: Se
override fun decodeChar(): Char = fromCurrentValue { decodeChar() }
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = fromCurrentValue { decodeEnum(enumDescriptor) }

override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
if (!haveStartedReadingEntries) {
return super.decodeSerializableValue(deserializer)
}
return fromCurrentValue {
decodeSerializableValue(deserializer)
}
}

protected fun <T> fromCurrentValue(action: YamlInput.() -> T): T {
try {
return action(currentValueDecoder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2724,10 +2724,7 @@ private object DecodingFromYamlNodeSerializer : KSerializer<DatabaseListing> {
override fun deserialize(decoder: Decoder): DatabaseListing {
check(decoder is YamlInput)

val currentMap = decoder.node.yamlMap.get<YamlMap>("databaseListing")
checkNotNull(currentMap)

val list = currentMap.entries.map { (_, value) ->
val list = decoder.node.yamlMap.entries.map { (_, value) ->
decoder.yaml.decodeFromYamlNode(Database.serializer(), value)
}

Expand Down