Skip to content

Commit a46d3ad

Browse files
committed
asArrayAsListOrNull with asList
1 parent 23a7469 commit a46d3ad

File tree

8 files changed

+70
-80
lines changed

8 files changed

+70
-80
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/TypeUtils.kt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -544,28 +544,25 @@ internal val Any.isArray: Boolean
544544
get() = this::class.isArray
545545

546546
/**
547-
* Returns this array object as a list of values.
548-
*
549-
* @throws IllegalArgumentException if this object is not an array
547+
* If [this] is an array of any kind, the function returns it as a list of values,
548+
* else it returns `null`.
550549
*/
551-
internal fun Any.asArrayToList(): List<*> {
552-
require(this.isArray) { "Not an array" }
553-
return when (this) {
554-
is BooleanArray -> toList()
555-
is ByteArray -> toList()
556-
is ShortArray -> toList()
557-
is IntArray -> toList()
558-
is LongArray -> toList()
559-
is FloatArray -> toList()
560-
is DoubleArray -> toList()
561-
is CharArray -> toList()
562-
563-
is UByteArray -> toList()
564-
is UShortArray -> toList()
565-
is UIntArray -> toList()
566-
is ULongArray -> toList()
567-
568-
is Array<*> -> toList()
569-
else -> error("Not an array")
550+
internal fun Any.asArrayAsListOrNull(): List<*>? =
551+
when (this) {
552+
is BooleanArray -> asList()
553+
is ByteArray -> asList()
554+
is ShortArray -> asList()
555+
is IntArray -> asList()
556+
is LongArray -> asList()
557+
is FloatArray -> asList()
558+
is DoubleArray -> asList()
559+
is CharArray -> asList()
560+
561+
is UByteArray -> asList()
562+
is UShortArray -> asList()
563+
is UIntArray -> asList()
564+
is ULongArray -> asList()
565+
566+
is Array<*> -> asList()
567+
else -> null
570568
}
571-
}

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/string.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import org.jetbrains.kotlinx.dataframe.api.columnsCount
77
import org.jetbrains.kotlinx.dataframe.api.isNumber
88
import org.jetbrains.kotlinx.dataframe.api.take
99
import org.jetbrains.kotlinx.dataframe.api.toColumn
10-
import org.jetbrains.kotlinx.dataframe.impl.asArrayToList
11-
import org.jetbrains.kotlinx.dataframe.impl.isArray
10+
import org.jetbrains.kotlinx.dataframe.impl.asArrayAsListOrNull
1211
import org.jetbrains.kotlinx.dataframe.impl.owner
1312
import org.jetbrains.kotlinx.dataframe.impl.renderType
1413
import org.jetbrains.kotlinx.dataframe.impl.scale
@@ -177,11 +176,9 @@ internal fun renderValueToString(value: Any?, decimalFormat: RendererDecimalForm
177176
is List<*> -> if (value.isEmpty()) "[ ]" else value.toString()
178177
is Array<*> -> if (value.isEmpty()) "[ ]" else value.toList().toString()
179178
else ->
180-
if (value?.isArray == true) {
181-
renderValueToString(value.asArrayToList(), decimalFormat)
182-
} else {
183-
value.toString()
184-
}
179+
value
180+
?.asArrayAsListOrNull()?.let { renderValueToString(it, decimalFormat) }
181+
?: value.toString()
185182
}
186183

187184
internal fun internallyRenderable(value: Any?): Boolean {

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ class CreateDataFrameTests {
220220
@Test
221221
fun builtInTypes() {
222222
val string = listOf("aaa", "aa", null)
223-
string.toDataFrame().also { it.print() } shouldBe dataFrameOf("value")(*string.toTypedArray())
223+
string.toDataFrame() shouldBe dataFrameOf("value")(*string.toTypedArray())
224224

225225
val int = listOf(1, 2, 3)
226-
int.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*int.toTypedArray())
226+
int.toDataFrame() shouldBe dataFrameOf("value")(*int.toTypedArray())
227227

228228
val doubles = listOf(1.0, 2.0, 3.0)
229-
doubles.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*doubles.toTypedArray())
229+
doubles.toDataFrame() shouldBe dataFrameOf("value")(*doubles.toTypedArray())
230230

231231
val floats = listOf(1.0f, 2.0f, 3.0f)
232-
floats.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*floats.toTypedArray())
232+
floats.toDataFrame() shouldBe dataFrameOf("value")(*floats.toTypedArray())
233233
}
234234

235235
@Ignore

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/types/UtilTests.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.types
22

33
import io.kotest.matchers.shouldBe
4-
import org.jetbrains.kotlinx.dataframe.impl.asArrayToList
4+
import org.jetbrains.kotlinx.dataframe.impl.asArrayAsListOrNull
55
import org.jetbrains.kotlinx.dataframe.impl.commonParent
66
import org.jetbrains.kotlinx.dataframe.impl.commonParents
77
import org.jetbrains.kotlinx.dataframe.impl.commonType
@@ -62,11 +62,12 @@ class UtilTests {
6262
arrayOfNulls<Any?>(1).isPrimitiveArray shouldBe false
6363

6464
// Any asArrayToList
65-
booleanArrayOf(true, false).asArrayToList() shouldBe listOf(true, false)
66-
uintArrayOf(1u, 2u).asArrayToList() shouldBe listOf(1u, 2u)
67-
arrayOf(1, 2).asArrayToList() shouldBe listOf(1, 2)
68-
arrayOf(1, null).asArrayToList() shouldBe listOf(1, null)
69-
arrayOfNulls<Any?>(1).asArrayToList() shouldBe listOf(null)
65+
booleanArrayOf(true, false).asArrayAsListOrNull() shouldBe listOf(true, false)
66+
uintArrayOf(1u, 2u).asArrayAsListOrNull() shouldBe listOf(1u, 2u)
67+
arrayOf(1, 2).asArrayAsListOrNull() shouldBe listOf(1, 2)
68+
arrayOf(1, null).asArrayAsListOrNull() shouldBe listOf(1, null)
69+
arrayOfNulls<Any?>(1).asArrayAsListOrNull() shouldBe listOf(null)
70+
1.asArrayAsListOrNull() shouldBe null
7071
}
7172

7273
@Test

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/TypeUtils.kt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -544,28 +544,25 @@ internal val Any.isArray: Boolean
544544
get() = this::class.isArray
545545

546546
/**
547-
* Returns this array object as a list of values.
548-
*
549-
* @throws IllegalArgumentException if this object is not an array
547+
* If [this] is an array of any kind, the function returns it as a list of values,
548+
* else it returns `null`.
550549
*/
551-
internal fun Any.asArrayToList(): List<*> {
552-
require(this.isArray) { "Not an array" }
553-
return when (this) {
554-
is BooleanArray -> toList()
555-
is ByteArray -> toList()
556-
is ShortArray -> toList()
557-
is IntArray -> toList()
558-
is LongArray -> toList()
559-
is FloatArray -> toList()
560-
is DoubleArray -> toList()
561-
is CharArray -> toList()
562-
563-
is UByteArray -> toList()
564-
is UShortArray -> toList()
565-
is UIntArray -> toList()
566-
is ULongArray -> toList()
567-
568-
is Array<*> -> toList()
569-
else -> error("Not an array")
550+
internal fun Any.asArrayAsListOrNull(): List<*>? =
551+
when (this) {
552+
is BooleanArray -> asList()
553+
is ByteArray -> asList()
554+
is ShortArray -> asList()
555+
is IntArray -> asList()
556+
is LongArray -> asList()
557+
is FloatArray -> asList()
558+
is DoubleArray -> asList()
559+
is CharArray -> asList()
560+
561+
is UByteArray -> asList()
562+
is UShortArray -> asList()
563+
is UIntArray -> asList()
564+
is ULongArray -> asList()
565+
566+
is Array<*> -> asList()
567+
else -> null
570568
}
571-
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/string.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import org.jetbrains.kotlinx.dataframe.api.columnsCount
77
import org.jetbrains.kotlinx.dataframe.api.isNumber
88
import org.jetbrains.kotlinx.dataframe.api.take
99
import org.jetbrains.kotlinx.dataframe.api.toColumn
10-
import org.jetbrains.kotlinx.dataframe.impl.asArrayToList
11-
import org.jetbrains.kotlinx.dataframe.impl.isArray
10+
import org.jetbrains.kotlinx.dataframe.impl.asArrayAsListOrNull
1211
import org.jetbrains.kotlinx.dataframe.impl.owner
1312
import org.jetbrains.kotlinx.dataframe.impl.renderType
1413
import org.jetbrains.kotlinx.dataframe.impl.scale
@@ -177,11 +176,9 @@ internal fun renderValueToString(value: Any?, decimalFormat: RendererDecimalForm
177176
is List<*> -> if (value.isEmpty()) "[ ]" else value.toString()
178177
is Array<*> -> if (value.isEmpty()) "[ ]" else value.toList().toString()
179178
else ->
180-
if (value?.isArray == true) {
181-
renderValueToString(value.asArrayToList(), decimalFormat)
182-
} else {
183-
value.toString()
184-
}
179+
value
180+
?.asArrayAsListOrNull()?.let { renderValueToString(it, decimalFormat) }
181+
?: value.toString()
185182
}
186183

187184
internal fun internallyRenderable(value: Any?): Boolean {

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ class CreateDataFrameTests {
220220
@Test
221221
fun builtInTypes() {
222222
val string = listOf("aaa", "aa", null)
223-
string.toDataFrame().also { it.print() } shouldBe dataFrameOf("value")(*string.toTypedArray())
223+
string.toDataFrame() shouldBe dataFrameOf("value")(*string.toTypedArray())
224224

225225
val int = listOf(1, 2, 3)
226-
int.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*int.toTypedArray())
226+
int.toDataFrame() shouldBe dataFrameOf("value")(*int.toTypedArray())
227227

228228
val doubles = listOf(1.0, 2.0, 3.0)
229-
doubles.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*doubles.toTypedArray())
229+
doubles.toDataFrame() shouldBe dataFrameOf("value")(*doubles.toTypedArray())
230230

231231
val floats = listOf(1.0f, 2.0f, 3.0f)
232-
floats.toDataFrame().alsoDebug() shouldBe dataFrameOf("value")(*floats.toTypedArray())
232+
floats.toDataFrame() shouldBe dataFrameOf("value")(*floats.toTypedArray())
233233
}
234234

235235
@Ignore

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/types/UtilTests.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.types
22

33
import io.kotest.matchers.shouldBe
4-
import org.jetbrains.kotlinx.dataframe.impl.asArrayToList
4+
import org.jetbrains.kotlinx.dataframe.impl.asArrayAsListOrNull
55
import org.jetbrains.kotlinx.dataframe.impl.commonParent
66
import org.jetbrains.kotlinx.dataframe.impl.commonParents
77
import org.jetbrains.kotlinx.dataframe.impl.commonType
@@ -62,11 +62,12 @@ class UtilTests {
6262
arrayOfNulls<Any?>(1).isPrimitiveArray shouldBe false
6363

6464
// Any asArrayToList
65-
booleanArrayOf(true, false).asArrayToList() shouldBe listOf(true, false)
66-
uintArrayOf(1u, 2u).asArrayToList() shouldBe listOf(1u, 2u)
67-
arrayOf(1, 2).asArrayToList() shouldBe listOf(1, 2)
68-
arrayOf(1, null).asArrayToList() shouldBe listOf(1, null)
69-
arrayOfNulls<Any?>(1).asArrayToList() shouldBe listOf(null)
65+
booleanArrayOf(true, false).asArrayAsListOrNull() shouldBe listOf(true, false)
66+
uintArrayOf(1u, 2u).asArrayAsListOrNull() shouldBe listOf(1u, 2u)
67+
arrayOf(1, 2).asArrayAsListOrNull() shouldBe listOf(1, 2)
68+
arrayOf(1, null).asArrayAsListOrNull() shouldBe listOf(1, null)
69+
arrayOfNulls<Any?>(1).asArrayAsListOrNull() shouldBe listOf(null)
70+
1.asArrayAsListOrNull() shouldBe null
7071
}
7172

7273
@Test

0 commit comments

Comments
 (0)