Skip to content

Commit 6f3773c

Browse files
authored
Merge pull request #202 from Kotlin/jupyter-codegen-var/val
Jupyter codegen isOpen both for var/val
2 parents b34a541 + a15e1f0 commit 6f3773c

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ import org.jetbrains.kotlinx.dataframe.codeGen.CodeWithConverter
66
import org.jetbrains.kotlinx.dataframe.impl.codeGen.ReplCodeGeneratorImpl
77
import org.jetbrains.kotlinx.jupyter.api.Code
88
import kotlin.reflect.KClass
9-
import kotlin.reflect.KMutableProperty
109
import kotlin.reflect.KProperty
1110

1211
internal interface ReplCodeGenerator {
1312

1413
fun process(
1514
df: AnyFrame,
1615
property: KProperty<*>? = null,
17-
isMutable: Boolean = property is KMutableProperty?,
1816
): CodeWithConverter
1917

2018
fun process(
2119
row: AnyRow,
2220
property: KProperty<*>? = null,
23-
isMutable: Boolean = property is KMutableProperty?,
2421
): CodeWithConverter
2522

2623
fun process(markerClass: KClass<*>): Code

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.jetbrains.kotlinx.dataframe.codeGen.MarkersExtractor
1616
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
1717
import org.jetbrains.kotlinx.jupyter.api.Code
1818
import kotlin.reflect.KClass
19-
import kotlin.reflect.KMutableProperty
2019
import kotlin.reflect.KProperty
2120
import kotlin.reflect.KType
2221
import kotlin.reflect.KVisibility
@@ -45,25 +44,22 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
4544
else -> null
4645
}
4746

48-
override fun process(row: AnyRow, property: KProperty<*>?, isMutable: Boolean) =
49-
process(row.df(), property, isMutable)
47+
override fun process(row: AnyRow, property: KProperty<*>?): CodeWithConverter = process(row.df(), property)
5048

51-
override fun process(df: AnyFrame, property: KProperty<*>?, isMutable: Boolean): CodeWithConverter {
49+
override fun process(df: AnyFrame, property: KProperty<*>?): CodeWithConverter {
5250
var targetSchema = df.schema()
53-
var isMutable = isMutable
5451

5552
if (property != null) {
5653
val wasProcessedBefore = property in registeredProperties
5754
registeredProperties.add(property)
58-
isMutable = property is KMutableProperty
5955

6056
// maybe property is already properly typed, let's do some checks
6157
val currentMarker = getMarkerClass(property.returnType)
6258
?.takeIf { it.findAnnotation<DataSchema>() != null }
6359
?.let { registeredMarkers[it] ?: MarkersExtractor.get(it) }
6460
if (currentMarker != null) {
65-
// if property is mutable, we need to make sure that its marker type is open in order to let derived data frames be assignable to it
66-
if (!isMutable || currentMarker.isOpen) {
61+
// we need to make sure that the property's marker type is open in order to let derived data frames be assignable to it
62+
if (currentMarker.isOpen) {
6763
val columnSchema = currentMarker.schema
6864
// for mutable properties we do strong typing only at the first processing, after that we allow its type to be more general than actual data frame type
6965
if (wasProcessedBefore || columnSchema == targetSchema) {
@@ -79,7 +75,7 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
7975
}
8076
}
8177

82-
return generate(schema = targetSchema, name = markerInterfacePrefix, isOpen = isMutable)
78+
return generate(schema = targetSchema, name = markerInterfacePrefix, isOpen = true)
8379
}
8480

8581
fun generate(

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/codeGen/CodeGenerationTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CodeGenerationTests : BaseTest() {
5959
val generated = codeGen.process(df, ::df)
6060
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
6161
val expectedDeclaration = """
62-
@DataSchema(isOpen = false)
62+
@DataSchema
6363
interface $typeName
6464
6565
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)
@@ -84,7 +84,7 @@ class CodeGenerationTests : BaseTest() {
8484
val generated = ReplCodeGenerator.create().process(df[0], property)
8585
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
8686
val expectedDeclaration = """
87-
@DataSchema(isOpen = false)
87+
@DataSchema
8888
interface $typeName
8989
9090
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)
@@ -118,7 +118,7 @@ class CodeGenerationTests : BaseTest() {
118118
""".trimIndent()
119119

120120
val declaration2 = """
121-
@DataSchema(isOpen = false)
121+
@DataSchema
122122
interface $type2
123123
124124
val $dfName<$type2>.age: $dataCol<$intName> @JvmName("${type2}_age") get() = this["age"] as $dataCol<$intName>

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/codeGen/ReplCodeGenTests.kt

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

33
import io.kotest.matchers.shouldBe
4-
import io.kotest.matchers.string.shouldBeEmpty
4+
import io.kotest.matchers.string.shouldNotBeEmpty
55
import org.jetbrains.dataframe.impl.codeGen.ReplCodeGenerator
66
import org.jetbrains.dataframe.impl.codeGen.process
77
import org.jetbrains.kotlinx.dataframe.ColumnsContainer
@@ -68,7 +68,7 @@ class ReplCodeGenTests : BaseTest() {
6868
@Test
6969
fun `process derived markers`() {
7070
val repl = ReplCodeGenerator.create()
71-
val code = repl.process(df, isMutable = true).declarations
71+
val code = repl.process(df).declarations
7272

7373
val marker = ReplCodeGeneratorImpl.markerInterfacePrefix
7474
val markerFull = Test1._DataFrameType::class.qualifiedName!!
@@ -100,10 +100,10 @@ class ReplCodeGenTests : BaseTest() {
100100
code2 shouldBe ""
101101

102102
val df3 = typed.filter { city != null }
103-
val code3 = repl.process(df3, isMutable = false).declarations
103+
val code3 = repl.process(df3).declarations
104104
val marker3 = marker + "1"
105105
val expected3 = """
106-
@DataSchema(isOpen = false)
106+
@DataSchema
107107
interface $marker3 : $markerFull
108108
109109
val $dfName<$marker3>.city: $dataCol<$stringName> @JvmName("${marker3}_city") get() = this["city"] as $dataCol<$stringName>
@@ -118,10 +118,10 @@ class ReplCodeGenTests : BaseTest() {
118118
code4 shouldBe ""
119119

120120
val df5 = typed.filter { weight != null }
121-
val code5 = repl.process(df5, isMutable = false).declarations
121+
val code5 = repl.process(df5).declarations
122122
val marker5 = marker + "2"
123123
val expected5 = """
124-
@DataSchema(isOpen = false)
124+
@DataSchema
125125
interface $marker5 : $markerFull
126126
127127
val $dfName<$marker5>.weight: $dataCol<$intName> @JvmName("${marker5}_weight") get() = this["weight"] as $dataCol<$intName>
@@ -144,12 +144,12 @@ class ReplCodeGenTests : BaseTest() {
144144
repl.process<Test2._DataFrameType1>() shouldBe ""
145145

146146
val expected = """
147-
@DataSchema(isOpen = false)
147+
@DataSchema
148148
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName}
149149
150150
""".trimIndent()
151151

152-
val code = repl.process(typed, isMutable = false).declarations.trimIndent()
152+
val code = repl.process(typed).declarations.trimIndent()
153153
code shouldBe expected
154154
}
155155

@@ -163,7 +163,7 @@ class ReplCodeGenTests : BaseTest() {
163163

164164
val marker = Test2._DataFrameType2::class.simpleName!!
165165
val expected = """
166-
@DataSchema(isOpen = false)
166+
@DataSchema
167167
interface $marker : ${Test2._DataFrameType::class.qualifiedName}
168168
169169
val $dfName<$marker>.city: $dataCol<$stringName?> @JvmName("${marker}_city") get() = this["city"] as $dataCol<$stringName?>
@@ -176,18 +176,18 @@ class ReplCodeGenTests : BaseTest() {
176176
val $dfRowName<$marker?>.weight: $intName? @JvmName("Nullable${marker}_weight") get() = this["weight"] as $intName?
177177
""".trimIndent()
178178

179-
val code = repl.process(typed, isMutable = false).declarations.trimIndent()
179+
val code = repl.process(typed).declarations.trimIndent()
180180
code shouldBe expected
181181
}
182182

183183
@Test
184-
fun `process overriden property`() {
184+
fun `process overridden property`() {
185185
val repl = ReplCodeGenerator.create()
186186
repl.process<Test3.A>()
187187
repl.process<Test3.B>()
188188
repl.process<Test3.C>()
189189
val c = repl.process(Test3.df, Test3::df)
190-
c.declarations.shouldBeEmpty()
190+
c.declarations.shouldNotBeEmpty()
191191
}
192192

193193
@Test

0 commit comments

Comments
 (0)