Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.jetbrains.kotlinx.dataframe.codeGen.TypeCastGenerator
import org.jetbrains.kotlinx.dataframe.codeGen.ValidFieldName
import org.jetbrains.kotlinx.dataframe.codeGen.toNullable
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
import org.jetbrains.kotlinx.dataframe.impl.ColumnNameGenerator
import org.jetbrains.kotlinx.dataframe.impl.toSnakeCase
import org.jetbrains.kotlinx.dataframe.keywords.HardKeywords
import org.jetbrains.kotlinx.dataframe.keywords.ModifierKeywords
Expand Down Expand Up @@ -274,6 +275,34 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR

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

private val troubleshootingLink = ""

/**
* Generate a try-catch block for column accessor with custom error messages.
*
* Catch any exception. Throws an [IllegalStateException] with a special message and original exception.
* Can be improved with custom exception (#1872).
*
* 1) IllegalArgumentException -> most probably column not found. Should be replaced with a custom exception (#1871)
* 2) ClassCastException -> most probably incorrect column type. Should be replaced with a custom exception (#1871)
* 3) else -> unexpected exception.
*/
private fun generateTryCatchColumnAccess(columnAccessCode: String, columnName: String): String {
val renderedColumnName = renderStringLiteral(columnName)
return """
try {
$columnAccessCode
} catch (e: kotlin.Exception) {
val msg = when (e) {
is kotlin.IllegalArgumentException -> "Column not found exception in the generated DataFrame extension property '$renderedColumnName': " + e.getLocalizedMessage() + ". See $troubleshootingLink for more information."

@Jolanrensen Jolanrensen Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you mix string templates and + here, actually? Just curious :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me just better readability. But let's be consistent and idiomatic, ok.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also use a string builder if that looks more readable :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote with better templates!

is kotlin.ClassCastException -> "Incorrect column type exception in generated DataFrame extension property '$renderedColumnName': " + e.getLocalizedMessage() + " See $troubleshootingLink for more information."
else -> "Unexpected exception in generated DataFrame extension property '$renderedColumnName'. Please report it to https://github.com/Kotlin/dataframe/issues." + "Exception message: " + e.toString()
Comment thread
Jolanrensen marked this conversation as resolved.
Outdated
}
throw IllegalStateException(msg, e)
}
""".trimIndent()
}

private fun generatePropertyCode(
marker: IsolatedMarker,
shortMarkerName: String,
Expand All @@ -282,6 +311,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
propertyType: String,
getter: String,
visibility: String,
columnName: String,
): String {
// jvm name is required to prevent signature clash like this:
// val DataRow<Type>.name: String
Expand All @@ -296,7 +326,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
}
return "${visibility}val$typeParameters $typeName.$name: $propertyType @JvmName(\"${
renderStringLiteral(jvmName)
}\") get() = $getter as $propertyType"
}\") get() = ${generateTryCatchColumnAccess("$getter as $propertyType", columnName)} "
}

/**
Expand Down Expand Up @@ -355,6 +385,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
propertyType = columnType,
getter = getter,
visibility = visibility,
columnName = it.columnName,
),
generatePropertyCode(
marker = marker,
Expand All @@ -364,6 +395,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
propertyType = fieldType,
getter = getter,
visibility = visibility,
columnName = it.columnName,
),
),
)
Expand All @@ -378,6 +410,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
propertyType = nullableColumnType,
getter = getter,
visibility = visibility,
columnName = it.columnName,
),
generatePropertyCode(
marker = marker,
Expand All @@ -387,6 +420,7 @@ internal open class ExtensionsCodeGeneratorImpl(private val typeRendering: TypeR
propertyType = nullableFieldType,
getter = getter,
visibility = visibility,
columnName = it.columnName,
),
),
)
Expand Down
Loading
Loading