diff --git a/README.md b/README.md index ce96ec5..a55312f 100644 --- a/README.md +++ b/README.md @@ -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") } ``` @@ -76,3 +76,25 @@ listOf( 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. + +```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"])) } +``` diff --git a/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRow.kt b/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRow.kt new file mode 100644 index 0000000..04d7563 --- /dev/null +++ b/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRow.kt @@ -0,0 +1,27 @@ +package com.github.t45k.kotlin_data_table + +fun strTableToRow(table: String, delimiter: String = "|"): List { + return table.split(System.lineSeparator()) + .map { line -> line.split(delimiter).map { it.trim() } } + .map { StrTableRow(it) } +} + +class StrTableRow(private val values: List) { + 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 +} diff --git a/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRowWithName.kt b/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRowWithName.kt new file mode 100644 index 0000000..5b88a2d --- /dev/null +++ b/src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRowWithName.kt @@ -0,0 +1,19 @@ +package com.github.t45k.kotlin_data_table + +fun strTableToRowWithName(table: String, delimiter: String = "|"): List { + 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) { + 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 +} diff --git a/src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt b/src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt index 92e4013..7bb67ee 100644 --- a/src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt +++ b/src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt @@ -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 { @@ -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) } @@ -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 {