# Data Schemas Generation From Existing DataFrame

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Special utility functions that generate code of useful Kotlin definitions (returned as a `String`) based on the current `DataFrame` schema.

## generateInterfaces

```KOTLIN
inline fun <reified T> DataFrame<T>.generateInterfaces(): CodeString

fun <T> DataFrame<T>.generateInterfaces(markerName: String): CodeString
```

Generates [@DataSchema](schemas.html) interfaces for this `DataFrame` (including all nested `DataFrame` columns and column groups) as Kotlin interfaces.

This is useful when working with the [compiler plugin](compiler-plugin.html) in cases where the schema cannot be inferred automatically from the source.

### Arguments

* `markerName`: `String?` — The base name to use for generated interfaces. If `null`, uses the simple name of the `T` type argument of `DataFrame`. Default: `null`.

* `extensionProperties`: `Boolean` – Whether to generate [extension properties](extensionpropertiesapi.html) in addition to `interface` declarations. Useful if you don't use the [compiler plugin](compiler-plugin.html), otherwise they are not needed; the [compiler plugin](compiler-plugin.html), [notebooks](setupkotlinnotebook.html), and older [Gradle/KSP plugin](schemasgradle.html) generate them automatically. Default: `false`.

* `visibility`: `MarkerVisibility` – Visibility modifier for the generated declarations. Default: `MarkerVisibility.IMPLICIT_PUBLIC`.

* `useFqNames`: `Boolean` – If `true`, fully qualified type names will be used in generated code. Default: `false`.

* `nameNormalizer`: `NameNormalizer` – Strategy for converting column names (with spaces, underscores, etc.) to Kotlin-style identifiers. Generated properties will still refer to columns by their actual name using the `@ColumnName` annotation. Default: `NameNormalizer.default`.

* `nestedMarkerNameProvider`: `MarkerNameProvider` – Strategy for generating names for nested data schema declarations (markers). * `MarkerNameProvider.fromColumnName` (default) generates descriptive names from the column names. * `MarkerNameProvider.PredefinedName` uses the name of the root marker for all nested declarations and appends a numerical suffix to resolve name conflicts. Default: `MarkerNameProvider.fromColumnName`.

### Returns

* `CodeString` – A value class wrapper for `String`, containing the generated Kotlin code of `@DataSchema` interfaces. It also contains generated [extension properties](extensionpropertiesapi.html) for `extensionProperties=true`.

### Examples

```KOTLIN
df
```

[Embedded content](./resources/notebook_test_generate_docs_1.html)

```KOTLIN
df.generateInterfaces(markerName = "Customer")
```

Output:

```KOTLIN
@DataSchema
interface Customer {
    val orders: List<Orders>
    val user: String

    @DataSchema(isOpen = false)
    interface Orders {
        val amount: Double
        val orderId: Int
    }
}
```

By adding these interfaces to your project with the [compiler plugin](compiler-plugin.html) enabled,

you'll gain full support for the [extension properties API](extensionpropertiesapi.html) and type-safe operations.

Use [cast](cast.html) to apply the generated schema to a `DataFrame`. Right after that, you can use [extension properties](extensionpropertiesapi.html) in the following operations:

```KOTLIN
df.cast<Customer>().filter { orders.all { orderId >= 102 } }
```

## generateDataClasses

```KOTLIN
inline fun <reified T> DataFrame<T>.generateDataClasses(
    markerName: String? = null,
    extensionProperties: Boolean = false,
    visibility: MarkerVisibility = MarkerVisibility.IMPLICIT_PUBLIC,
    useFqNames: Boolean = false,
    nameNormalizer: NameNormalizer = NameNormalizer.default,
    nestedMarkerNameProvider: MarkerNameProvider = MarkerNameProvider.fromColumnName,
): CodeString
```

Generates Kotlin data classes corresponding to the `DataFrame` schema (including all nested `DataFrame` columns and column groups).

Useful when you want to:

* Work with the data as regular Kotlin data classes.

* Convert a dataframe to instantiated data classes with `df.toListOf<DataClassType>()`.

* Work with data classes serialization.

* Extract structured types for further use in your application.

### Arguments

* `markerName`: `String?` — The base name to use for generated data classes. If `null`, uses the simple name of the `T` type argument of `DataFrame`. Default: `null`.

* `extensionProperties`: `Boolean` – Whether to generate [extension properties](extensionpropertiesapi.html) in addition to `data class` declarations. Useful if you don't use the [compiler plugin](compiler-plugin.html), otherwise they are not needed; the [compiler plugin](compiler-plugin.html), [notebooks](setupkotlinnotebook.html), and older [Gradle/KSP plugin](schemasgradle.html) generate them automatically. Default: `false`.

* `visibility`: `MarkerVisibility` – Visibility modifier for the generated declarations. Default: `MarkerVisibility.IMPLICIT_PUBLIC`.

* `useFqNames`: `Boolean` – If `true`, fully qualified type names will be used in generated code. Default: `false`.

* `nameNormalizer`: `NameNormalizer` – Strategy for converting column names (with spaces, underscores, etc.) to Kotlin-style identifiers. Generated properties will still refer to columns by their actual name using the `@ColumnName` annotation. Default: `NameNormalizer.default`.

* `nestedMarkerNameProvider`: `MarkerNameProvider` – Strategy for generating names for nested data schema declarations (markers). * `MarkerNameProvider.fromColumnName` (default) generates descriptive names from the column names. * `MarkerNameProvider.PredefinedName` uses the name of the root marker for all nested declarations and appends a numerical suffix to resolve name conflicts. Default: `MarkerNameProvider.fromColumnName`.

### Returns

* `CodeString` – A value class wrapper for `String`, containing the generated Kotlin code of `@DataSchema` data classes. It also contains generated [extension properties](extensionpropertiesapi.html) for `extensionProperties=true`.

### Examples

```KOTLIN
df.generateDataClasses("Customer")
```

Output:

```KOTLIN
@DataSchema
data class Customer(
    val orders: List<Orders>,
    val user: String
) {
    @DataSchema
    data class Orders(
        val amount: Double,
        val orderId: Int
    )
}
```

By adding these data classes to your project with the [compiler plugin](compiler-plugin.html) enabled,

you'll gain full support for the [extension properties API](extensionpropertiesapi.html) and type-safe operations.

Use [cast](cast.html) to apply the generated schema to a `DataFrame`. Right after that, you can use [extension properties](extensionpropertiesapi.html) in the following operations:

```KOTLIN
df.cast<Customer>().filter { orders.all { orderId >= 102 } }
```

You can use these classes for the `DataFrame` conversion to a list of typed objects:

```KOTLIN
val customers: List<Customer> = df.cast<Customer>().toList()
```

