WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit cd8d59e

Browse files
Merge pull request #1601 from Kotlin/optin-experimental-instant
Optin experimental Instant in codegen if this type is used
2 parents 6b4877f + 222ccf9 commit cd8d59e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ import org.jetbrains.kotlinx.dataframe.codeGen.Marker
1515
import org.jetbrains.kotlinx.dataframe.codeGen.MarkerVisibility
1616
import org.jetbrains.kotlinx.dataframe.codeGen.MarkersExtractor
1717
import org.jetbrains.kotlinx.dataframe.codeGen.TypeCastGenerator
18+
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
1819
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
1920
import kotlin.reflect.KClass
2021
import kotlin.reflect.KProperty
2122
import kotlin.reflect.KType
2223
import kotlin.reflect.KVisibility
2324
import kotlin.reflect.full.findAnnotation
25+
import kotlin.reflect.full.isSubtypeOf
2426
import kotlin.reflect.full.superclasses
2527
import kotlin.reflect.jvm.jvmErasure
28+
import kotlin.reflect.typeOf
29+
import kotlin.time.Instant
2630

2731
internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
2832

@@ -116,9 +120,24 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
116120
result.newMarkers.forEach {
117121
generatedMarkers[it.name] = it
118122
}
119-
return result.code
123+
return if (schema.hasExperimentalInstant()) {
124+
result.code.copy(
125+
declarations = "@file:OptIn(kotlin.time.ExperimentalTime::class)\n" + result.code.declarations,
126+
)
127+
} else {
128+
result.code
129+
}
120130
}
121131

132+
private fun DataFrameSchema.hasExperimentalInstant(): Boolean =
133+
columns.values.any { column ->
134+
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?>())
138+
}
139+
}
140+
122141
override fun process(markerClass: KClass<*>): Code {
123142
val newMarkers = mutableListOf<Marker>()
124143

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ class JupyterCodegenTests : JupyterReplTestCase() {
3333
res2["value"].type shouldBe typeOf<List<Any?>>()
3434
}
3535

36+
@Test
37+
fun `opt in experimental Instant`() {
38+
@Language("kts")
39+
val res1 = execRaw(
40+
"""
41+
@file:OptIn(ExperimentalTime::class)
42+
43+
import kotlin.time.ExperimentalTime
44+
45+
val values: kotlin.time.Instant = kotlin.time.Clock.System.now()
46+
val df = dataFrameOf("a" to columnOf(values))
47+
df
48+
""".trimIndent(),
49+
)
50+
51+
res1.shouldBeInstanceOf<AnyFrame>()
52+
}
53+
3654
@Test
3755
fun `Don't inherit from data class`() {
3856
@Language("kts")

0 commit comments

Comments
 (0)