Skip to content

Commit

Permalink
Merge pull request orchestr7#70 from akuleshov7/bugfix/empty-toml
Browse files Browse the repository at this point in the history
Bug: processing of empty toml strings and files
  • Loading branch information
orchestr7 authored Aug 30, 2021
2 parents cdaf63c + 3185c9b commit 6a583cd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public class TomlDecoder(
is TomlFile -> {
checkMissingRequiredField(rootNode.children, descriptor)
val firstFileChild = rootNode.getFirstChild() ?: throw InternalDecodingException(
"Missing child nodes (tales, key-values) for TomlFile." +
"Missing child nodes (tables, key-values) for TomlFile." +
" Empty toml was provided to the input?"
)
TomlDecoder(firstFileChild, config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ public inline class TomlParser(private val ktomlConf: KtomlConf) {
}

private fun MutableList<String>.trimEmptyLines(): MutableList<String> {
if (this.isEmpty()) {
return this
}
// removing all empty lines at the end, to cover empty tables properly
while (this.last().isEmptyLine()) {
this.removeLast()
if (this.isEmpty()) {
return this
}
}
return this
}
Expand Down
29 changes: 27 additions & 2 deletions ktoml-core/src/commonTest/kotlin/decoder/NullableTablesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package decoder

import com.akuleshov7.ktoml.KtomlConf
import com.akuleshov7.ktoml.Toml
import com.akuleshov7.ktoml.exceptions.InternalDecodingException
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull

@Serializable
Expand All @@ -20,10 +22,13 @@ data class Config2(val key: Key? = null)
@Serializable
data class Config3(val key: Key = Key(0L))

@Serializable
data class Config(val key: Key? = Key(0L))

@Serializable
data class Config4(val key: Key)

class RegressionScenariosTest {
class NullableTablesTest {
@Test
fun nullableKey() {
val mapper = Toml(
Expand Down Expand Up @@ -62,7 +67,7 @@ class RegressionScenariosTest {
assertNotNull(toml3)
assertEquals(1L, toml3.key.value)

val toml4 = mapper.decodeFromString<Config3>(
val toml4 = mapper.decodeFromString<Config4>(
"""
[key]
value = 1
Expand All @@ -73,3 +78,23 @@ class RegressionScenariosTest {
assertEquals(1L, toml4.key.value)
}
}

class EmptyTomlTest {
@Test
fun emptyToml() {
assertFailsWith<InternalDecodingException> {
Toml().decodeFromString<Config>(
"""
""".trimIndent()
)
}

assertFailsWith<InternalDecodingException> {
Toml().decodeFromString(
"".trimIndent()
)
}
}
}

0 comments on commit 6a583cd

Please sign in to comment.