Skip to content

Commit 60a0c96

Browse files
committed
Add castTo(Function<T>) overload to use with compiler plugin
1 parent 439f65d commit 60a0c96

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/cast.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ public inline fun <reified T> AnyFrame.castTo(
4242
verify: Boolean = true,
4343
): DataFrame<T> = cast<T>(verify = verify)
4444

45+
/**
46+
* With the compiler plugin, schema marker T of DataFrame can be a local type.
47+
* You cannot refer to it directly from your code, like a type argument for cast.
48+
* The example below shows a situation where you'd need to cast DataFrame<*> to DataFrame<plugin generated local type>.
49+
* This function helps by inferring type from [schemaFrom]
50+
* ```
51+
*
52+
* // parse listOf("b:1:abc", "c:2:bca")
53+
* private fun convert(data: List<String>)/*: DataFrame<plugin generated local type>*/ = data.map { it.split(":") }.toDataFrame {
54+
* "part1" from { it[0] }
55+
* "part2" from { it[1].toInt() }
56+
* "part3" from { it[2] }
57+
* }
58+
*
59+
* fun serialize(data: List<String>, destination: File) {
60+
* convert(data).writeJson(destination)
61+
* }
62+
*
63+
* fun deserializeAndUse(file: File) {
64+
* val df = DataFrame.readJson(file).castTo(schemaFrom = ::convert)
65+
* // Possible to use properties
66+
* df.part1.print()
67+
* }
68+
* ```
69+
*/
70+
public inline fun <reified T> AnyFrame.castTo(
71+
@Suppress("UNUSED_PARAMETER") schemaFrom: Function<DataFrame<T>>,
72+
verify: Boolean = true,
73+
): DataFrame<T> = cast<T>(verify = verify)
74+
4575
public fun <T> AnyRow.cast(): DataRow<T> = this as DataRow<T>
4676

4777
public inline fun <reified T> AnyRow.cast(verify: Boolean = true): DataRow<T> = df().cast<T>(verify)[0]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.jetbrains.kotlinx.dataframe.*
2+
import org.jetbrains.kotlinx.dataframe.annotations.*
3+
import org.jetbrains.kotlinx.dataframe.api.*
4+
import org.jetbrains.kotlinx.dataframe.io.*
5+
import java.io.File
6+
7+
private fun convert(data: List<String>) = data.map { it.split(":") }.toDataFrame {
8+
"part1" from { it[0] }
9+
"part2" from { it[1].toInt() }
10+
"part3" from { it[2] }
11+
}
12+
13+
fun serialize(data: List<String>, destination: File) {
14+
convert(data).writeJson(destination)
15+
}
16+
17+
fun deserializeAndUse(file: File) {
18+
val df = DataFrame.readJson(file).castTo(schemaFrom = ::convert)
19+
df.part1.print()
20+
}
21+
22+
fun box(): String {
23+
val file = File.createTempFile("temp", "json")
24+
serialize(listOf("b:1:abc", "c:2:bca"), file)
25+
deserializeAndUse(file)
26+
return "OK"
27+
}

plugins/kotlin-dataframe/tests-gen/org/jetbrains/kotlin/fir/dataframe/DataFrameBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public void testCastTo() {
4040
runTest("testData/box/castTo.kt");
4141
}
4242

43+
@Test
44+
@TestMetadata("castTo_function.kt")
45+
public void testCastTo_function() {
46+
runTest("testData/box/castTo_function.kt");
47+
}
48+
4349
@Test
4450
@TestMetadata("columnGroupApi.kt")
4551
public void testColumnGroupApi() {

0 commit comments

Comments
 (0)