Skip to content

Commit 3e6297e

Browse files
committed
Follow up for #1601 which does the same for kotlin.uuid.Uuid
1 parent 3c77605 commit 3e6297e

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ allprojects {
194194
// enables support for kotlin.time.Instant as kotlinx.datetime.Instant was deprecated; Issue #1350
195195
// Can be removed once kotlin.time.Instant is marked "stable".
196196
optIn.add("kotlin.time.ExperimentalTime")
197+
// can be removed once kotlin.uuid.ExperimentalUuidApi is marked "stable".
198+
optIn.add("kotlin.uuid.ExperimentalUuidApi")
197199
}
198200
}
199201

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlin.reflect.full.superclasses
2727
import kotlin.reflect.jvm.jvmErasure
2828
import kotlin.reflect.typeOf
2929
import kotlin.time.Instant
30+
import kotlin.uuid.Uuid
3031

3132
internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
3233

@@ -120,24 +121,34 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
120121
result.newMarkers.forEach {
121122
generatedMarkers[it.name] = it
122123
}
123-
return if (schema.hasExperimentalInstant()) {
124+
125+
val optIns = buildString {
126+
if (schema.hasExperimentalInstant()) appendLine("@file:OptIn(kotlin.time.ExperimentalTime::class)")
127+
if (schema.hasExperimentalUuid()) appendLine("@file:OptIn(kotlin.uuid.ExperimentalUuidApi::class)")
128+
}
129+
130+
return if (optIns.isNotEmpty()) {
124131
result.code.copy(
125-
declarations = "@file:OptIn(kotlin.time.ExperimentalTime::class)\n" + result.code.declarations,
132+
declarations = optIns + "\n" + result.code.declarations,
126133
)
127134
} else {
128135
result.code
129136
}
130137
}
131138

132-
private fun DataFrameSchema.hasExperimentalInstant(): Boolean =
139+
private fun DataFrameSchema.hasColumnOfType(type: KType): Boolean =
133140
columns.values.any { column ->
134141
when (column) {
135-
is ColumnSchema.Frame -> column.schema.hasExperimentalInstant()
136-
is ColumnSchema.Group -> column.schema.hasExperimentalInstant()
137-
is ColumnSchema.Value -> column.type.isSubtypeOf(typeOf<Instant?>())
142+
is ColumnSchema.Frame -> column.schema.hasColumnOfType(type)
143+
is ColumnSchema.Group -> column.schema.hasColumnOfType(type)
144+
is ColumnSchema.Value -> column.type.isSubtypeOf(type)
138145
}
139146
}
140147

148+
private fun DataFrameSchema.hasExperimentalInstant(): Boolean = hasColumnOfType(typeOf<Instant?>())
149+
150+
private fun DataFrameSchema.hasExperimentalUuid(): Boolean = hasColumnOfType(typeOf<Uuid?>())
151+
141152
override fun process(markerClass: KClass<*>): Code {
142153
val newMarkers = mutableListOf<Marker>()
143154

dataframe-jupyter/src/test/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/JupyterCodegenTests.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ class JupyterCodegenTests : JupyterReplTestCase() {
5959
res2.type() shouldBe typeOf<kotlin.time.Instant>()
6060
}
6161

62+
@Test
63+
fun `opt in experimental Uuid`() {
64+
@Language("kts")
65+
val res1 = execRaw(
66+
"""
67+
@file:OptIn(kotlin.uuid.ExperimentalUuidApi::class)
68+
69+
val uuid: kotlin.uuid.Uuid = kotlin.uuid.Uuid.NIL
70+
val df = dataFrameOf("a" to columnOf(uuid))
71+
df
72+
""".trimIndent(),
73+
)
74+
75+
@Language("kts")
76+
val res2 = execRaw(
77+
"""
78+
@file:OptIn(kotlin.uuid.ExperimentalUuidApi::class)
79+
80+
df.a
81+
""".trimIndent(),
82+
)
83+
84+
res2.shouldBeInstanceOf<DataColumn<*>>()
85+
res2.type() shouldBe typeOf<kotlin.uuid.Uuid>()
86+
}
87+
6288
@Test
6389
fun `Don't inherit from data class`() {
6490
@Language("kts")

0 commit comments

Comments
 (0)