Skip to content

Commit

Permalink
Move extensionDefinitionPrefix to YamlConfiguration.
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Aug 14, 2019
1 parent 431a386 commit 795ada3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/main/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import org.snakeyaml.engine.v1.api.StreamDataWriter
import java.io.StringWriter

class Yaml(
val extensionDefinitionPrefix: String? = null,
override val context: SerialModule = EmptyModule,
val configuration: YamlConfiguration = YamlConfiguration()
) : AbstractSerialFormat(context), StringFormat {
override fun <T> parse(deserializer: DeserializationStrategy<T>, string: String): T {
val parser = YamlParser(string)
val reader = YamlNodeReader(parser, extensionDefinitionPrefix)
val reader = YamlNodeReader(parser, configuration.extensionDefinitionPrefix)
val rootNode = reader.read()
parser.ensureEndOfStreamReached()

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/charleskorn/kaml/YamlConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ package com.charleskorn.kaml
*
* [encodeDefaults]: set to false to not write default property values to YAML (defaults to true)
* [strictMode]: set to true to throw an exception when reading an object that has an unknown property, or false to ignore unknown properties (defaults to true)
* [extensionDefinitionPrefix]: prefix used on root-level keys (where document root is an object) to define extensions that can later be merged (defaults to null, which disables extensions altogether). See https://batect.charleskorn.com/config/Overview.html#anchors-aliases-extensions-and-merging for example.
*/
data class YamlConfiguration constructor(
internal val encodeDefaults: Boolean = true,
internal val strictMode: Boolean = true
internal val strictMode: Boolean = true,
internal val extensionDefinitionPrefix: String? = null
)
4 changes: 3 additions & 1 deletion src/test/kotlin/com/charleskorn/kaml/YamlReadingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,9 @@ object YamlReadingTest : Spek({
""".trimIndent()

context("parsing that input") {
val result = Yaml(extensionDefinitionPrefix = ".").parse(SimpleStructure.serializer(), input)
val configuration = YamlConfiguration(extensionDefinitionPrefix = ".")
val yaml = Yaml(configuration = configuration)
val result = yaml.parse(SimpleStructure.serializer(), input)

it("deserializes it to a Kotlin object, replacing the reference to the extension with the extension") {
assert(result).toBe(SimpleStructure("Jamie"))
Expand Down

0 comments on commit 795ada3

Please sign in to comment.