Skip to content

Commit a8843f2

Browse files
authored
Merge pull request #1731 from Kotlin/soft-keywords-quote
Prevented escaping of SoftKeywords in code generator
2 parents 891a37e + 7d1c419 commit a8843f2

3 files changed

Lines changed: 50 additions & 38 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
3737
import org.jetbrains.kotlinx.dataframe.impl.toSnakeCase
3838
import org.jetbrains.kotlinx.dataframe.keywords.HardKeywords
3939
import org.jetbrains.kotlinx.dataframe.keywords.ModifierKeywords
40+
import org.jetbrains.kotlinx.dataframe.keywords.SoftKeywords
4041
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode
4142
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
4243
import kotlin.reflect.KClass
@@ -71,12 +72,15 @@ internal fun String.needsQuoting(): Boolean =
7172
isBlank() ||
7273
first().isDigit() ||
7374
contains(charsToQuote) ||
74-
HardKeywords.VALUES.contains(this) ||
75-
ModifierKeywords.VALUES.contains(this) ||
75+
hardKeywords.contains(this) ||
76+
modifierKeywordsToQuote.contains(this) ||
7677
all { it == '_' } ||
7778
any { it != '_' && it.category !in letterCategories }
7879
}
7980

81+
private val hardKeywords: Set<String> = HardKeywords.VALUES.toSet()
82+
private val modifierKeywordsToQuote: Set<String> = ModifierKeywords.VALUES.toSet() - SoftKeywords.VALUES.toSet()
83+
8084
public fun String.isQuoted(): Boolean = startsWith("`") && endsWith("`")
8185

8286
public fun String.quoteIfNeeded(): String = if (needsQuoting()) "`$this`" else this

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ class CodeGenerationTests : BaseTest() {
196196
assertEquals(expected, code.value)
197197
}
198198

199+
@Test
200+
fun `generateDataClasses with SoftKeywords`() {
201+
val df = dataFrameOf("value" to columnOf(123))
202+
val code = df.generateDataClasses()
203+
val expected =
204+
"""
205+
@DataSchema
206+
data class DataEntry(
207+
val value: Int
208+
)
209+
""".trimIndent()
210+
assertEquals(expected, code.value)
211+
}
212+
199213
val personClassName = Person::class.qualifiedName!!
200214

201215
val personShortName = Person::class.simpleName!!

0 commit comments

Comments
 (0)