Skip to content

Commit

Permalink
Adding the support for empty TOML
Browse files Browse the repository at this point in the history
  • Loading branch information
orchestr7 committed Jan 27, 2022
1 parent cc6e05a commit 2295a74
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ In case you don't have much time for this - at least spend 5 seconds to give us
**Thanks!** :pray:

### Acknowledgement
Special thanks to those devoted who help us maintain and improve the project: @NightEule5, @Peanuuutz, @petertrr, @Olivki and @edrd-f.
Special thanks to those awesome developers who give us great suggestions, help us to maintain and improve this project:
@NightEule5, @Peanuuutz, @petertrr, @Olivki and @edrd-f.

## Supported platforms
All the code is written in Kotlin **common** module. This means that it can be built for each and every Kotlin native platform.
Expand Down Expand Up @@ -192,7 +193,11 @@ Toml(
// allow/prohibit empty values like "a = # comment", default true
emptyValuesAllowed = true,
// allow/prohibit escaping of single quotes in literal strings, default true
escapedQuotesInLiteralStringsAllowed = true
escapedQuotesInLiteralStringsAllowed = true,
// allow/prohibit processing of empty toml, if false - throws an InternalDecodingException exception, default is true
allowEmptyToml = true,
// indentation symbols for serialization, default 4 spaces
indentation = Indentation.FOUR_SPACES,
)
).decodeFromString<MyClass>(
tomlString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ public class KtomlConf(
* @property ignoreUnknownNames - a control to allow/prohibit unknown names during the deserialization
* @property allowEmptyValues - a control to allow/prohibit empty values: a = # comment
* @property allowEscapedQuotesInLiteralStrings - a control to allow/prohibit escaping of single quotes in literal strings
* @property indentation
* @property indentation - the number of spaces in the indents for the serialization
* @property allowEmptyToml - controls if empty toml can be processed, if false - will throw an exception
*/
public open class TomlConfig(
public val ignoreUnknownNames: Boolean = false,
public val allowEmptyValues: Boolean = true,
public val allowEscapedQuotesInLiteralStrings: Boolean = true,
public val indentation: Indentation = Indentation.FOUR_SPACES,
public val allowEmptyToml: Boolean = true,
) {
/**
* @property value
* @property value - string with indents, used for the formatting of serialization
*/
public enum class Indentation(public val value: String) {
FOUR_SPACES(" "),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class TomlMainDecoder(
}

// the iteration will go through all elements that will be found in the input
private fun isDecodingDone() = elementIndex == rootNode.getNeighbourNodes().size
private fun isDecodingDone() =
if (rootNode is TomlFile) true else elementIndex == rootNode.getNeighbourNodes().size

/**
* Getting the node with the value
Expand Down Expand Up @@ -177,10 +178,15 @@ public class TomlMainDecoder(
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder = when (rootNode) {
is TomlFile -> {
checkMissingRequiredProperties(rootNode.children, descriptor)
val firstFileChild = rootNode.getFirstChild() ?: throw InternalDecodingException(
"Missing child nodes (tables, key-values) for TomlFile." +
" Was empty toml provided to the input?"
)
val firstFileChild = rootNode.getFirstChild() ?: if (!config.allowEmptyToml) {
throw InternalDecodingException(
"Missing child nodes (tables, key-values) for TomlFile." +
" Was empty toml provided to the input?"
)
} else {
rootNode
}

TomlMainDecoder(firstFileChild, config)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import com.akuleshov7.ktoml.parsers.trimQuotes
public sealed class TomlNode(
public open val content: String,
public open val lineNo: Int,
public open val config: TomlConfig = TomlConfig()) {
public open val config: TomlConfig = TomlConfig()
) {
public open val children: MutableSet<TomlNode> = mutableSetOf()
public open var parent: TomlNode? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ class NullableTablesTest {
class EmptyTomlTest {
@Test
fun emptyToml() {
assertFailsWith<InternalDecodingException> {
Toml().decodeFromString<Config>(
"""
var res = Toml().decodeFromString<Config>(
"""
""".trimIndent()
)
}
)

assertFailsWith<InternalDecodingException> {
Toml().decodeFromString(
"".trimIndent()
)
}
assertEquals(Config(), res)

res = Toml().decodeFromString<Config>(
"".trimIndent()
)

assertEquals(Config(), res)
}
}

0 comments on commit 2295a74

Please sign in to comment.