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

Already on GitHub? Sign in to your account

Primitive Char should be encoded with a single quote #261

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\'"
)
}
}
Loading