Skip to content

Commit

Permalink
fix: Rename annotations, Use existing enums, Add Folded output
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Aug 11, 2024
1 parent a42e592 commit 1cdeea2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
19 changes: 4 additions & 15 deletions src/commonMain/kotlin/com/charleskorn/kaml/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public annotation class YamlComment(
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.BINARY)
@SerialInfo
public annotation class YamlWriteSingleLineStringUsingScalarStyle(
val stringScalarStyle: StringScalarStyle,
public annotation class YamlSingleLineStringStyle(
val singleLineStringStyle: SingleLineStringStyle,
)

/**
Expand All @@ -53,17 +53,6 @@ public annotation class YamlWriteSingleLineStringUsingScalarStyle(
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.BINARY)
@SerialInfo
public annotation class YamlWriteMultiLineStringUsingScalarStyle(
val stringScalarStyle: StringScalarStyle,
public annotation class YamlMultiLineStringStyle(
val multiLineStringStyle: MultiLineStringStyle,
)

/**
* The scalar styles that can be used in the annotation to force a specific field to be rendered in.
*/
public enum class StringScalarStyle {
DOUBLE_QUOTED,
SINGLE_QUOTED,
LITERAL,
FOLDED,
PLAIN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ 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
* * [singleLineStringStyle]: the style in which a single line String value is written. Can be overruled for a specific field with the [YamlWriteSingleLineStringUsingScalarStyle] annotation.
* * [multiLineStringStyle]: the style in which a multi line String value is written. Can be overruled for a specific field with the [YamlWriteMultiLineStringUsingScalarStyle] annotation.
* * [singleLineStringStyle]: the style in which a single line String value is written. Can be overruled for a specific field with the [YamlSingleLineStringStyle] annotation.
* * [multiLineStringStyle]: the style in which a multi line String value is written. Can be overruled for a specific field with the [YamlMultiLineStringStyle] annotation.
* * [ambiguousQuoteStyle]: how strings should be escaped when [singleLineStringStyle] is [SingleLineStringStyle.PlainExceptAmbiguous] and the value is ambiguous
* * [sequenceBlockIndent]: number of spaces to use as indentation for sequences, if [sequenceStyle] set to [SequenceStyle.Block]
* * [allowAnchorsAndAliases]: set to true to allow anchors and aliases when decoding YAML (defaults to `false`)
* * [yamlNamingStrategy]: The system that converts the field names in to the names used in the Yaml.
* * [codePointLimit]: the maximum amount of code points allowed in the input YAML document (defaults to 3 MB)
*/
public data class YamlConfiguration(
Expand Down Expand Up @@ -84,6 +85,7 @@ public enum class SequenceStyle {

public enum class MultiLineStringStyle {
Literal,
Folded,
DoubleQuoted,
SingleQuoted,
Plain,
Expand Down
22 changes: 7 additions & 15 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ internal class YamlOutput(
override fun encodeLong(value: Long) = emitPlainScalar(value.toString())
override fun encodeShort(value: Short) = emitPlainScalar(value.toString())

private var forcedSingleLineScalarStyle: StringScalarStyle? = null
private var forcedMultiLineScalarStyle: StringScalarStyle? = null
private var forcedSingleLineScalarStyle: SingleLineStringStyle? = null
private var forcedMultiLineScalarStyle: MultiLineStringStyle? = null

override fun encodeString(value: String) {
if (shouldReadTypeName) {
currentTypeName = value
shouldReadTypeName = false
} else {
val singleLineScalarStyle = forcedSingleLineScalarStyle ?.toScalarStyle() ?: configuration.singleLineStringStyle.scalarStyle
val multiLineScalarStyle = forcedMultiLineScalarStyle ?.toScalarStyle() ?: configuration.multiLineStringStyle.scalarStyle
val singleLineScalarStyle = forcedSingleLineScalarStyle ?.scalarStyle ?: configuration.singleLineStringStyle.scalarStyle
val multiLineScalarStyle = forcedMultiLineScalarStyle ?.scalarStyle ?: configuration.multiLineStringStyle.scalarStyle
when {
value.contains('\n')
-> emitScalar(value, multiLineScalarStyle)
Expand Down Expand Up @@ -124,8 +124,8 @@ internal class YamlOutput(
}

// If this field was annotated we overrule the used ScalarStyle with the annotation
forcedSingleLineScalarStyle = descriptor.getAnnotation<YamlWriteSingleLineStringUsingScalarStyle>(index)?.stringScalarStyle
forcedMultiLineScalarStyle = descriptor.getAnnotation<YamlWriteMultiLineStringUsingScalarStyle>(index)?.stringScalarStyle
forcedSingleLineScalarStyle = descriptor.getAnnotation<YamlSingleLineStringStyle>(index)?.singleLineStringStyle
forcedMultiLineScalarStyle = descriptor.getAnnotation<YamlMultiLineStringStyle>(index)?.multiLineStringStyle

return super.encodeElement(descriptor, index)
}
Expand Down Expand Up @@ -228,6 +228,7 @@ internal class YamlOutput(
MultiLineStringStyle.DoubleQuoted -> ScalarStyle.DOUBLE_QUOTED
MultiLineStringStyle.SingleQuoted -> ScalarStyle.SINGLE_QUOTED
MultiLineStringStyle.Literal -> ScalarStyle.LITERAL
MultiLineStringStyle.Folded -> ScalarStyle.FOLDED
MultiLineStringStyle.Plain -> ScalarStyle.PLAIN
}

Expand All @@ -250,12 +251,3 @@ internal class YamlOutput(
private val ALL_EXPLICIT = ImplicitTuple(false, false)
}
}

internal fun StringScalarStyle.toScalarStyle(): ScalarStyle =
when (this) {
StringScalarStyle.DOUBLE_QUOTED -> ScalarStyle.DOUBLE_QUOTED
StringScalarStyle.SINGLE_QUOTED -> ScalarStyle.SINGLE_QUOTED
StringScalarStyle.LITERAL -> ScalarStyle.LITERAL
StringScalarStyle.FOLDED -> ScalarStyle.FOLDED
StringScalarStyle.PLAIN -> ScalarStyle.PLAIN
}
63 changes: 37 additions & 26 deletions src/jvmTest/kotlin/com/charleskorn/kaml/JvmYamlWritingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class JvmYamlWritingTest : DescribeSpec({
val thing = ThingSL(
"Name of Thing",
"String",
"Literal",
"Folded",
"Single Quoted",
"Double Quoted",
"Plain",
)

Expand All @@ -192,10 +192,8 @@ class JvmYamlWritingTest : DescribeSpec({
"""
name: "Name of Thing"
string: "String"
literal: |-
Literal
folded: >-
Folded
singleQuoted: 'Single Quoted'
doubleQuoted: "Double Quoted"
plain: Plain
""".trimIndent()
Expand All @@ -206,8 +204,8 @@ class JvmYamlWritingTest : DescribeSpec({
val thing = ThingSL(
"Name of Thing",
"String 1\nString 2\nString 3\n",
"Literal 1\nLiteral 2\nLiteral 3\n",
"Folded 1\nFolded 2\nFolded 3\n",
"Single Quoted 1\nSingle Quoted 2\nSingle Quoted 3\n",
"Double Quoted 1\nDouble Quoted 2\nDouble Quoted 3\n",
"Plain 1\nPlain 2\nPlain 3\n",
)

Expand All @@ -216,10 +214,22 @@ class JvmYamlWritingTest : DescribeSpec({
"""
name: "Name of Thing"
string: "String 1\nString 2\nString 3\n"
literal: "Literal 1\nLiteral 2\nLiteral 3\n"
folded: "Folded 1\nFolded 2\nFolded 3\n"
plain: "Plain 1\nPlain 2\nPlain 3\n"
singleQuoted: 'Single Quoted 1
Single Quoted 2
Single Quoted 3
'
doubleQuoted: "Double Quoted 1\nDouble Quoted 2\nDouble Quoted 3\n"
plain: 'Plain 1
Plain 2
Plain 3
'
""".trimIndent()
}

Expand All @@ -240,7 +250,7 @@ class JvmYamlWritingTest : DescribeSpec({
string: "String"
literal: "Literal"
folded: "Folded"
plain: "Plain"
plain: Plain
""".trimIndent()
}
Expand Down Expand Up @@ -277,6 +287,7 @@ class JvmYamlWritingTest : DescribeSpec({
Plain 3
'
""".trimIndent()
}

Expand All @@ -300,14 +311,14 @@ data class ThingSL(
val name: String,
// Without any annotations
val string: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.LITERAL)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
val literal: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.FOLDED)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
val folded: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.PLAIN)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
@YamlSingleLineStringStyle(SingleLineStringStyle.SingleQuoted)
@YamlMultiLineStringStyle(MultiLineStringStyle.SingleQuoted)
val singleQuoted: String,
@YamlSingleLineStringStyle(SingleLineStringStyle.DoubleQuoted)
@YamlMultiLineStringStyle(MultiLineStringStyle.DoubleQuoted)
val doubleQuoted: String,
@YamlSingleLineStringStyle(SingleLineStringStyle.Plain)
@YamlMultiLineStringStyle(MultiLineStringStyle.Plain)
val plain: String,
)

Expand All @@ -316,13 +327,13 @@ data class ThingML(
val name: String,
// Without any annotations
val string: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.LITERAL)
@YamlSingleLineStringStyle(SingleLineStringStyle.DoubleQuoted)
@YamlMultiLineStringStyle(MultiLineStringStyle.Literal)
val literal: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.FOLDED)
@YamlSingleLineStringStyle(SingleLineStringStyle.DoubleQuoted)
@YamlMultiLineStringStyle(MultiLineStringStyle.Folded)
val folded: String,
@YamlWriteSingleLineStringUsingScalarStyle(StringScalarStyle.DOUBLE_QUOTED)
@YamlWriteMultiLineStringUsingScalarStyle(StringScalarStyle.PLAIN)
@YamlSingleLineStringStyle(SingleLineStringStyle.Plain)
@YamlMultiLineStringStyle(MultiLineStringStyle.Plain)
val plain: String,
)

0 comments on commit 1cdeea2

Please sign in to comment.