Skip to content

Commit a4cae3c

Browse files
committed
Merging KeyValueProperty and NameValuePair, Issue #659
1 parent 7c189f0 commit a4cae3c

File tree

9 files changed

+75
-20
lines changed

9 files changed

+75
-20
lines changed

core/api/core.api

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,8 @@ public final class org/jetbrains/kotlinx/dataframe/api/DataFrameGetKt {
19481948
}
19491949

19501950
public final class org/jetbrains/kotlinx/dataframe/api/DataRowApiKt {
1951+
public static final fun NameValuePairAny_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
1952+
public static final fun NameValuePairAny_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
19511953
public static final fun NameValuePairAny_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
19521954
public static final fun NameValuePairAny_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
19531955
public static final fun NameValuePairAny_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
@@ -1960,6 +1962,8 @@ public final class org/jetbrains/kotlinx/dataframe/api/DataRowApiKt {
19601962
public static final fun containsKey (Lorg/jetbrains/kotlinx/dataframe/DataRow;Ljava/lang/String;)Z
19611963
public static final fun containsKey (Lorg/jetbrains/kotlinx/dataframe/DataRow;Lkotlin/reflect/KProperty;)Z
19621964
public static final fun containsKey (Lorg/jetbrains/kotlinx/dataframe/DataRow;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Z
1965+
public static final fun copy (Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;Ljava/lang/String;Ljava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
1966+
public static synthetic fun copy$default (Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;Ljava/lang/String;Ljava/lang/Object;ILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
19631967
public static final fun diff (Lorg/jetbrains/kotlinx/dataframe/DataRow;DLkotlin/jvm/functions/Function2;)D
19641968
public static final fun diff (Lorg/jetbrains/kotlinx/dataframe/DataRow;FLkotlin/jvm/functions/Function2;)F
19651969
public static final fun diff (Lorg/jetbrains/kotlinx/dataframe/DataRow;ILkotlin/jvm/functions/Function2;)I
@@ -1968,6 +1972,7 @@ public final class org/jetbrains/kotlinx/dataframe/api/DataRowApiKt {
19681972
public static final fun diffOrNull (Lorg/jetbrains/kotlinx/dataframe/DataRow;Lkotlin/jvm/functions/Function2;)Ljava/lang/Float;
19691973
public static final fun diffOrNull (Lorg/jetbrains/kotlinx/dataframe/DataRow;Lkotlin/jvm/functions/Function2;)Ljava/lang/Integer;
19701974
public static final fun diffOrNull (Lorg/jetbrains/kotlinx/dataframe/DataRow;Lkotlin/jvm/functions/Function2;)Ljava/lang/Long;
1975+
public static final fun getName (Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;)Ljava/lang/String;
19711976
public static final fun getRow (Lorg/jetbrains/kotlinx/dataframe/DataRow;I)Lorg/jetbrains/kotlinx/dataframe/DataRow;
19721977
public static final fun getRowOrNull (Lorg/jetbrains/kotlinx/dataframe/DataRow;I)Lorg/jetbrains/kotlinx/dataframe/DataRow;
19731978
public static final fun getRows (Lorg/jetbrains/kotlinx/dataframe/DataRow;Ljava/lang/Iterable;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
@@ -3466,26 +3471,31 @@ public final class org/jetbrains/kotlinx/dataframe/api/MoveKt {
34663471
public static final fun under (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnAccessor;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34673472
}
34683473

3469-
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair {
3474+
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair : org/jetbrains/kotlinx/dataframe/api/KeyValueProperty {
3475+
public static final field Companion Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair$Companion;
34703476
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
34713477
public final fun component1 ()Ljava/lang/String;
34723478
public final fun component2 ()Ljava/lang/Object;
34733479
public final fun copy (Ljava/lang/String;Ljava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
34743480
public static synthetic fun copy$default (Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;Ljava/lang/String;Ljava/lang/Object;ILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
34753481
public fun equals (Ljava/lang/Object;)Z
3476-
public final fun getName ()Ljava/lang/String;
3477-
public final fun getValue ()Ljava/lang/Object;
3482+
public fun getKey ()Ljava/lang/String;
3483+
public fun getValue ()Ljava/lang/Object;
34783484
public fun hashCode ()I
34793485
public fun toString ()Ljava/lang/String;
34803486
}
34813487

3488+
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair$Companion {
3489+
public final fun invoke (Ljava/lang/String;Ljava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
3490+
}
3491+
34823492
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair_ExtensionsKt {
3483-
public static final fun NameValuePair_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3484-
public static final fun NameValuePair_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3493+
public static final fun NameValuePair_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3494+
public static final fun NameValuePair_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
34853495
public static final fun NameValuePair_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
34863496
public static final fun NameValuePair_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
3487-
public static final fun NullableNameValuePair_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3488-
public static final fun NullableNameValuePair_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3497+
public static final fun NullableNameValuePair_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3498+
public static final fun NullableNameValuePair_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
34893499
public static final fun NullableNameValuePair_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
34903500
public static final fun NullableNameValuePair_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
34913501
}

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.jetbrains.kotlinx.dataframe.util.GET_ROW_REPLACE
2525
import org.jetbrains.kotlinx.dataframe.util.IS_EMPTY_REPLACE
2626
import org.jetbrains.kotlinx.dataframe.util.IS_NOT_EMPTY_REPLACE
2727
import org.jetbrains.kotlinx.dataframe.util.MESSAGE_SHORTCUT
28+
import org.jetbrains.kotlinx.dataframe.util.NAME_VALUE_PAIR
2829
import kotlin.experimental.ExperimentalTypeInference
2930
import kotlin.reflect.KProperty
3031
import kotlin.reflect.KType
@@ -39,18 +40,52 @@ public fun AnyRow.isNotEmpty(): Boolean = !isEmpty()
3940
public inline fun <reified R> AnyRow.valuesOf(): List<R> = values().filterIsInstance<R>()
4041

4142
// region DataSchema
43+
44+
/**
45+
* Instantiatable [KeyValueProperty] representing a key-value pair [DataSchema] for a [DataFrame].
46+
*
47+
* [NameValuePair] may be deprecated in favor of an instantiatable [KeyValueProperty] class in the future.
48+
*
49+
* @param V type of the value
50+
* @param key the name of the key column (previously called `name`)
51+
* @param value the name of the value column
52+
*/
4253
@DataSchema
4354
@RequiredByIntellijPlugin
44-
public data class NameValuePair<V>(val name: String, val value: V)
55+
public data class NameValuePair<V>(override val key: String, override val value: V) : KeyValueProperty<V> {
56+
public companion object {
57+
@Deprecated(NAME_VALUE_PAIR, level = DeprecationLevel.WARNING)
58+
public operator fun <V> invoke(name: String, value: V): NameValuePair<V> = NameValuePair(name, value)
59+
}
60+
}
61+
62+
@Deprecated(NAME_VALUE_PAIR, ReplaceWith("key"), level = DeprecationLevel.WARNING)
63+
public val NameValuePair<*>.name: String
64+
get() = key
65+
66+
@Deprecated(NAME_VALUE_PAIR, ReplaceWith("this.copy(name, value)"), level = DeprecationLevel.WARNING)
67+
public fun <V> NameValuePair<V>.copy(name: String = this.key, value: V = this.value): NameValuePair<V> =
68+
NameValuePair(key = name, value = value)
4569

4670
// Without these overloads row.transpose().name or row.map { name } won't resolve
71+
72+
@Deprecated(NAME_VALUE_PAIR, ReplaceWith("this.key"), level = DeprecationLevel.WARNING)
4773
public val ColumnsContainer<NameValuePair<*>>.name: DataColumn<String>
4874
@JvmName("NameValuePairAny_name")
49-
get() = this["name"] as DataColumn<String>
75+
get() = this["key"] as DataColumn<String>
5076

77+
@Deprecated(NAME_VALUE_PAIR, ReplaceWith("this.key"), level = DeprecationLevel.WARNING)
5178
public val DataRow<NameValuePair<*>>.name: String
5279
@JvmName("NameValuePairAny_name")
53-
get() = this["name"] as String
80+
get() = this["key"] as String
81+
82+
public val ColumnsContainer<NameValuePair<*>>.key: DataColumn<String>
83+
@JvmName("NameValuePairAny_key")
84+
get() = this["key"] as DataColumn<String>
85+
86+
public val DataRow<NameValuePair<*>>.key: String
87+
@JvmName("NameValuePairAny_key")
88+
get() = this["key"] as String
5489

5590
public val ColumnsContainer<NameValuePair<*>>.value: DataColumn<*>
5691
@JvmName("NameValuePairAny_value")

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/KeyValueProperty.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package org.jetbrains.kotlinx.dataframe.api
33
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
44
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
55

6-
/** A [DataSchema] interface / class can implement this if it represents a map-like data schema (so key: value). */
6+
/**
7+
* A [DataSchema] interface / class can implement this if it represents a map-like data schema (so key: value).
8+
* @see [NameValuePair]
9+
*/
710
@DataSchema
811
public interface KeyValueProperty<T> {
912
// needs to be explicitly overridden in @DataSchema interface, otherwise extension functions won't generate (TODO)

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/transpose.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import kotlin.reflect.typeOf
1414
// region DataRow
1515

1616
public fun <T> DataRow<T>.transpose(): DataFrame<NameValuePair<*>> {
17-
val valueColumn = DataColumn.createByInference(NameValuePair<*>::value.columnName, values)
18-
val nameColumn = owner.columnNames().toValueColumn(NameValuePair<*>::name.name)
17+
val valueColumn = DataColumn.createByInference(KeyValueProperty<*>::value.columnName, values)
18+
val nameColumn = owner.columnNames().toValueColumn(KeyValueProperty<*>::key.name)
1919
return dataFrameOf(nameColumn, valueColumn).cast()
2020
}
2121

@@ -24,8 +24,8 @@ public inline fun <reified T> AnyRow.transposeTo(): DataFrame<NameValuePair<T>>
2424
@PublishedApi
2525
internal fun <T> AnyRow.transposeTo(type: KType): DataFrame<NameValuePair<T>> {
2626
val convertedValues = values.map { it?.convertTo(type) as T? }
27-
val valueColumn = DataColumn.createByInference(NameValuePair<T>::value.columnName, convertedValues)
28-
val nameColumn = owner.columnNames().toValueColumn(NameValuePair<T>::name.name)
27+
val valueColumn = DataColumn.createByInference(KeyValueProperty<T>::value.columnName, convertedValues)
28+
val nameColumn = owner.columnNames().toValueColumn(KeyValueProperty<T>::key.name)
2929
return dataFrameOf(nameColumn, valueColumn).cast()
3030
}
3131

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ internal const val COPY_REPLACE = "columns().toDataFrame().cast()"
263263
internal const val LISTS_TO_DATAFRAME_MIGRATION =
264264
"Function moved from io to api package, and a new `header` parameter is introduced. $MESSAGE_1_1"
265265

266+
internal const val NAME_VALUE_PAIR =
267+
"'name' of NameValuePair will be renamed to 'key' to align with KeyValueProperty, Issue #659. $MESSAGE_1_1"
268+
266269
// endregion
267270

268271
// region keep across releases

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/puzzles/MediumTests.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.jetbrains.kotlinx.dataframe.api.diffOrNull
1212
import org.jetbrains.kotlinx.dataframe.api.filter
1313
import org.jetbrains.kotlinx.dataframe.api.groupBy
1414
import org.jetbrains.kotlinx.dataframe.api.isNaN
15+
import org.jetbrains.kotlinx.dataframe.api.key
1516
import org.jetbrains.kotlinx.dataframe.api.map
1617
import org.jetbrains.kotlinx.dataframe.api.mapToColumn
1718
import org.jetbrains.kotlinx.dataframe.api.minBy
@@ -78,8 +79,8 @@ class MediumTests {
7879
.sum()
7980
.transposeTo<Double>()
8081
.minBy { value }
81-
.name shouldBe "b"
82-
df.sum().transpose().minBy("value")["name"] shouldBe "b"
82+
.key shouldBe "b"
83+
df.sum().transpose().minBy("value")["key"] shouldBe "b"
8384
}
8485

8586
@Test

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/animals/AnimalsTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.kotest.matchers.doubles.shouldBeNaN
44
import io.kotest.matchers.shouldBe
55
import org.jetbrains.kotlinx.dataframe.api.columnOf
66
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
7+
import org.jetbrains.kotlinx.dataframe.api.key
78
import org.jetbrains.kotlinx.dataframe.api.mean
89
import org.jetbrains.kotlinx.dataframe.api.name
910
import org.jetbrains.kotlinx.dataframe.api.transpose
@@ -33,7 +34,7 @@ class AnimalsTests {
3334
val mean = df.mean().transpose()
3435
mean.columnsCount() shouldBe 2
3536
mean.rowsCount() shouldBe 2
36-
mean.name.values() shouldBe listOf("age", "visits")
37+
mean.key.values() shouldBe listOf("age", "visits")
3738
mean.value.type() shouldBe typeOf<Double>()
3839
}
3940

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import org.jetbrains.kotlinx.dataframe.api.isEmpty
8585
import org.jetbrains.kotlinx.dataframe.api.isFrameColumn
8686
import org.jetbrains.kotlinx.dataframe.api.isNA
8787
import org.jetbrains.kotlinx.dataframe.api.isNumber
88+
import org.jetbrains.kotlinx.dataframe.api.key
8889
import org.jetbrains.kotlinx.dataframe.api.keysInto
8990
import org.jetbrains.kotlinx.dataframe.api.last
9091
import org.jetbrains.kotlinx.dataframe.api.leftJoin
@@ -2601,7 +2602,7 @@ class DataFrameTests : BaseTest() {
26012602
typed[2]
26022603
.transpose()
26032604
.dropNulls { value }
2604-
.name
2605+
.key
26052606
.toList() shouldBe listOf("name", "age", "city")
26062607
}
26072608

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataRowTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.jetbrains.kotlinx.dataframe.api.drop
1010
import org.jetbrains.kotlinx.dataframe.api.dropLast
1111
import org.jetbrains.kotlinx.dataframe.api.first
1212
import org.jetbrains.kotlinx.dataframe.api.intoList
13+
import org.jetbrains.kotlinx.dataframe.api.key
1314
import org.jetbrains.kotlinx.dataframe.api.mapToColumn
1415
import org.jetbrains.kotlinx.dataframe.api.merge
1516
import org.jetbrains.kotlinx.dataframe.api.name
@@ -109,7 +110,7 @@ class DataRowTests : BaseTest() {
109110
@Test
110111
fun transposeTo() {
111112
val df = dataFrameOf("a", "b")(1, 2).first().transposeTo<Int>()
112-
df.name.toList() shouldBe listOf("a", "b")
113+
df.key.toList() shouldBe listOf("a", "b")
113114
df.value.toList() shouldBe listOf(1, 2)
114115
}
115116

0 commit comments

Comments
 (0)