Skip to content

Commit

Permalink
implicit conversion from Char to String in DataColumn.convertTo and D…
Browse files Browse the repository at this point in the history
…ataFrame.convert()
  • Loading branch information
Jolanrensen committed Dec 11, 2024
1 parent df2ca63 commit f86a6cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,13 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n

Char::class -> when (toClass) {
Int::class -> convert<Char> { it.code }
else -> null

else -> // convert char to string and then to target type
getConverter(typeOf<String>(), to, options)?.let { stringConverter ->
convert<Char> {
stringConverter(it.toString())
}
}
}

Int::class -> when (toClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.api
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalTime
Expand Down Expand Up @@ -69,6 +70,20 @@ class ConvertTests {
@Test
fun `convert string to enum`() {
columnOf("A", "B").convertTo<EnumClass>() shouldBe columnOf(EnumClass.A, EnumClass.B)

dataFrameOf(columnOf("A", "B") named "colA")
.convert("colA").to<EnumClass>()
.getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA")
}

@Test
fun `convert char to enum`() {
// Char -> String -> Enum
columnOf('A', 'B').convertTo<EnumClass>() shouldBe columnOf(EnumClass.A, EnumClass.B)

dataFrameOf(columnOf('A', 'B') named "colA")
.convert("colA").to<EnumClass>()
.getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA")
}

@JvmInline
Expand Down Expand Up @@ -199,6 +214,13 @@ class ConvertTests {
val col = columnOf(65, 66)
col.convertTo<Char>() shouldBe columnOf('A', 'B')
col.convertTo<Char>().convertTo<Int>() shouldBe col

// this means
columnOf('1', '2').convertToInt() shouldNotBe columnOf(1, 2)
columnOf('1', '2').convertToInt() shouldBe columnOf(49, 50)

// but
columnOf('1', '2').convertToString().convertToInt() shouldBe columnOf(1, 2)
}

@Test
Expand Down

0 comments on commit f86a6cc

Please sign in to comment.