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

Handle trailing comma in array #169

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ internal fun String.trimCurlyBraces(): String = trimSymbols(this, "{", "}")
*/
internal fun String.trimBrackets(): String = trimSymbols(this, "[", "]")

/**
* If this string ends with comma(,) - will return the string with trailing comma removed.
* Otherwise, returns this string.
*
* @return string with the result
*/
internal fun String.removeTrailingComma(): String = this.removeSuffix(",")

/**
* If this string starts and end with a pair brackets([[]]) - will return the string with brackets removed
* Otherwise, returns this string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.akuleshov7.ktoml.TomlConfig
import com.akuleshov7.ktoml.TomlInputConfig
import com.akuleshov7.ktoml.TomlOutputConfig
import com.akuleshov7.ktoml.exceptions.ParseException
import com.akuleshov7.ktoml.parsers.removeTrailingComma
import com.akuleshov7.ktoml.parsers.trimBrackets
import com.akuleshov7.ktoml.tree.nodes.parseValue
import com.akuleshov7.ktoml.writers.TomlEmitter
Expand Down Expand Up @@ -137,8 +138,9 @@ public class TomlArray internal constructor(
*/
@Suppress("NESTED_BLOCK", "TOO_LONG_FUNCTION")
private fun String.parseArray(): MutableList<String> {
val trimmed = trimBrackets().removeTrailingComma()
// covering cases when the array is intentionally blank: myArray = []. It should be empty and not contain null
if (this.trimBrackets().isBlank()) {
if (trimmed.isBlank()) {
return mutableListOf()
}

Expand All @@ -148,7 +150,6 @@ public class TomlArray internal constructor(
var bufferBetweenCommas = StringBuilder()
val result: MutableList<String> = mutableListOf()

val trimmed = trimBrackets()
for (i in trimmed.indices) {
when (val current = trimmed[i]) {
'[' -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import kotlin.test.assertTrue
@Serializable
data class SimpleArray(val a: List<Long>)

@Serializable
data class SimpleArrayWithNullableValues(val a: List<Long?>)

@Serializable
data class SimpleStringArray(val a: List<String>)

Expand Down Expand Up @@ -335,6 +338,21 @@ class SimpleArrayDecoderTest {
assertEquals(SimpleArray(listOf(1, 2, 3)), Toml.decodeFromString(test))
}

@Test
fun testArrayWithTrailingComma() {
var test = "a = [1, 2, 3,]"
assertEquals(SimpleArrayWithNullableValues(listOf(1, 2, 3)), Toml.decodeFromString(test))

test = """
a = [
1,
2,
3,
]
""".trimIndent()
assertEquals(SimpleArrayWithNullableValues(listOf(1, 2, 3)), Toml.decodeFromString(test))
}

@Test
fun testSimpleArrayDecoderInNestedTable() {
var test = """
Expand Down