Skip to content

Commit f34dadf

Browse files
Automated commit of generated code
1 parent 505807e commit f34dadf

File tree

3 files changed

+52
-9
lines changed
  • core/generated-sources/src

3 files changed

+52
-9
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,23 +387,43 @@ internal fun KCallable<*>.isGetterLike(): Boolean =
387387
else -> false
388388
}
389389

390+
/** Returns the getter name for this callable.
391+
* The name of the callable is returned with proper getter-trimming if it's a [KFunction]. */
392+
internal val KFunction<*>.getterName: String
393+
get() = name
394+
.removePrefix("get")
395+
.removePrefix("is")
396+
.replaceFirstChar { it.lowercase() }
397+
398+
/** Returns the getter name for this callable.
399+
* The name of the callable is returned with proper getter-trimming if it's a [KFunction]. */
400+
internal val KProperty<*>.getterName: String
401+
get() = name
402+
403+
/**
404+
* Returns the getter name for this callable.
405+
* The name of the callable is returned with proper getter-trimming if it's a [KFunction].
406+
*/
407+
internal val KCallable<*>.getterName: String
408+
get() = when (this) {
409+
is KFunction<*> -> getterName
410+
is KProperty<*> -> getterName
411+
else -> name
412+
}
413+
390414
/** Returns the column name for this callable.
391415
* If the callable contains the [ColumnName][org.jetbrains.kotlinx.dataframe.annotations.ColumnName] annotation, its [ColumnName.name][org.jetbrains.kotlinx.dataframe.annotations.ColumnName.name] is returned.
392416
* Otherwise, the name of the callable is returned with proper getter-trimming if it's a [KFunction]. */
393417
@PublishedApi
394418
internal val KFunction<*>.columnName: String
395-
get() = findAnnotation<ColumnName>()?.name
396-
?: name
397-
.removePrefix("get")
398-
.removePrefix("is")
399-
.replaceFirstChar { it.lowercase() }
419+
get() = findAnnotation<ColumnName>()?.name ?: getterName
400420

401421
/** Returns the column name for this callable.
402422
* If the callable contains the [ColumnName][org.jetbrains.kotlinx.dataframe.annotations.ColumnName] annotation, its [ColumnName.name][org.jetbrains.kotlinx.dataframe.annotations.ColumnName.name] is returned.
403423
* Otherwise, the name of the callable is returned with proper getter-trimming if it's a [KFunction]. */
404424
@PublishedApi
405425
internal val KProperty<*>.columnName: String
406-
get() = findAnnotation<ColumnName>()?.name ?: name
426+
get() = findAnnotation<ColumnName>()?.name ?: getterName
407427

408428
/**
409429
* Returns the column name for this callable.
@@ -415,5 +435,5 @@ internal val KCallable<*>.columnName: String
415435
get() = when (this) {
416436
is KFunction<*> -> columnName
417437
is KProperty<*> -> columnName
418-
else -> findAnnotation<ColumnName>()?.name ?: name
438+
else -> findAnnotation<ColumnName>()?.name ?: getterName
419439
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
1515
import org.jetbrains.kotlinx.dataframe.hasNulls
1616
import org.jetbrains.kotlinx.dataframe.impl.columnName
1717
import org.jetbrains.kotlinx.dataframe.impl.commonType
18+
import org.jetbrains.kotlinx.dataframe.impl.getterName
1819
import org.jetbrains.kotlinx.dataframe.impl.isGetterLike
1920
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
2021
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
@@ -192,8 +193,8 @@ internal fun <T> Iterable<KCallable<T>>.sortWithConstructor(klass: KClass<*>): L
192193
// else sort the ones in the primary constructor first according to the order in there
193194
// leave the rest at the end in lexicographical order
194195
val (propsInConstructor, propsNotInConstructor) =
195-
lexicographicalColumns.partition { it.columnName in primaryConstructorOrder.keys }
196+
lexicographicalColumns.partition { it.getterName in primaryConstructorOrder.keys }
196197

197198
return propsInConstructor
198-
.sortedBy { primaryConstructorOrder[it.columnName] } + propsNotInConstructor
199+
.sortedBy { primaryConstructorOrder[it.getterName] } + propsNotInConstructor
199200
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.kotest.matchers.shouldBe
55
import org.jetbrains.kotlinx.dataframe.DataColumn
66
import org.jetbrains.kotlinx.dataframe.DataFrame
77
import org.jetbrains.kotlinx.dataframe.DataRow
8+
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
89
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
910
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
1011
import org.jetbrains.kotlinx.dataframe.kind
@@ -79,14 +80,35 @@ class CreateDataFrameTests {
7980

8081
@Test
8182
fun `preserve fields order`() {
83+
// constructor properties will keep order, so x, c
8284
class B(val x: Int, val c: String, d: Double) {
85+
// then child properties will be sorted lexicographically by column name, so a, b
8386
val b: Int = x
8487
val a: Double = d
8588
}
8689

8790
listOf(B(1, "a", 2.0)).toDataFrame().columnNames() shouldBe listOf("x", "c", "a", "b")
8891
}
8992

93+
@Test
94+
fun `preserve fields order with @ColumnName`() {
95+
// constructor properties will keep order, so z, y
96+
class B(
97+
@ColumnName("z") val x: Int,
98+
@ColumnName("y") val c: String,
99+
d: Double,
100+
) {
101+
// then child properties will be sorted lexicographically by column name, so w, x
102+
@ColumnName("x")
103+
val a: Double = d
104+
105+
@ColumnName("w")
106+
val b: Int = x
107+
}
108+
109+
listOf(B(1, "a", 2.0)).toDataFrame().columnNames() shouldBe listOf("z", "y", "w", "x")
110+
}
111+
90112
@DataSchema
91113
data class A(val v: Int)
92114

0 commit comments

Comments
 (0)