From c4b6b78fe8a34b2f61c8f0166d2907f9a1947b6c Mon Sep 17 00:00:00 2001 From: sya-ri Date: Sun, 9 Jun 2024 00:50:51 +0900 Subject: [PATCH] feat: Support ByteArray for String, Char --- .../dev/s7a/ktconfig/internal/Content.kt | 2 +- .../dev/s7a/ktconfig/internal/Deserializer.kt | 12 + src/test/kotlin/types/CharTest.kt | 191 +++++++++++++++ src/test/kotlin/types/StringTest.kt | 223 ++++++++++++++++++ src/test/kotlin/utils/AssertKtConfigString.kt | 11 + .../kotlin/utils/AssertSaveKtConfigString.kt | 11 + src/test/kotlin/utils/Data.kt | 3 + src/test/kotlin/utils/NullableData.kt | 3 + 8 files changed, 455 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/types/CharTest.kt create mode 100644 src/test/kotlin/types/StringTest.kt create mode 100644 src/test/kotlin/utils/AssertKtConfigString.kt create mode 100644 src/test/kotlin/utils/AssertSaveKtConfigString.kt create mode 100644 src/test/kotlin/utils/Data.kt create mode 100644 src/test/kotlin/utils/NullableData.kt diff --git a/src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt b/src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt index 8e481e9..01f5ebe 100644 --- a/src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt +++ b/src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt @@ -37,7 +37,7 @@ internal sealed class Content(val type: KType) { deserializer: Deserializer, path: String, value: Any, - ) = value.toString() + ) = deserializer.string(value) override fun serialize( path: String, diff --git a/src/main/kotlin/dev/s7a/ktconfig/internal/Deserializer.kt b/src/main/kotlin/dev/s7a/ktconfig/internal/Deserializer.kt index 322a34e..18031c6 100644 --- a/src/main/kotlin/dev/s7a/ktconfig/internal/Deserializer.kt +++ b/src/main/kotlin/dev/s7a/ktconfig/internal/Deserializer.kt @@ -25,6 +25,13 @@ internal class Deserializer { return SnakeYamlReflection.getScalarNode(value) } + fun string(value: Any): String { + return when (value) { + is ByteArray -> value.decodeToString() + else -> value.toString() + } + } + private fun number(value: String): Number? { return runCatching { constructYamlInt.construct(node(value)) as Number @@ -179,6 +186,7 @@ internal class Deserializer { return when (value) { is Number -> char(value) is String -> char(value) + is ByteArray -> char(value) else -> null } } @@ -191,6 +199,10 @@ internal class Deserializer { return value.singleOrNull() } + private fun char(value: ByteArray): Char? { + return char(value.decodeToString()) + } + fun short(value: Any): Short? { return when (value) { is Number -> short(value) diff --git a/src/test/kotlin/types/CharTest.kt b/src/test/kotlin/types/CharTest.kt new file mode 100644 index 0000000..fd456a1 --- /dev/null +++ b/src/test/kotlin/types/CharTest.kt @@ -0,0 +1,191 @@ +package types + +import utils.Data +import utils.NullableData +import utils.assertKtConfigString +import utils.assertSaveKtConfigString +import kotlin.test.Test + +class CharTest { + @Test + fun `should get char from config`() { + assertKtConfigString( + Data('a'), + """ + value: a + """.trimIndent(), + ) + assertKtConfigString( + NullableData('a'), + """ + value: a + """.trimIndent(), + ) + } + + @Test + fun `should get null from config`() { + assertKtConfigString( + NullableData(null), + """ + value: null + """.trimIndent(), + ) + } + + @Test + fun `should get int as char from config`() { + assertKtConfigString( + Data(0x2), + """ + value: 2 + """.trimIndent(), + ) + assertKtConfigString( + Data('2'), + """ + value: '2' + """.trimIndent(), + ) + } + + @Test + fun `should get byte array as char from config`() { + assertKtConfigString( + Data(2.toChar()), + """ + value: !!binary |- + Ag== + """.trimIndent(), + ) + } + + @Test + fun `should get char as list from config`() { + assertKtConfigString( + Data(listOf('a')), + """ + value: a + """.trimIndent(), + ) + assertKtConfigString( + NullableData(listOf('a')), + """ + value: a + """.trimIndent(), + ) + } + + @Test + fun `should get char list (single value) from config`() { + assertKtConfigString( + Data(listOf('a')), + """ + value: + - a + """.trimIndent(), + ) + assertKtConfigString( + NullableData(listOf('a')), + """ + value: + - a + """.trimIndent(), + ) + } + + @Test + fun `should get char list (multiple values) from config`() { + assertKtConfigString( + Data(listOf('a', '6', 9.toChar(), 2.toChar())), + """ + value: + - a + - '6' + - 9 + - !!binary |- + Ag== + """.trimIndent(), + ) + } + + @Test + fun `should save char to config`() { + assertSaveKtConfigString( + """ + value: a + + """.trimIndent(), + Data('a'), + ) + assertSaveKtConfigString( + """ + value: a + + """.trimIndent(), + NullableData('a'), + ) + } + + @Test + fun `should save null to config`() { + assertSaveKtConfigString( + "", + NullableData(null), + ) + } + + @Test + fun `should save int to config`() { + assertSaveKtConfigString( + """ + value: 2 + + """.trimIndent(), + Data(0x2), + ) + assertSaveKtConfigString( + """ + value: '2' + + """.trimIndent(), + Data('2'), + ) + } + + @Test + fun `should save char list (single value) to config`() { + assertSaveKtConfigString( + """ + value: + - a + + """.trimIndent(), + Data(listOf('a')), + ) + assertSaveKtConfigString( + """ + value: + - a + + """.trimIndent(), + NullableData(listOf('a')), + ) + } + + @Test + fun `should save char list (multiple values) to config`() { + assertSaveKtConfigString( + """ + value: + - a + - '6' + - "\t" + - !!binary |- + Ag== + + """.trimIndent(), + Data(listOf('a', '6', 9.toChar(), 2.toChar())), + ) + } +} diff --git a/src/test/kotlin/types/StringTest.kt b/src/test/kotlin/types/StringTest.kt new file mode 100644 index 0000000..2180cc7 --- /dev/null +++ b/src/test/kotlin/types/StringTest.kt @@ -0,0 +1,223 @@ +package types + +import utils.Data +import utils.NullableData +import utils.assertKtConfigString +import utils.assertSaveKtConfigString +import kotlin.test.Test + +class StringTest { + @Test + fun `should get string from config`() { + assertKtConfigString( + Data("config string"), + """ + value: config string + """.trimIndent(), + ) + assertKtConfigString( + NullableData("config string"), + """ + value: config string + """.trimIndent(), + ) + } + + @Test + fun `should get char from config`() { + assertKtConfigString( + Data("a"), + """ + value: a + """.trimIndent(), + ) + assertKtConfigString( + NullableData("a"), + """ + value: a + """.trimIndent(), + ) + } + + @Test + fun `should get 'null' from config`() { + assertKtConfigString( + Data("null"), + """ + value: "null" + """.trimIndent(), + ) + } + + @Test + fun `should get null from config`() { + assertKtConfigString( + NullableData(null), + """ + value: null + """.trimIndent(), + ) + } + + @Test + fun `should get int string from config`() { + assertKtConfigString( + Data("12"), + """ + value: 12 + """.trimIndent(), + ) + } + + @Test + fun `should get byte array as string from config`() { + assertKtConfigString( + Data(2.toChar().toString()), + """ + value: !!binary |- + Ag== + """.trimIndent(), + ) + } + + @Test + fun `should get string as list from config`() { + assertKtConfigString( + Data(listOf("config string")), + """ + value: config string + """.trimIndent(), + ) + assertKtConfigString( + NullableData(listOf("config string")), + """ + value: config string + """.trimIndent(), + ) + } + + @Test + fun `should get string list (single value) from config`() { + assertKtConfigString( + Data(listOf("config string")), + """ + value: + - config string + """.trimIndent(), + ) + assertKtConfigString( + NullableData(listOf("config string")), + """ + value: + - config string + """.trimIndent(), + ) + } + + @Test + fun `should get string list (multiple values) from config`() { + assertKtConfigString( + Data(listOf("config string", "other string", "")), + """ + value: + - config string + - other string + - "" + """.trimIndent(), + ) + } + + @Test + fun `should save string to config`() { + assertSaveKtConfigString( + """ + value: config string + + """.trimIndent(), + Data("config string"), + ) + assertSaveKtConfigString( + """ + value: config string + + """.trimIndent(), + NullableData("config string"), + ) + } + + @Test + fun `should save 'null' to config`() { + assertSaveKtConfigString( + """ + value: 'null' + + """.trimIndent(), + Data("null"), + ) + } + + @Test + fun `should save null to config`() { + assertSaveKtConfigString( + "", + NullableData(null), + ) + } + + @Test + fun `should save int string to config`() { + assertSaveKtConfigString( + """ + value: '12' + + """.trimIndent(), + Data("12"), + ) + } + + @Test + fun `should save byte array as string to config`() { + assertSaveKtConfigString( + """ + value: !!binary |- + Ag== + + """.trimIndent(), + Data(2.toChar().toString()), + ) + } + + @Test + fun `should save string list (single value) to config`() { + assertSaveKtConfigString( + """ + value: + - config string + + """.trimIndent(), + Data(listOf("config string")), + ) + assertSaveKtConfigString( + """ + value: + - config string + + """.trimIndent(), + NullableData(listOf("config string")), + ) + } + + @Test + fun `should save string list (multiple values) to config`() { + assertSaveKtConfigString( + """ + value: + - config string + - other string + - '' + + """.trimIndent(), + Data(listOf("config string", "other string", "")), + ) + } +} diff --git a/src/test/kotlin/utils/AssertKtConfigString.kt b/src/test/kotlin/utils/AssertKtConfigString.kt new file mode 100644 index 0000000..f9d337f --- /dev/null +++ b/src/test/kotlin/utils/AssertKtConfigString.kt @@ -0,0 +1,11 @@ +package utils + +import dev.s7a.ktconfig.ktConfigString +import kotlin.test.assertEquals + +inline fun assertKtConfigString( + expected: T, + yaml: String, +) { + assertEquals(expected, ktConfigString(yaml)) +} diff --git a/src/test/kotlin/utils/AssertSaveKtConfigString.kt b/src/test/kotlin/utils/AssertSaveKtConfigString.kt new file mode 100644 index 0000000..66eed05 --- /dev/null +++ b/src/test/kotlin/utils/AssertSaveKtConfigString.kt @@ -0,0 +1,11 @@ +package utils + +import dev.s7a.ktconfig.saveKtConfigString +import kotlin.test.assertEquals + +inline fun assertSaveKtConfigString( + expected: String, + data: T, +) { + assertEquals(expected, saveKtConfigString(data)) +} diff --git a/src/test/kotlin/utils/Data.kt b/src/test/kotlin/utils/Data.kt new file mode 100644 index 0000000..b3e5693 --- /dev/null +++ b/src/test/kotlin/utils/Data.kt @@ -0,0 +1,3 @@ +package utils + +data class Data(val value: T) diff --git a/src/test/kotlin/utils/NullableData.kt b/src/test/kotlin/utils/NullableData.kt new file mode 100644 index 0000000..56da321 --- /dev/null +++ b/src/test/kotlin/utils/NullableData.kt @@ -0,0 +1,3 @@ +package utils + +typealias NullableData = Data