Skip to content

Commit

Permalink
feat: Support ByteArray for String, Char
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Jun 10, 2024
1 parent 85ca9ef commit c4b6b78
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal sealed class Content<T>(val type: KType) {
deserializer: Deserializer,
path: String,
value: Any,
) = value.toString()
) = deserializer.string(value)

override fun serialize(
path: String,
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/dev/s7a/ktconfig/internal/Deserializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,6 +186,7 @@ internal class Deserializer {
return when (value) {
is Number -> char(value)
is String -> char(value)
is ByteArray -> char(value)
else -> null
}
}
Expand All @@ -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)
Expand Down
191 changes: 191 additions & 0 deletions src/test/kotlin/types/CharTest.kt
Original file line number Diff line number Diff line change
@@ -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<Char>(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<Char>(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())),
)
}
}
Loading

0 comments on commit c4b6b78

Please sign in to comment.