Skip to content

Commit

Permalink
adds kotlin.dataframe.debug property which runs the #713 checks when …
Browse files Browse the repository at this point in the history
…enabled. Updated contribution guide too. TC will have to be updated manually
  • Loading branch information
Jolanrensen committed Jul 30, 2024
1 parent 9bacc4e commit c16b37e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ so do familiarize yourself with the following guidelines.

## PR workflow

0. The contributor builds the library locally and runs all unit tests via the Gradle task `dataframe:test`
(see the ["Building"](#building) chapter).
0. The contributor builds the library locally and runs all unit tests via the Gradle task
`dataframe:test -Pkotlin.dataframe.debug=true` (see the ["Building"](#building) chapter).
1. The contributor submits the PR if the local build is successful and the tests are green.
2. The reviewer puts their name in the "Reviewers" section of the proposed PR at the start of the review process.
3. The reviewer leaves comments or marks the PR with the abbreviation "LGTM" (Looks good to me).
Expand Down Expand Up @@ -103,6 +103,8 @@ This library is built with Gradle.
* Run `./gradlew build` to build. It also runs all the tests and checks the linter.
* Run `./gradlew <module>:test` to test the module you are looking at to speed
things up during development.
* Make sure to pass the extra parameter `-Pkotlin.dataframe.debug=true` to enable debug mode. This flag will
make sure some extra checks are run, which are important but too heavy for production.

You can import this project into IDEA, but you have to delegate the build actions
to Gradle (in Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner)
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ allprojects {
packageName = "org.jetbrains.kotlinx.dataframe"
className = "BuildConfig"
buildConfigField("VERSION", "${project.version}")
buildConfigField("DEBUG", findProperty("kotlin.dataframe.debug")?.toString()?.toBoolean() ?: false)
}
} catch (_: UnknownDomainObjectException) {
logger.warn("Could not set buildConfig on :${this.name}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.jetbrains.kotlinx.dataframe.impl.columns

import org.jetbrains.kotlinx.dataframe.BuildConfig
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.impl.isArray
import org.jetbrains.kotlinx.dataframe.impl.isPrimitiveArray
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf

internal abstract class DataColumnImpl<T>(
protected val values: List<T>,
Expand All @@ -12,6 +17,32 @@ internal abstract class DataColumnImpl<T>(
) : DataColumn<T>,
DataColumnInternal<T> {

private infix fun <T> T?.matches(type: KType) =
when {
this == null -> type.isMarkedNullable

this.isPrimitiveArray ->
type.isPrimitiveArray &&
this!!::class.qualifiedName == type.classifier?.let { (it as KClass<*>).qualifiedName }

this.isArray -> type.isArray

// cannot check the precise type of array
else -> this!!::class.isSubclassOf(type.classifier as KClass<*>)
}

init {
/* Check for [Issue #713](https://github.com/Kotlin/dataframe/issues/713).
* This only runs with `kotlin.dataframe.debug=true` in gradle.properties
*/
if (BuildConfig.DEBUG) {
require(values.all { it matches type }) {
val types = values.map { if (it == null) "Nothing?" else it!!::class.simpleName }.distinct()
"Values of column '$name' have types '$types' which are not compatible given with column type '$type'"
}
}
}

protected val distinct = distinct ?: lazy { values.toSet() }

override fun name() = name
Expand Down
5 changes: 5 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ org.gradle.jvmargs=-Xmx4G
# This makes it mandatory to explicitly apply your own version of the
# KSP plugin in the modules that use it.
kotlin.dataframe.add.ksp=false

# Enables debug mode for dataframe.
# This can make certain tests run that should not be run in production.
# It can also be turned on from the command line with `-Pkotlin.dataframe.debug=true`
kotlin.dataframe.debug=false

0 comments on commit c16b37e

Please sign in to comment.