Simple data table DSL for Kotlin.
This is inspired by Spock Data Tables.
Please refer to JitPack page.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.t45k</groupId>
<artifactId>kotlin-data-table</artifactId>
<version>0.0.1</version>
</dependency>
import java.net.URI
repositories {
maven { url = URI("https://jitpack.io") }
}
dependencies {
implementation("com.github.T45K:kotlin-data-table:0.1.0")
}
Please refer to test cases.
tableToRow
converts table DSL to tuple of columns.
It is destructive.
tableToRow {
"Bob" `|` 27 `|` Gender.MALE
"Alice" `|` 34 `|` Gender.FEMALE
"Alex" `|` 1 `|` Gender.MALE
}.map { (name: String, age: Int, gender: Gender) -> Person(name, age, gender) }
listOf(
Person("Bob", 27, Gender.MALE),
Person("Alice", 34, Gender.FEMALE),
Person("Alex", 1, Gender.MALE),
)
tableToRowWithName
converts table DSL to tuple of columns by name.
It is like Map
structure, i.e., you can get value by name.
tableToRowWithName {
"name" `|` "age" `|` "gender" // column names are necessary in the first row
"Bob" `|` 27 `|` Gender.MALE
"Alice" `|` 34 `|` Gender.FEMALE
"Alex" `|` 1 `|` Gender.MALE
}.map { Person(it["name"], it["age"], it["gender"]) } // get value by column name
listOf(
Person("Bob", 27, Gender.MALE),
Person("Alice", 34, Gender.FEMALE),
Person("Alex", 1, Gender.MALE),
)
You cal use multi-line string instead.
Please note that it is your own responsibility to map string type to appropriate type.
strTableToRow(
"""
Bob | 27 | MALE
Alice | 34 | FEMALE
Alex | 1 | MALE
""".trimIndent()
).map { (name, age, gender) -> Person(name, age.toInt(), Gender.valueOf(gender)) }
strTableToRowWithName(
"""
name | age | gender
Bob | 27 | MALE
Alice | 34 | FEMALE
Alex | 1 | MALE
""".trimIndent()
).map { Person(it["name"], it["age"].toInt(), Gender.valueOf(it["gender"])) }