Skip to content

Commit dc01d84

Browse files
committed
Add missing overloads for DataRow.containsKey()
1 parent 7f3c54e commit dc01d84

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

src/main/kotlin/org/jetbrains/kotlinx/dataframe/DataRow.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public interface DataRow<out T> {
3434
public fun getColumnGroup(columnName: String): AnyRow = get(columnName) as AnyRow
3535
public fun getOrNull(name: String): Any?
3636
public fun <R> getValueOrNull(column: ColumnReference<R>): R?
37-
public fun containsKey(name: String): Boolean
3837

3938
// endregion
4039

src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/DataFrameGet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public fun <T> ColumnsContainer<T>.getColumnGroupOrNull(name: String): ColumnGro
8282

8383
// region containsColumn
8484

85-
public fun ColumnsContainer<*>.containsColumn(column: Column): Boolean = containsColumn(column.path())
85+
public fun <C> ColumnsContainer<*>.containsColumn(column: ColumnReference<C>): Boolean = getColumnOrNull(column) != null
8686
public fun ColumnsContainer<*>.containsColumn(column: KProperty<*>): Boolean = containsColumn(column.columnName)
8787

8888
public operator fun ColumnsContainer<*>.contains(column: Column): Boolean = containsColumn(column)

src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/DataRowApi.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.api
22

33
import org.jetbrains.kotlinx.dataframe.AnyFrame
44
import org.jetbrains.kotlinx.dataframe.AnyRow
5+
import org.jetbrains.kotlinx.dataframe.Column
56
import org.jetbrains.kotlinx.dataframe.DataColumn
67
import org.jetbrains.kotlinx.dataframe.DataFrame
78
import org.jetbrains.kotlinx.dataframe.DataRow
@@ -33,13 +34,28 @@ public inline fun <reified R> AnyRow.namedValuesOf(): List<NameValuePair<R>> =
3334
public fun AnyRow.namedValues(): List<NameValuePair<Any?>> =
3435
values().zip(columnNames()) { value, name -> NameValuePair(name, value) }
3536

37+
// region getValue
38+
3639
public fun <T> AnyRow.getValue(columnName: String): T = get(columnName) as T
3740
public fun <T> AnyRow.getValue(column: ColumnReference<T>): T = get(column)
3841
public fun <T> AnyRow.getValue(column: KProperty<T>): T = get(column)
3942

4043
public fun <T> AnyRow.getValueOrNull(columnName: String): T? = getOrNull(columnName) as T?
4144
public fun <T> AnyRow.getValueOrNull(column: KProperty<T>): T? = getValueOrNull<T>(column.columnName)
4245

46+
// endregion
47+
48+
// region contains
49+
50+
public fun AnyRow.containsKey(columnName: String): Boolean = owner.containsColumn(columnName)
51+
public fun AnyRow.containsKey(column: Column): Boolean = owner.containsColumn(column)
52+
public fun AnyRow.containsKey(column: KProperty<*>): Boolean = owner.containsColumn(column)
53+
54+
public operator fun AnyRow.contains(column: Column): Boolean = containsKey(column)
55+
public operator fun AnyRow.contains(column: KProperty<*>): Boolean = containsKey(column)
56+
57+
// endregion
58+
4359
@OptIn(ExperimentalTypeInference::class)
4460
@OverloadResolutionByLambdaReturnType
4561
public fun <T> DataRow<T>.diff(expression: RowExpression<T, Double>): Double? = prev()?.let { p -> expression(this, this) - expression(p, p) }

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/DataRowImpl.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ internal open class DataRowImpl<T>(private val index: Int, private val df: DataF
5454
ColumnAccessTracker.registerColumnAccess(name)
5555
return df.getColumnOrNull(name)?.get(index)
5656
}
57-
58-
override fun containsKey(name: String): Boolean = df.containsColumn(name)
5957
}
6058

6159
internal val <T> DataRow<T>.owner: DataFrame<T> get() = df()

src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/contains.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ class ContainsTests {
4141
val df = dataFrameOf("a")(1, 2).group("a").into("g")
4242
(a in df) shouldBe true
4343
}
44+
45+
@Test
46+
fun `row contains key`() {
47+
val a by column<Int>()
48+
val b by column<Int>()
49+
data class A(val a: Int, val b: Int)
50+
51+
val df = dataFrameOf("a")(1, 2)
52+
val row = df[0]
53+
54+
row.containsKey("a") shouldBe true
55+
row.containsKey(a) shouldBe true
56+
row.containsKey(A::a) shouldBe true
57+
(A::a in row) shouldBe true
58+
(a in row) shouldBe true
59+
60+
row.containsKey("b") shouldBe false
61+
row.containsKey(b) shouldBe false
62+
row.containsKey(A::b) shouldBe false
63+
}
4464
}

0 commit comments

Comments
 (0)