8000 Add a configurable indent for sequences by Cloudate9 · Pull Request #317 · charleskorn/kaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add a configurable indent for sequences #317

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 8000 privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 15, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package com.charleskorn.kaml
* * [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
* * [sequenceBlockIndent]: number of spaces to use as indentation for sequences, if [sequenceStyle] set to [SequenceStyle.Block]
*/
public data class YamlConfiguration constructor(
internal val encodeDefaults: Boolean = true,
Expand All @@ -42,7 +43,8 @@ public data class YamlConfiguration constructor(
internal val breakScalarsAt: Int = 80,
internal val sequenceStyle: SequenceStyle = SequenceStyle.Block,
internal val singleLineStringStyle: SingleLineStringStyle = SingleLineStringStyle.DoubleQuoted,
internal val multiLineStringStyle: MultiLineStringStyle = singleLineStringStyle.multiLineStringStyle
internal val multiLineStringStyle: MultiLineStringStyle = singleLineStringStyle.multiLineStringStyle,
internal val sequenceBlockIndent: Int = 0
)

public enum class PolymorphismStyle {
Expand Down
52 changes: 52 additions & 0 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlWritingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,58 @@ class YamlWritingTest : DescribeSpec({
}
}

context("serializing a list of integers in flow form with sequence block indent") {
val output = Yaml(configuration = YamlConfiguration(sequenceStyle = SequenceStyle.Flow, sequenceBlockIndent = 2))
.encodeToString(ListSerializer(Int.serializer()), listOf(1, 2, 3))

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

context("serializing a list of integers without sequence block indent") {
val output = Yaml(configuration = YamlConfiguration(sequenceBlockIndent = 0))
.encodeToString(ListSerializer(Int.serializer()), listOf(1, 2, 3))

it("returns the value serialized in the expected YAML form") {
output shouldBe
"""
|- 1
|- 2
|- 3
""".trimMargin()
}
}

context("serializing a list of integers with sequence block indent") {
val output = Yaml(configuration = YamlConfiguration(sequenceBlockIndent = 2))
.encodeToString(ListSerializer(Int.serializer()), listOf(1, 2, 3))

it("returns the value serialized in the expected YAML form") {
output shouldBe
"""
| - 1
| - 2
| - 3
""".trimMargin()
}
}

context("serializing a list of objects with sequence block indent") {
@Serializable
data class Foo(val bar: String)

val output = Yaml(configuration = YamlConfiguration(sequenceBlockIndent = 2))
.encodeToString(ListSerializer(Foo.serializer()), listOf(Foo("baz")))

it("returns the value serialized in the expected YAML form") {
output shouldBe
"""
| - bar: "baz"
""".trimMargin()
}
}

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))
Expand Down
12 changes: 7 additions & 5 deletions src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ internal class YamlOutput(
private val configuration: YamlConfiguration
) : AbstractEncoder(), AutoCloseable {
private val settings = DumpSettings.builder()
// SnakeYAML validates that this value must be at least 1
.setIndent(configuration.encodingIndentationSize)
// SnakeYAML helps to validate that this value must be non-negative
.setIndicatorIndent(configuration.sequenceBlockIndent)
// No special reason why true is conditional. Designed to be consistent with 0.46.0 of kaml
.setIndentWithIndicator(configuration.sequenceBlockIndent > 0)
// Unclear if this value is validated
.setWidth(configuration.breakScalarsAt)
.build()

Expand Down Expand Up @@ -149,11 +155,7 @@ internal class YamlOutput(
private fun encodeComment(descriptor: SerialDescriptor, index: Int) {
val commentAnno = descriptor.getElementAnnotations(index)
.filterIsInstance<YamlComment>()
.firstOrNull()

if (commentAnno == null) {
return
}
.firstOrNull() ?: return

for (line in commentAnno.lines) {
emitter.emit(CommentEvent(CommentType.BLOCK, " $line", Optional.empty(), Optional.empty()))
Expand Down
0