Skip to content

Commit

Permalink
Merge pull request #40 from knightzmc/master
Browse files Browse the repository at this point in the history
Add support for different flow styles in Sequences
  • Loading branch information
charleskorn committed Sep 3, 2020
2 parents 6fbd643 + d2d6626 commit a4464df
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/main/kotlin/com/charleskorn/kaml/YamlConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.charleskorn.kaml

import org.snakeyaml.engine.v2.common.FlowStyle

/**
* Configuration options for parsing YAML to objects and serialising objects to YAML.
*
Expand All @@ -29,17 +31,39 @@ package com.charleskorn.kaml
* * [PolymorphismStyle.Property]: use a property (eg. `{ type: typeOfThing, property: value }`)
* * [encodingIndentationSize]: number of spaces to use as indentation when encoding objects as YAML
* * [breakScalarsAt]: maximum length of scalars when encoding objects as YAML (scalars exceeding this length will be split into multiple lines)
* * [sequenceStyle]: how sequences (aka lists and arrays) should be formatted. See [SequenceStyle] for an example of each
*/
public data class YamlConfiguration constructor(
internal val encodeDefaults: Boolean = true,
internal val strictMode: Boolean = true,
internal val extensionDefinitionPrefix: String? = null,
internal val polymorphismStyle: PolymorphismStyle = PolymorphismStyle.Tag,
internal val encodingIndentationSize: Int = 2,
internal val breakScalarsAt: Int = 80
internal val breakScalarsAt: Int = 80,
internal val sequenceStyle: SequenceStyle = SequenceStyle.Block
)

public enum class PolymorphismStyle {
Tag,
Property
}

public enum class SequenceStyle(internal val flowStyle: FlowStyle) {
/**
* The block form, eg
* ```yaml
* - 1
* - 2
* - 3
* ```
*/
Block(FlowStyle.BLOCK),

/**
* The flow form, eg
* ```yaml
* [1, 2, 3]
* ```
*/
Flow(FlowStyle.FLOW)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal class YamlOutput(
override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder {
when (descriptor.kind) {
is PolymorphicKind -> shouldReadTypeName = true
StructureKind.LIST -> emitter.emit(SequenceStartEvent(Optional.empty(), Optional.empty(), true, FlowStyle.BLOCK))
StructureKind.LIST -> emitter.emit(SequenceStartEvent(Optional.empty(), Optional.empty(), true, configuration.sequenceStyle.flowStyle))
StructureKind.MAP, StructureKind.CLASS, StructureKind.OBJECT -> {
val typeName = getAndClearTypeName()

Expand Down
77 changes: 77 additions & 0 deletions src/test/kotlin/com/charleskorn/kaml/YamlWritingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ object YamlWritingTest : Spek({
)
}
}
context("serializing a list of integers in flow form") {
val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow))
.encodeToString(ListSerializer(Int.serializer()), listOf(1, 2, 3))

it("returns the value serialized in the expected YAML form") {
expect(output).toBe(
"[1, 2, 3]"
)
}
}

context("serializing a list of nullable integers") {
val output = Yaml.default.encodeToString(ListSerializer(Int.serializer().nullable), listOf(1, null, 3))
Expand All @@ -241,6 +251,17 @@ object YamlWritingTest : Spek({
}
}

context("serializing a list of nullable integers in flow form") {
val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow))
.encodeToString(ListSerializer(Int.serializer().nullable), listOf(1, null, 3))

it("returns the value serialized in the expected YAML form") {
expect(output).toBe(
"[1, null, 3]"
)
}
}

context("serializing a list of strings") {
val output = Yaml.default.encodeToString(ListSerializer(String.serializer()), listOf("item1", "item2"))

Expand All @@ -254,6 +275,17 @@ object YamlWritingTest : Spek({
}
}

context("serializing a list of strings in flow form") {
val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow))
.encodeToString(ListSerializer(String.serializer()), listOf("item1", "item2"))

it("returns the value serialized in the expected YAML form") {
expect(output).toBe(
"""["item1", "item2"]"""
)
}
}

context("serializing a list of strings with a string longer than the maximum scalar width") {
val yamlWithScalarLimit = Yaml(configuration = YamlConfiguration(breakScalarsAt = 80))
val output = yamlWithScalarLimit.encodeToString(ListSerializer(String.serializer()), listOf("item1", "Hello world this is a string that is much, much, much (ok, not that much) longer than 80 characters"))
Expand All @@ -267,6 +299,19 @@ object YamlWritingTest : Spek({
}
}

context("serializing a list of strings with a string longer than the maximum scalar width in flow form") {
val yamlWithScalarLimit = Yaml(configuration = YamlConfiguration(breakScalarsAt = 80, sequenceStyle = SequenceStyle.Flow))
val output = yamlWithScalarLimit.encodeToString(ListSerializer(String.serializer()), listOf("item1", "Hello world this is a string that is much, much, much (ok, not that much) longer than 80 characters"))

it("returns the value serialized in the expected YAML form, broken onto a new line at the maximum scalar width") {
expect(output).toBe("""
["item1", "Hello world this is a string that is much, much, much (ok, not that much)\
\ longer than 80 characters"]
""".trimIndent()
)
}
}

context("serializing a list of a list of integers") {
val input = listOf(
listOf(1, 2, 3),
Expand All @@ -288,6 +333,22 @@ object YamlWritingTest : Spek({
}
}

context("serializing a list of a list of integers in flow form") {
val input = listOf(
listOf(1, 2, 3),
listOf(4, 5)
)

val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow))
.encodeToString(ListSerializer(ListSerializer(Int.serializer())), input)

it("returns the value serialized in the expected YAML form") {
expect(output).toBe(
"""[[1, 2, 3], [4, 5]]"""
)
}
}

context("serializing a list of maps from strings to strings") {
val input = listOf(
mapOf(
Expand Down Expand Up @@ -330,6 +391,22 @@ object YamlWritingTest : Spek({
)
}
}
context("serializing a list of objects in flow form") {
val input = listOf(
SimpleStructure("name1"),
SimpleStructure("name2")
)

val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow)).encodeToString(ListSerializer(SimpleStructure.serializer()), input)

it("returns the value serialized in the expected YAML form") {
expect(output).toBe(
"""
[{name: "name1"}, {name: "name2"}]
""".trimIndent()
)
}
}
}

describe("serializing maps") {
Expand Down

0 comments on commit a4464df

Please sign in to comment.