|
1 | 1 | package org.jetbrains.kotlinx.dataframe.annotations |
2 | 2 |
|
| 3 | +import org.jetbrains.kotlinx.dataframe.api.convertTo |
| 4 | +import org.jetbrains.kotlinx.dataframe.api.cast |
| 5 | + |
| 6 | +/** |
| 7 | + * Annotation to generate extension properties API for a given declaration, according to its properties. |
| 8 | + * Annotated declaration should be non-local and non-private interface or a class. |
| 9 | + * The aim here is to provide convenient syntax for working with a dataframe instance right after reading from it CSV, JSON, Databases, Arrow, etc. |
| 10 | + * After `val df = DataFrame.read*` operation, `df` is a source of truth for the DataSchema. |
| 11 | + * One way to look at it, DataSchema "tells" the compiler what's already there. It doesn't affect reading. |
| 12 | + * See the list below of code generation methods to simplify the process of getting what we call an initial dataschema. |
| 13 | + * Given the initial schema of the data you read, the compiler plugin will provide a typed result for most operations. |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * ``` |
| 17 | + * @DataSchema |
| 18 | + * data class Group( |
| 19 | + * val id: String, |
| 20 | + * val participants: List<Person> |
| 21 | + * ) |
| 22 | + * |
| 23 | + * @DataSchema |
| 24 | + * data class Person( |
| 25 | + * val name: Name, |
| 26 | + * val age: Int, |
| 27 | + * val city: String? |
| 28 | + * ) |
| 29 | + * |
| 30 | + * @DataSchema |
| 31 | + * data class Name( |
| 32 | + * val firstName: String, |
| 33 | + * val lastName: String, |
| 34 | + * ) |
| 35 | + * |
| 36 | + * fun main() { |
| 37 | + * val url = "https://raw.githubusercontent.com/Kotlin/dataframe/refs/heads/master/data/participants.json" |
| 38 | + * val df = DataFrame.readJson(url).cast<Group>() |
| 39 | + * val i: Int = df.id[0] // properties style access to columns and values |
| 40 | + * |
| 41 | + * val df1 = df.asGroupBy { participants }.aggregate { |
| 42 | + * count() into "groupSize" |
| 43 | + * distinct { city } into "cities" |
| 44 | + * } |
| 45 | + * |
| 46 | + * // now compiler plugin uses previous knowledge of `Group` combined with its understanding of aggregate operation |
| 47 | + * // to help you access new columns |
| 48 | + * val l: List<String> = df1.cities[0] |
| 49 | + * } |
| 50 | + * ``` |
| 51 | + * |
| 52 | + * @see [org.jetbrains.kotlinx.dataframe.api.generateDataClasses] |
| 53 | + * @see [org.jetbrains.kotlinx.dataframe.api.generateInterfaces] |
| 54 | + * @see [org.jetbrains.kotlinx.dataframe.DataFrame.cast] |
| 55 | + * @see [org.jetbrains.kotlinx.dataframe.DataFrame.convertTo] |
| 56 | + */ |
3 | 57 | @Target(AnnotationTarget.CLASS) |
4 | 58 | public annotation class DataSchema(val isOpen: Boolean = true) |
0 commit comments