Skip to content

Commit 3d9c104

Browse files
committed
Add kdocs for DataSchema and DataRowSchema
1 parent 2438baf commit 3d9c104

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,58 @@
11
package org.jetbrains.kotlinx.dataframe.annotations
22

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+
*/
357
@Target(AnnotationTarget.CLASS)
458
public annotation class DataSchema(val isOpen: Boolean = true)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
4+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
45

6+
/**
7+
* Marker interface that's automatically added to classes annotated with [DataSchema]
8+
*/
59
public interface DataRowSchema
610

11+
/**
12+
* Example:
13+
* ```
14+
* @DataSchema
15+
* data class Person(val name: String, val age: Int)
16+
*
17+
* fun main() {
18+
* val df = dataFrameOf(Person("Alice", 30), Person("Bob", 25))
19+
* }
20+
* ```
21+
*/
722
public inline fun <reified T : DataRowSchema> dataFrameOf(vararg rows: T): DataFrame<T> =
823
rows.asIterable().toDataFrame()
924

0 commit comments

Comments
 (0)