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

CellConversionException in DataColumn<String?>.convertToDouble(locale) #190

Merged
merged 3 commits into from
Nov 28, 2022
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 @@ -16,6 +16,7 @@ import org.jetbrains.kotlinx.dataframe.RowValueExpression
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.dataTypes.IFRAME
import org.jetbrains.kotlinx.dataframe.dataTypes.IMG
import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException
import org.jetbrains.kotlinx.dataframe.impl.api.Parsers
import org.jetbrains.kotlinx.dataframe.impl.api.convertRowColumnImpl
Expand Down Expand Up @@ -151,16 +152,28 @@ public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColum
*/
@JvmName("convertToDoubleFromStringNullable")
public fun DataColumn<String?>.convertToDouble(locale: Locale? = null): DataColumn<Double?> {
if (locale != null) {
fun applyParser(parser: (String) -> Double?): DataColumn<Double?> {
var currentRow = 0
try {
return mapIndexed { row, value ->
currentRow = row
value?.let { parser(value.trim()) ?: throw TypeConversionException(value, typeOf<String>(), typeOf<Double>()) }
}
} catch (e: TypeConversionException) {
throw CellConversionException(e.value, e.from, e.to, this.name(), currentRow, e)
}
}

return if (locale != null) {
val explicitParser = Parsers.getDoubleParser(locale)
return map { it?.let { explicitParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
applyParser(explicitParser)
} else {
return try {
try {
val defaultParser = Parsers.getDoubleParser()
map { it?.let { defaultParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
applyParser(defaultParser)
} catch (e: TypeConversionException) {
val posixParser = Parsers.getDoubleParser(Locale.forLanguageTag("C.UTF-8"))
map { it?.let { posixParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
applyParser(posixParser)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException
import org.jetbrains.kotlinx.dataframe.hasNulls
import org.junit.Test
import java.time.LocalTime
import kotlin.reflect.*
import kotlin.reflect.typeOf
import kotlin.time.Duration.Companion.hours

class ConvertTests {
Expand Down Expand Up @@ -85,6 +85,10 @@ class ConvertTests {
columnOf("1", "10", "a").convertTo<IntClass>()
}.row shouldBe 2

shouldThrow<CellConversionException> {
columnOf("1", "x", "2.5").convertToDouble()
}.row shouldBe 1

shouldThrow<TypeConverterNotFoundException> {
columnOf(EnumClass.A).convertTo<IntClass>()
}
Expand Down