Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repositories {
}

dependencies {
implementation("com.github.T45K:kotlin-data-table:0.0.1")
implementation("com.github.T45K:kotlin-data-table:0.1.0")
}
```

Expand Down Expand Up @@ -76,3 +76,25 @@ listOf(
Person("Alex", 1, Gender.MALE),
)
```

You cal use multi-line string instead.<br>
Please note that it is your own responsibility to map string type to appropriate type.

```kotlin
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"])) }
```
27 changes: 27 additions & 0 deletions src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.t45k.kotlin_data_table

fun strTableToRow(table: String, delimiter: String = "|"): List<StrTableRow> {
return table.split(System.lineSeparator())
.map { line -> line.split(delimiter).map { it.trim() } }
.map { StrTableRow(it) }
}

class StrTableRow(private val values: List<String>) {
operator fun component1(): String = values[0]
operator fun component2(): String = values[1]
operator fun component3(): String = values[2]
operator fun component4(): String = values[3]
operator fun component5(): String = values[4]
operator fun component6(): String = values[5]
operator fun component7(): String = values[6]
operator fun component8(): String = values[7]
operator fun component9(): String = values[8]
operator fun component10(): String = values[9]

operator fun get(index: Int): String = values[index]

override fun toString(): String = values.toString()
override fun hashCode(): Int = values.hashCode()
override fun equals(other: Any?): Boolean =
this.values == (other as? TableRow)?.values
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.t45k.kotlin_data_table

fun strTableToRowWithName(table: String, delimiter: String = "|"): List<StrTableRowWithName> {
val lines = table.split(System.lineSeparator())
val headerColumns = lines[0].split(delimiter).map { it.trim() }
return lines.subList(1, lines.size)
.map { line -> line.split(delimiter).map { it.trim() } }
.map { headerColumns.zip(it).toMap() }
.map { StrTableRowWithName(it) }
}

class StrTableRowWithName(private val values: Map<String, String>) {
operator fun get(key: String): String = values[key]!!

override fun toString(): String = values.toString()
override fun hashCode(): Int = values.hashCode()
override fun equals(other: Any?): Boolean =
this.values == (other as? TableRow)?.values
}
41 changes: 30 additions & 11 deletions src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class TableTest {
private val expected = listOf(
Person("Bob", 27, Gender.MALE),
Person("Alice", 34, Gender.FEMALE),
Person("Alex", 1, Gender.MALE),
)

@Test
fun tableToRowTest() {
val actual = tableToRow {
Expand All @@ -14,12 +20,6 @@ class TableTest {
// @formatter:on
}.map { (name: String, age: Int, gender: Gender) -> Person(name, age, gender) }

val expected = listOf(
Person("Bob", 27, Gender.MALE),
Person("Alice", 34, Gender.FEMALE),
Person("Alex", 1, Gender.MALE),
)

assertEquals(expected, actual)
}

Expand All @@ -34,15 +34,34 @@ class TableTest {
// @formatter:on
}.map { Person(it["name"], it["age"], it["gender"]) }

val expected = listOf(
Person("Bob", 27, Gender.MALE),
Person("Alice", 34, Gender.FEMALE),
Person("Alex", 1, Gender.MALE),
)
assertEquals(expected, actual)
}

@Test
fun strTableToRow() {
val actual = strTableToRow(
"""
Bob | 27 | MALE
Alice | 34 | FEMALE
Alex | 1 | MALE
""".trimIndent()
).map { (name, age, gender) -> Person(name, age.toInt(), Gender.valueOf(gender)) }

assertEquals(expected, actual)
}

@Test
fun strTableToRowWithName() {
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"])) }
}

private data class Person(val name: String, val age: Int, val gender: Gender)

private enum class Gender {
Expand Down