Skip to content

Commit 96beb54

Browse files
committed
Improve redundant qualifiers removal in generated code
1 parent 9573c9a commit 96beb54

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.jetbrains.kotlinx.dataframe.keywords.ModifierKeywords
4040
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode
4141
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
4242
import kotlin.reflect.KClass
43+
import kotlin.text.replace
4344

4445
private fun renderNullability(nullable: Boolean) = if (nullable) "?" else ""
4546

@@ -246,18 +247,15 @@ internal object ShortNames : TypeRenderingStrategy {
246247
}
247248
}
248249

249-
private fun String.shorten() = removeRedundantQualifier(this)
250-
251-
private fun removeRedundantQualifier(markerName: String): String {
252-
val parts = markerName.split('.')
253-
return if (parts.size == 2 && parts[0] == "kotlin") {
254-
parts[1]
255-
} else {
256-
markerName
257-
}
258-
}
250+
private fun String.shorten() = this.dropKotlinPackages()
259251
}
260252

253+
internal fun String.dropKotlinPackages() = replace(REDUNDANT_PACKAGE_QUALIFIER, "")
254+
255+
private val REDUNDANT_PACKAGE_QUALIFIER = Regex(
256+
"""kotlin(?:\.(?:collections|sequences|ranges|text|io|comparisons))?\.(?=[A-Z])""",
257+
)
258+
261259
internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeRenderingStrategy) :
262260
ExtensionsCodeGenerator,
263261
TypeRenderingStrategy by typeRendering {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,19 @@ class CodeGenerationTests : BaseTest() {
543543
"""^[\d]""".toRegex().matches("3fds")
544544
}
545545

546+
@Test
547+
fun shortenKotlinCollections() {
548+
val df = dataFrameOf("a" to columnOf(listOf("abc")))
549+
val expected =
550+
"""
551+
@DataSchema
552+
data class DataEntry(
553+
val a: List<String>
554+
)
555+
""".trimIndent()
556+
assertEquals(expected, df.generateDataClasses().value)
557+
}
558+
546559
// region Tests for generateX functions
547560

548561
@Test

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
88
import org.jetbrains.kotlinx.dataframe.codeGen.MarkersExtractor
99
import org.jetbrains.kotlinx.dataframe.impl.codeGen.ShortNames
1010
import org.jetbrains.kotlinx.dataframe.impl.codeGen.TypeRenderingStrategy
11+
import org.jetbrains.kotlinx.dataframe.impl.codeGen.dropKotlinPackages
1112
import org.junit.Test
1213

1314
internal class ShortNamesRenderingTest : TypeRenderingStrategy by ShortNames {
@@ -61,8 +62,8 @@ internal class ShortNamesRenderingTest : TypeRenderingStrategy by ShortNames {
6162
@Test
6263
fun `short functional types are not supported`() {
6364
fields.keys.asClue {
64-
fields["d"]!!.renderAccessorFieldType() shouldBe "() -> kotlin.Unit"
65-
fields["d"]!!.renderFieldType() shouldBe "() -> kotlin.Unit"
65+
fields["d"]!!.renderAccessorFieldType() shouldBe "() -> Unit"
66+
fields["d"]!!.renderFieldType() shouldBe "() -> Unit"
6667
}
6768
}
6869

@@ -112,7 +113,7 @@ internal class ShortNamesRenderingTest : TypeRenderingStrategy by ShortNames {
112113
@Test
113114
fun `functional type column`() {
114115
fields.keys.asClue {
115-
fields["d"]!!.renderColumnType() shouldBe "DataColumn<() -> kotlin.Unit>"
116+
fields["d"]!!.renderColumnType() shouldBe "DataColumn<() -> Unit>"
116117
}
117118
}
118119

@@ -146,4 +147,14 @@ internal class ShortNamesRenderingTest : TypeRenderingStrategy by ShortNames {
146147
fun `generic column`() {
147148
MarkersExtractor.get(GenericDataSchema::class).allFields[0].renderColumnType() shouldBe "DataColumn<A>"
148149
}
150+
151+
@Test
152+
fun `drop redundant kotlin package prefixes`() {
153+
"kotlin.String".dropKotlinPackages() shouldBe "String"
154+
"kotlin.collections.List<kotlin.String>".dropKotlinPackages() shouldBe "List<String>"
155+
"kotlin.collections.Map<kotlin.String, kotlin.collections.List<kotlin.Int>>".dropKotlinPackages() shouldBe
156+
"Map<String, List<Int>>"
157+
"org.example.Foo<kotlin.String>".dropKotlinPackages() shouldBe "org.example.Foo<String>"
158+
"kotlin.internal.Foo".dropKotlinPackages() shouldBe "kotlin.internal.Foo"
159+
}
149160
}

0 commit comments

Comments
 (0)