Skip to content

Commit

Permalink
Merge pull request #427 from Kotlin/marker-compilation
Browse files Browse the repository at this point in the history
fix compilation for markers with empty bodies in Jupyter
  • Loading branch information
koperagen authored Jul 21, 2023
2 parents 2eac6b6 + bf5e98f commit 5488a34
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ internal open class ExtensionsCodeGeneratorImpl(

private fun String.removeQuotes() = this.removeSurrounding("`")

private fun generateExtensionProperties(markers: List<Marker>) = markers.map { generateExtensionProperties(it) }

private fun generatePropertyCode(
marker: IsolatedMarker,
shortMarkerName: String,
Expand Down Expand Up @@ -430,11 +428,6 @@ internal class CodeGeneratorImpl(typeRendering: TypeRenderingStrategy = FullyQua
return CodeGenResult(code, context.generatedMarkers)
}

private fun generateInterfaces(
schemas: List<Marker>,
fields: Boolean,
) = schemas.map { generateInterface(it, fields) }

private fun generateInterface(
marker: Marker,
fields: Boolean,
Expand Down Expand Up @@ -483,7 +476,7 @@ internal class CodeGeneratorImpl(typeRendering: TypeRenderingStrategy = FullyQua
append("\n}")
}
} else {
""
" { }"
}
resultDeclarations.add(header + baseInterfacesDeclaration + body)
return resultDeclarations.join()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CodeGenerationTests : BaseTest() {
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
val expectedDeclaration = """
@DataSchema
interface $typeName
interface $typeName { }
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)

Expand All @@ -85,7 +85,7 @@ class CodeGenerationTests : BaseTest() {
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
val expectedDeclaration = """
@DataSchema
interface $typeName
interface $typeName { }
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)

Expand All @@ -104,7 +104,7 @@ class CodeGenerationTests : BaseTest() {
val type2 = ReplCodeGeneratorImpl.markerInterfacePrefix
val declaration1 = """
@DataSchema(isOpen = false)
interface $type1
interface $type1 { }
val $dfName<$type1>.city: $dataCol<$stringName?> @JvmName("${type1}_city") get() = this["city"] as $dataCol<$stringName?>
val $dfRowName<$type1>.city: $stringName? @JvmName("${type1}_city") get() = this["city"] as $stringName?
Expand All @@ -119,7 +119,7 @@ class CodeGenerationTests : BaseTest() {

val declaration2 = """
@DataSchema
interface $type2
interface $type2 { }
val $dfName<$type2>.age: $dataCol<$intName> @JvmName("${type2}_age") get() = this["age"] as $dataCol<$intName>
val $dfRowName<$type2>.age: $intName @JvmName("${type2}_age") get() = this["age"] as $intName
Expand All @@ -146,7 +146,7 @@ class CodeGenerationTests : BaseTest() {
val personClass = (Person::class).qualifiedName!!
val expected = """
@DataSchema
interface $personClass
interface $personClass { }
""".trimIndent() + "\n" + expectedProperties(personClassName, personShortName)

val code = CodeGenerator.create(useFqNames = false)
Expand Down Expand Up @@ -203,7 +203,7 @@ class CodeGenerationTests : BaseTest() {
val code = codeGen.generate(df.schema(), "Person", false, true, true).code.declarations
val expected = """
@DataSchema
interface Person
interface Person { }
""".trimIndent() + "\n" + expectedProperties("Person", "Person")
code shouldBe expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ReplCodeGenTests : BaseTest() {

val expected = """
@DataSchema
interface $marker
interface $marker { }
val $dfName<$marker>.age: $dataCol<$intName> @JvmName("${marker}_age") get() = this["age"] as $dataCol<$intName>
val $dfRowName<$marker>.age: $intName @JvmName("${marker}_age") get() = this["age"] as $intName
Expand Down Expand Up @@ -104,7 +104,7 @@ class ReplCodeGenTests : BaseTest() {
val marker3 = marker + "1"
val expected3 = """
@DataSchema
interface $marker3 : $markerFull
interface $marker3 : $markerFull { }
val $dfName<$marker3>.city: $dataCol<$stringName> @JvmName("${marker3}_city") get() = this["city"] as $dataCol<$stringName>
val $dfRowName<$marker3>.city: $stringName @JvmName("${marker3}_city") get() = this["city"] as $stringName
Expand All @@ -122,7 +122,7 @@ class ReplCodeGenTests : BaseTest() {
val marker5 = marker + "2"
val expected5 = """
@DataSchema
interface $marker5 : $markerFull
interface $marker5 : $markerFull { }
val $dfName<$marker5>.weight: $dataCol<$intName> @JvmName("${marker5}_weight") get() = this["weight"] as $dataCol<$intName>
val $dfRowName<$marker5>.weight: $intName @JvmName("${marker5}_weight") get() = this["weight"] as $intName
Expand All @@ -145,7 +145,7 @@ class ReplCodeGenTests : BaseTest() {

val expected = """
@DataSchema
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName}
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName} { }
""".trimIndent()

Expand All @@ -164,7 +164,7 @@ class ReplCodeGenTests : BaseTest() {
val marker = Test2._DataFrameType2::class.simpleName!!
val expected = """
@DataSchema
interface $marker : ${Test2._DataFrameType::class.qualifiedName}
interface $marker : ${Test2._DataFrameType::class.qualifiedName} { }
val $dfName<$marker>.city: $dataCol<$stringName?> @JvmName("${marker}_city") get() = this["city"] as $dataCol<$stringName?>
val $dfRowName<$marker>.city: $stringName? @JvmName("${marker}_city") get() = this["city"] as $stringName?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ class CodeGenerationTests : DataFrameJupyterTest() {
row.a
""".checkCompilation()
}

@Test
fun `interface without body compiled correctly`() {
"""
val a = dataFrameOf("a")(1, 2, 3)
val b = dataFrameOf("b")(1, 2, 3)
val ab = dataFrameOf("a", "b")(1, 2)
ab.a
""".checkCompilation()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ internal open class ExtensionsCodeGeneratorImpl(

private fun String.removeQuotes() = this.removeSurrounding("`")

private fun generateExtensionProperties(markers: List<Marker>) = markers.map { generateExtensionProperties(it) }

private fun generatePropertyCode(
marker: IsolatedMarker,
shortMarkerName: String,
Expand Down Expand Up @@ -430,11 +428,6 @@ internal class CodeGeneratorImpl(typeRendering: TypeRenderingStrategy = FullyQua
return CodeGenResult(code, context.generatedMarkers)
}

private fun generateInterfaces(
schemas: List<Marker>,
fields: Boolean,
) = schemas.map { generateInterface(it, fields) }

private fun generateInterface(
marker: Marker,
fields: Boolean,
Expand Down Expand Up @@ -483,7 +476,7 @@ internal class CodeGeneratorImpl(typeRendering: TypeRenderingStrategy = FullyQua
append("\n}")
}
} else {
""
" { }"
}
resultDeclarations.add(header + baseInterfacesDeclaration + body)
return resultDeclarations.join()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CodeGenerationTests : BaseTest() {
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
val expectedDeclaration = """
@DataSchema
interface $typeName
interface $typeName { }
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)

Expand All @@ -85,7 +85,7 @@ class CodeGenerationTests : BaseTest() {
val typeName = ReplCodeGeneratorImpl.markerInterfacePrefix
val expectedDeclaration = """
@DataSchema
interface $typeName
interface $typeName { }
""".trimIndent() + "\n" + expectedProperties(typeName, typeName)

Expand All @@ -104,7 +104,7 @@ class CodeGenerationTests : BaseTest() {
val type2 = ReplCodeGeneratorImpl.markerInterfacePrefix
val declaration1 = """
@DataSchema(isOpen = false)
interface $type1
interface $type1 { }
val $dfName<$type1>.city: $dataCol<$stringName?> @JvmName("${type1}_city") get() = this["city"] as $dataCol<$stringName?>
val $dfRowName<$type1>.city: $stringName? @JvmName("${type1}_city") get() = this["city"] as $stringName?
Expand All @@ -119,7 +119,7 @@ class CodeGenerationTests : BaseTest() {

val declaration2 = """
@DataSchema
interface $type2
interface $type2 { }
val $dfName<$type2>.age: $dataCol<$intName> @JvmName("${type2}_age") get() = this["age"] as $dataCol<$intName>
val $dfRowName<$type2>.age: $intName @JvmName("${type2}_age") get() = this["age"] as $intName
Expand All @@ -146,7 +146,7 @@ class CodeGenerationTests : BaseTest() {
val personClass = (Person::class).qualifiedName!!
val expected = """
@DataSchema
interface $personClass
interface $personClass { }
""".trimIndent() + "\n" + expectedProperties(personClassName, personShortName)

val code = CodeGenerator.create(useFqNames = false)
Expand Down Expand Up @@ -203,7 +203,7 @@ class CodeGenerationTests : BaseTest() {
val code = codeGen.generate(df.schema(), "Person", false, true, true).code.declarations
val expected = """
@DataSchema
interface Person
interface Person { }
""".trimIndent() + "\n" + expectedProperties("Person", "Person")
code shouldBe expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ReplCodeGenTests : BaseTest() {

val expected = """
@DataSchema
interface $marker
interface $marker { }
val $dfName<$marker>.age: $dataCol<$intName> @JvmName("${marker}_age") get() = this["age"] as $dataCol<$intName>
val $dfRowName<$marker>.age: $intName @JvmName("${marker}_age") get() = this["age"] as $intName
Expand Down Expand Up @@ -104,7 +104,7 @@ class ReplCodeGenTests : BaseTest() {
val marker3 = marker + "1"
val expected3 = """
@DataSchema
interface $marker3 : $markerFull
interface $marker3 : $markerFull { }
val $dfName<$marker3>.city: $dataCol<$stringName> @JvmName("${marker3}_city") get() = this["city"] as $dataCol<$stringName>
val $dfRowName<$marker3>.city: $stringName @JvmName("${marker3}_city") get() = this["city"] as $stringName
Expand All @@ -122,7 +122,7 @@ class ReplCodeGenTests : BaseTest() {
val marker5 = marker + "2"
val expected5 = """
@DataSchema
interface $marker5 : $markerFull
interface $marker5 : $markerFull { }
val $dfName<$marker5>.weight: $dataCol<$intName> @JvmName("${marker5}_weight") get() = this["weight"] as $dataCol<$intName>
val $dfRowName<$marker5>.weight: $intName @JvmName("${marker5}_weight") get() = this["weight"] as $intName
Expand All @@ -145,7 +145,7 @@ class ReplCodeGenTests : BaseTest() {

val expected = """
@DataSchema
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName}
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName} { }
""".trimIndent()

Expand All @@ -164,7 +164,7 @@ class ReplCodeGenTests : BaseTest() {
val marker = Test2._DataFrameType2::class.simpleName!!
val expected = """
@DataSchema
interface $marker : ${Test2._DataFrameType::class.qualifiedName}
interface $marker : ${Test2._DataFrameType::class.qualifiedName} { }
val $dfName<$marker>.city: $dataCol<$stringName?> @JvmName("${marker}_city") get() = this["city"] as $dataCol<$stringName?>
val $dfRowName<$marker>.city: $stringName? @JvmName("${marker}_city") get() = this["city"] as $stringName?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ class CodeGenerationTests : DataFrameJupyterTest() {
row.a
""".checkCompilation()
}

@Test
fun `interface without body compiled correctly`() {
"""
val a = dataFrameOf("a")(1, 2, 3)
val b = dataFrameOf("b")(1, 2, 3)
val ab = dataFrameOf("a", "b")(1, 2)
ab.a
""".checkCompilation()
}
}

0 comments on commit 5488a34

Please sign in to comment.