Skip to content

Commit 17e823f

Browse files
authored
Merge pull request #1545 from Kotlin/NameValueProperty
`NameValueProperty`
2 parents df08715 + dc40cb3 commit 17e823f

File tree

17 files changed

+272
-109
lines changed

17 files changed

+272
-109
lines changed

core/api/core.api

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,17 +2739,6 @@ public abstract interface class org/jetbrains/kotlinx/dataframe/api/KeyValueProp
27392739
public abstract fun getValue ()Ljava/lang/Object;
27402740
}
27412741

2742-
public final class org/jetbrains/kotlinx/dataframe/api/KeyValueProperty_ExtensionsKt {
2743-
public static final fun KeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
2744-
public static final fun KeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
2745-
public static final fun KeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
2746-
public static final fun KeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
2747-
public static final fun NullableKeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
2748-
public static final fun NullableKeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
2749-
public static final fun NullableKeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
2750-
public static final fun NullableKeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
2751-
}
2752-
27532742
public abstract interface class org/jetbrains/kotlinx/dataframe/api/LastColumnsSelectionDsl {
27542743
public fun last (Lorg/jetbrains/kotlinx/dataframe/api/ColumnsSelectionDsl;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlinx/dataframe/columns/SingleColumn;
27552744
public fun last (Lorg/jetbrains/kotlinx/dataframe/columns/ColumnSet;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlinx/dataframe/columns/SingleColumn;
@@ -3466,15 +3455,15 @@ public final class org/jetbrains/kotlinx/dataframe/api/MoveKt {
34663455
public static final fun under (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnAccessor;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34673456
}
34683457

3469-
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair {
3458+
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair : org/jetbrains/kotlinx/dataframe/api/NameValueProperty {
34703459
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
34713460
public final fun component1 ()Ljava/lang/String;
34723461
public final fun component2 ()Ljava/lang/Object;
34733462
public final fun copy (Ljava/lang/String;Ljava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/api/NameValuePair;
34743463
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;
34753464
public fun equals (Ljava/lang/Object;)Z
3476-
public final fun getName ()Ljava/lang/String;
3477-
public final fun getValue ()Ljava/lang/Object;
3465+
public fun getName ()Ljava/lang/String;
3466+
public fun getValue ()Ljava/lang/Object;
34783467
public fun hashCode ()I
34793468
public fun toString ()Ljava/lang/String;
34803469
}
@@ -3490,6 +3479,37 @@ public final class org/jetbrains/kotlinx/dataframe/api/NameValuePair_ExtensionsK
34903479
public static final fun NullableNameValuePair_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
34913480
}
34923481

3482+
public abstract interface class org/jetbrains/kotlinx/dataframe/api/NameValueProperty {
3483+
public abstract fun getName ()Ljava/lang/String;
3484+
public abstract fun getValue ()Ljava/lang/Object;
3485+
}
3486+
3487+
public final class org/jetbrains/kotlinx/dataframe/api/NameValuePropertyKt {
3488+
public static final fun KeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3489+
public static final fun KeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3490+
public static final fun KeyValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3491+
public static final fun KeyValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3492+
public static final fun KeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3493+
public static final fun KeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
3494+
public static final fun NullableKeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3495+
public static final fun NullableKeyValueProperty_key (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3496+
public static final fun NullableKeyValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3497+
public static final fun NullableKeyValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3498+
public static final fun NullableKeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3499+
public static final fun NullableKeyValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
3500+
}
3501+
3502+
public final class org/jetbrains/kotlinx/dataframe/api/NameValueProperty_ExtensionsKt {
3503+
public static final fun NameValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3504+
public static final fun NameValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3505+
public static final fun NameValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3506+
public static final fun NameValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
3507+
public static final fun NullableNameValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3508+
public static final fun NullableNameValueProperty_name (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
3509+
public static final fun NullableNameValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/ColumnsScope;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
3510+
public static final fun NullableNameValueProperty_value (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
3511+
}
3512+
34933513
public abstract interface class org/jetbrains/kotlinx/dataframe/api/NoneColumnsSelectionDsl {
34943514
public fun none ()Lorg/jetbrains/kotlinx/dataframe/columns/ColumnsResolver;
34953515
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/annotations/ImportDataSchema.kt

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

33
import org.jetbrains.kotlinx.dataframe.api.JsonPath
4-
import org.jetbrains.kotlinx.dataframe.api.KeyValueProperty
4+
import org.jetbrains.kotlinx.dataframe.api.NameValueProperty
55
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
66
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
77
import org.jetbrains.kotlinx.dataframe.documentation.UnifyingNumbers
@@ -78,7 +78,7 @@ public annotation class JsonOptions(
7878
* */
7979
public val typeClashTactic: String = TypeClashTactics.ARRAY_AND_VALUE_COLUMNS,
8080
/**
81-
* List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[KeyValueProperty]>
81+
* List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[NameValueProperty]>
8282
* will be created.
8383
*
8484
* Example:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public inline fun <reified R> AnyRow.valuesOf(): List<R> = values().filterIsInst
4141
// region DataSchema
4242
@DataSchema
4343
@RequiredByIntellijPlugin
44-
public data class NameValuePair<V>(val name: String, val value: V)
44+
public data class NameValuePair<V>(override val name: String, override val value: V) : NameValueProperty<V>
4545

4646
// Without these overloads row.transpose().name or row.map { name } won't resolve
4747
public val ColumnsContainer<NameValuePair<*>>.name: DataColumn<String>

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

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
@file:Suppress("UNCHECKED_CAST", "DEPRECATION")
2+
3+
package org.jetbrains.kotlinx.dataframe.api
4+
5+
import org.jetbrains.kotlinx.dataframe.ColumnsScope
6+
import org.jetbrains.kotlinx.dataframe.DataColumn
7+
import org.jetbrains.kotlinx.dataframe.DataRow
8+
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
9+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
10+
import org.jetbrains.kotlinx.dataframe.util.KEY_VALUE_PROPERTY
11+
import org.jetbrains.kotlinx.dataframe.util.KEY_VALUE_PROPERTY_KEY
12+
13+
/** A [DataSchema] interface / class can implement this if it represents a map-like data schema (so name: value). */
14+
@DataSchema
15+
public interface NameValueProperty<T> {
16+
// needs to be explicitly overridden in @DataSchema interface, otherwise extension functions won't generate (TODO)
17+
public val name: String
18+
19+
// needs to be explicitly overridden in @DataSchema interface, otherwise type will be read as `T` and extensions won't generate (TODO)
20+
@ColumnName("value")
21+
public val `value`: T
22+
}
23+
24+
// region Deprecated
25+
26+
/** A [DataSchema] interface / class can implement this if it represents a map-like data schema (so key: value). */
27+
@Deprecated(KEY_VALUE_PROPERTY, ReplaceWith("NameValueProperty<T>"))
28+
public interface KeyValueProperty<T> {
29+
// needs to be explicitly overridden in @DataSchema interface, otherwise extension functions won't generate (TODO)
30+
public val key: String
31+
32+
// needs to be explicitly overridden in @DataSchema interface, otherwise type will be read as `T` and extensions won't generate (TODO)
33+
@ColumnName("value")
34+
public val `value`: T
35+
}
36+
37+
@Deprecated(KEY_VALUE_PROPERTY_KEY, ReplaceWith("name"))
38+
public val ColumnsScope<KeyValueProperty<*>>.key: DataColumn<String>
39+
@JvmName("KeyValueProperty_key")
40+
get() = this["key"].cast()
41+
42+
@Deprecated(KEY_VALUE_PROPERTY_KEY, ReplaceWith("name"))
43+
public val ColumnsScope<KeyValueProperty<*>?>.key: DataColumn<String?>
44+
@JvmName("NullableKeyValueProperty_key")
45+
get() = this["key"].cast()
46+
47+
@Deprecated(KEY_VALUE_PROPERTY_KEY, ReplaceWith("name"))
48+
public val DataRow<KeyValueProperty<*>>.key: String
49+
@JvmName("KeyValueProperty_key")
50+
get() = this["key"] as String
51+
52+
@Deprecated(KEY_VALUE_PROPERTY_KEY, ReplaceWith("name"))
53+
public val DataRow<KeyValueProperty<*>?>.key: String?
54+
@JvmName("NullableKeyValueProperty_key")
55+
get() = this["key"] as String?
56+
57+
/**
58+
* Accesses the 'key' column of this [KeyValueProperty][KeyValueProperty]
59+
* [ColumnsContainer][org.jetbrains.kotlinx.dataframe.ColumnsContainer].
60+
*
61+
* This is a temporary, future-proof, extension property in preparation of the migration from [KeyValueProperty]
62+
* to [NameValueProperty].
63+
*/
64+
public val ColumnsScope<KeyValueProperty<*>>.name: DataColumn<String>
65+
@JvmName("KeyValueProperty_name")
66+
get() = key
67+
68+
/**
69+
* Accesses the 'key' column of this [KeyValueProperty][KeyValueProperty]
70+
* [ColumnsContainer][org.jetbrains.kotlinx.dataframe.ColumnsContainer].
71+
*
72+
* This is a temporary, future-proof, extension property in preparation of the migration from [KeyValueProperty]
73+
* to [NameValueProperty].
74+
*/
75+
public val ColumnsScope<KeyValueProperty<*>?>.name: DataColumn<String?>
76+
@JvmName("NullableKeyValueProperty_name")
77+
get() = key
78+
79+
/**
80+
* Accesses the 'key' value of this [KeyValueProperty][KeyValueProperty]
81+
* [DataRow][DataRow].
82+
*
83+
* This is a temporary, future-proof, extension property in preparation of the migration from [KeyValueProperty]
84+
* to [NameValueProperty].
85+
*/
86+
public val DataRow<KeyValueProperty<*>>.name: String
87+
@JvmName("KeyValueProperty_name")
88+
get() = key
89+
90+
/**
91+
* Accesses the 'key' value of this [KeyValueProperty][KeyValueProperty]
92+
* [DataRow][DataRow].
93+
*
94+
* This is a temporary, future-proof, extension property in preparation of the migration from [KeyValueProperty]
95+
* to [NameValueProperty].
96+
*/
97+
public val DataRow<KeyValueProperty<*>?>.name: String?
98+
@JvmName("NullableKeyValueProperty_name")
99+
get() = key
100+
101+
public val <T : Any?> ColumnsScope<KeyValueProperty<T>>.value: DataColumn<T>
102+
@JvmName("KeyValueProperty_value")
103+
get() = this["value"].cast()
104+
105+
public val <T : Any?> ColumnsScope<KeyValueProperty<T>?>.value: DataColumn<T?>
106+
@JvmName("NullableKeyValueProperty_value")
107+
get() = this["value"].cast()
108+
109+
public val <T : Any?> DataRow<KeyValueProperty<T>>.value: T
110+
@JvmName("KeyValueProperty_value")
111+
get() = this["value"] as T
112+
113+
public val <T : Any?> DataRow<KeyValueProperty<T>?>.value: T?
114+
@JvmName("NullableKeyValueProperty_value")
115+
get() = this["value"] as T?
116+
117+
// endregion

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ 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 KEY_VALUE_PROPERTY = "Deprecated in favor of NameValueProperty. $MESSAGE_1_1"
267+
internal const val KEY_VALUE_PROPERTY_KEY =
268+
"This column will be renamed to 'name' when KeyValueProperty will be replaced with NameValueProperty. $MESSAGE_1_1"
269+
266270
// endregion
267271

268272
// region keep across releases

dataframe-json/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/readJson.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.jetbrains.kotlinx.dataframe.DataColumn
2121
import org.jetbrains.kotlinx.dataframe.DataFrame
2222
import org.jetbrains.kotlinx.dataframe.DataRow
2323
import org.jetbrains.kotlinx.dataframe.api.JsonPath
24-
import org.jetbrains.kotlinx.dataframe.api.KeyValueProperty
24+
import org.jetbrains.kotlinx.dataframe.api.NameValueProperty
2525
import org.jetbrains.kotlinx.dataframe.api.cast
2626
import org.jetbrains.kotlinx.dataframe.api.chunked
2727
import org.jetbrains.kotlinx.dataframe.api.columnOf
@@ -67,7 +67,7 @@ private enum class AnyColType {
6767
OBJECTS,
6868
}
6969

70-
internal interface AnyKeyValueProperty : KeyValueProperty<Any?> {
70+
internal interface AnyNameValueProperty : NameValueProperty<Any?> {
7171
override val value: Any?
7272
}
7373

@@ -122,7 +122,7 @@ internal fun readJsonImpl(
122122
*
123123
* @param records List of json elements to be converted to a [DataFrame].
124124
* @param unifyNumbers Whether to [unify the numbers that are read][UnifyingNumbers].
125-
* @param keyValuePaths List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[KeyValueProperty]>
125+
* @param keyValuePaths List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[NameValueProperty]>
126126
* will be created.
127127
* @param header Optional list of column names. If given, [records] will be read like an object with [header] being the keys.
128128
* @return [DataFrame] from the given [records].
@@ -340,22 +340,22 @@ internal fun fromJsonListAnyColumns(
340340
valueTypes += valueType
341341

342342
dataFrameOf(
343-
columnOf(*map.keys.toTypedArray()).named(KeyValueProperty<*>::key.name),
343+
columnOf(*map.keys.toTypedArray()).named(NameValueProperty<*>::name.name),
344344
createColumnGuessingType(
345345
values = map.values,
346346
suggestedType = TypeSuggestion.Use(valueType),
347347
unifyNumbers = unifyNumbers,
348-
).named(KeyValueProperty<*>::value.name),
348+
).named(NameValueProperty<*>::value.name),
349349
)
350350
}
351351

352-
is JsonNull, null -> DataFrame.emptyOf<AnyKeyValueProperty>()
352+
is JsonNull, null -> DataFrame.emptyOf<AnyNameValueProperty>()
353353

354354
else -> error("Expected JsonObject, got $record")
355355
}
356356
}
357357

358-
val valueColumns = dataFrames.map { it[KeyValueProperty<*>::value.name] }
358+
val valueColumns = dataFrames.map { it[NameValueProperty<*>::value.name] }
359359
val valueColumnSchema = when {
360360
// in these cases we can safely combine the columns to get a single column schema
361361
valueColumns.all { it is ColumnGroup<*> } || valueColumns.all { it is FrameColumn<*> } ->
@@ -373,8 +373,8 @@ internal fun fromJsonListAnyColumns(
373373
schema = lazy {
374374
DataFrameSchemaImpl(
375375
columns = mapOf(
376-
KeyValueProperty<*>::key.name to ColumnSchema.Value(typeOf<String>()),
377-
KeyValueProperty<*>::value.name to valueColumnSchema,
376+
NameValueProperty<*>::name.name to ColumnSchema.Value(typeOf<String>()),
377+
NameValueProperty<*>::value.name to valueColumnSchema,
378378
),
379379
)
380380
},
@@ -443,7 +443,7 @@ private fun AnyFrame.isSingleUnnamedColumn() = columnsCount() == 1 && getColumn(
443443
*
444444
* @param records List of json elements to be converted to a [DataFrame].
445445
* @param unifyNumbers Whether to [unify the numbers that are read][UnifyingNumbers].
446-
* @param keyValuePaths List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[KeyValueProperty]>
446+
* @param keyValuePaths List of [JsonPath]s where instead of a [ColumnGroup], a [FrameColumn]<[NameValueProperty]>
447447
* will be created.
448448
* @param header Optional list of column names. If given, [records] will be read like an object with [header] being the keys.
449449
* @return [DataFrame] from the given [records].
@@ -527,16 +527,16 @@ internal fun fromJsonListArrayAndValueColumns(
527527
.commonType()
528528

529529
dataFrameOf(
530-
columnOf(*map.keys.toTypedArray()).named(KeyValueProperty<*>::key.name),
530+
columnOf(*map.keys.toTypedArray()).named(NameValueProperty<*>::name.name),
531531
createColumnGuessingType(
532532
values = map.values,
533533
suggestedType = TypeSuggestion.Use(valueType),
534534
unifyNumbers = unifyNumbers,
535-
).named(KeyValueProperty<*>::value.name),
535+
).named(NameValueProperty<*>::value.name),
536536
)
537537
}
538538

539-
is JsonNull, null -> DataFrame.emptyOf<AnyKeyValueProperty>()
539+
is JsonNull, null -> DataFrame.emptyOf<AnyNameValueProperty>()
540540

541541
else -> error("Expected JsonObject, got $record")
542542
}

0 commit comments

Comments
 (0)