From f17001bc829108360319cd063e9b970d5752d57a Mon Sep 17 00:00:00 2001 From: Andrey Kuleshov Date: Fri, 8 Mar 2024 18:05:35 +0300 Subject: [PATCH] Primitive Char should be encoded with a single quote (#261) ### What's done: - As per the TOML specification, there is no predefined Char type. However, we've introduced the concept in our TOML implementation to align more closely with Kotlin's syntax. So we are expecting Chars to have single quote on the decoding. We need to make it more convenient and do encoding and decoding in a similar way. --- .../ktoml/encoders/TomlAbstractEncoder.kt | 9 ++++++++- .../ktoml/encoders/PrimitiveEncoderTest.kt | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/encoders/TomlAbstractEncoder.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/encoders/TomlAbstractEncoder.kt index 61a3584f..59fcacc6 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/encoders/TomlAbstractEncoder.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/encoders/TomlAbstractEncoder.kt @@ -152,7 +152,14 @@ public abstract class TomlAbstractEncoder protected constructor( override fun encodeShort(value: Short): Unit = encodeLong(value.toLong()) override fun encodeInt(value: Int): Unit = encodeLong(value.toLong()) override fun encodeFloat(value: Float): Unit = encodeDouble(value.toDouble()) - override fun encodeChar(value: Char): Unit = encodeString(value.toString()) + + /** + * https://github.com/akuleshov7/ktoml/issues/260 + * As per the TOML specification, there is no predefined Char type. + * However, we've introduced the concept in our TOML implementation to align more closely with Kotlin's syntax. + * So we are expecting Chars to have single quote on the decoding. So encoding should be done in a similar way. + */ + override fun encodeChar(value: Char): Unit = appendValue(TomlLiteralString(value.toString())) // Structure diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt index 7f9644be..16369405 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt @@ -126,4 +126,17 @@ class PrimitiveEncoderTest { expectedToml = "wholeNumberDouble = 3.0" ) } + + @Test + fun charRegression() { + @Serializable + data class File( + val charVal: Char = 'W' + ) + + assertEncodedEquals( + value = File(), + expectedToml = "charVal = \'W\'" + ) + } }