@@ -27,6 +27,7 @@ import org.jetbrains.kotlinx.dataframe.api.to
27
27
import org.jetbrains.kotlinx.dataframe.columns.values
28
28
import org.jetbrains.kotlinx.dataframe.dataTypes.IFRAME
29
29
import org.jetbrains.kotlinx.dataframe.dataTypes.IMG
30
+ import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException
30
31
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException
31
32
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException
32
33
import org.jetbrains.kotlinx.dataframe.impl.columns.DataColumnInternal
@@ -79,31 +80,50 @@ internal fun AnyCol.convertToTypeImpl(to: KType): AnyCol {
79
80
else -> throw TypeConversionException (null , from, to)
80
81
}
81
82
82
- return when {
83
- from == to -> this
84
- from.isSubtypeOf(to) -> (this as DataColumnInternal <* >).changeType(to.withNullability(hasNulls()))
85
- else -> when (val converter = getConverter(from, to, ParserOptions (locale = Locale .getDefault()))) {
86
- null -> when (from.classifier) {
87
- Any ::class , Number ::class , java.io.Serializable ::class -> {
83
+ fun applyConverter (converter : TypeConverter ): AnyCol {
84
+ var currentRow = 0
85
+ try {
86
+ val values = values.mapIndexed { row, value ->
87
+ currentRow = row
88
+ value?.let { converter(value) }.checkNulls()
89
+ }
90
+ return DataColumn .createValueColumn(name, values, to.withNullability(nullsFound))
91
+ } catch (e: TypeConversionException ) {
92
+ throw CellConversionException (e.value, e.from, e.to, this .name(), currentRow, e)
93
+ }
94
+ }
95
+
96
+ fun convertPerCell (): AnyCol {
97
+ var currentRow = 0
98
+ try {
99
+ return when (from.classifier) {
100
+ Any ::class , Comparable ::class , Number ::class , java.io.Serializable ::class -> {
88
101
// find converter for every value
89
- val values = values.map {
90
- it?.let {
102
+ val values = values.mapIndexed { row, value ->
103
+ currentRow = row
104
+ value?.let {
91
105
val clazz = it.javaClass.kotlin
92
106
val type = clazz.createStarProjectedType(false )
93
- val converter = getConverter(type, to, ParserOptions (locale = Locale .getDefault())) ? : throw TypeConverterNotFoundException (from, to)
107
+ val converter = getConverter(type, to, ParserOptions (locale = Locale .getDefault()))
108
+ ? : throw TypeConverterNotFoundException (from, to)
94
109
converter(it)
95
110
}.checkNulls()
96
111
}
97
112
DataColumn .createValueColumn(name, values, to.withNullability(nullsFound))
98
113
}
99
114
else -> throw TypeConverterNotFoundException (from, to)
100
115
}
101
- else -> {
102
- val values = values.map {
103
- it?.let { converter(it) }.checkNulls()
104
- }
105
- DataColumn .createValueColumn(name, values, to.withNullability(nullsFound))
106
- }
116
+ } catch (e: TypeConversionException ) {
117
+ throw CellConversionException (e.value, e.from, e.to, this .name(), currentRow, e)
118
+ }
119
+ }
120
+
121
+ return when {
122
+ from == to -> this
123
+ from.isSubtypeOf(to) -> (this as DataColumnInternal <* >).changeType(to.withNullability(hasNulls()))
124
+ else -> when (val converter = getConverter(from, to, ParserOptions (locale = Locale .getDefault()))) {
125
+ null -> convertPerCell()
126
+ else -> applyConverter(converter)
107
127
}
108
128
}
109
129
}
0 commit comments