Skip to content

Commit

Permalink
Fix #3: nullable value parsing in block map
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed May 22, 2020
1 parent c377afd commit faaae8a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ internal class YamlDecoder(
override val kind: Kind
get() = Kind.BLOCK_CLASS

@Suppress("DuplicatedCode")
override fun decodeNotNullMark(): Boolean {
val indent = tokenStream.currentIndent
return when (val token = tokenStream.nextToken()) {
END_OF_FILE -> false
Token.STRING_NULL -> false
else -> {
tokenStream.reuseToken(token)
tokenStream.currentIndent > indent
}
}
}

override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
when (val token = tokenStream.nextToken()) {
null -> return READ_DONE
Expand Down Expand Up @@ -267,6 +280,19 @@ internal class YamlDecoder(
override val kind: Kind
get() = Kind.BLOCK_MAP

@Suppress("DuplicatedCode")
override fun decodeNotNullMark(): Boolean {
val indent = tokenStream.currentIndent
return when (val token = tokenStream.nextToken()) {
END_OF_FILE -> false
Token.STRING_NULL -> false
else -> {
tokenStream.reuseToken(token)
tokenStream.currentIndent > indent
}
}
}

override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
if (index.isOdd()) {
return index++
Expand Down
24 changes: 24 additions & 0 deletions yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/BlockMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import net.mamoe.yamlkt.Yaml
import net.mamoe.yamlkt.Yaml.Companion.default
import net.mamoe.yamlkt.toContentMap
import org.junit.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -159,4 +160,27 @@ t:
), map
)
}

// from https://github.com/Him188/yamlkt/issues/3
@Test
fun testNullValue() {
@Serializable
data class TestData(
val nullable: String?,
val nonnull: String,
val nullableMap: Map<String, String>?,
val nullableList: List<String>?,
)

assertEquals(
TestData(null, "value", null, null), default.parse(
TestData.serializer(), """
nullable:
nonnull: value
nullableMap:
nullableList:
""".trimIndent()
)
)
}
}

0 comments on commit faaae8a

Please sign in to comment.