Skip to content

Commit 5487df5

Browse files
authored
Merge pull request #1 from T45K/strTable
add `strTableToXXX` APIs
2 parents 577d44b + 8ad84a9 commit 5487df5

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ repositories {
3434
}
3535

3636
dependencies {
37-
implementation("com.github.T45K:kotlin-data-table:0.0.1")
37+
implementation("com.github.T45K:kotlin-data-table:0.1.0")
3838
}
3939
```
4040

@@ -76,3 +76,25 @@ listOf(
7676
Person("Alex", 1, Gender.MALE),
7777
)
7878
```
79+
80+
You cal use multi-line string instead.<br>
81+
Please note that it is your own responsibility to map string type to appropriate type.
82+
83+
```kotlin
84+
strTableToRow(
85+
"""
86+
Bob | 27 | MALE
87+
Alice | 34 | FEMALE
88+
Alex | 1 | MALE
89+
""".trimIndent()
90+
).map { (name, age, gender) -> Person(name, age.toInt(), Gender.valueOf(gender)) }
91+
92+
strTableToRowWithName(
93+
"""
94+
name | age | gender
95+
Bob | 27 | MALE
96+
Alice | 34 | FEMALE
97+
Alex | 1 | MALE
98+
""".trimIndent()
99+
).map { Person(it["name"], it["age"].toInt(), Gender.valueOf(it["gender"])) }
100+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.t45k.kotlin_data_table
2+
3+
fun strTableToRow(table: String, delimiter: String = "|"): List<StrTableRow> {
4+
return table.split(System.lineSeparator())
5+
.map { line -> line.split(delimiter).map { it.trim() } }
6+
.map { StrTableRow(it) }
7+
}
8+
9+
class StrTableRow(private val values: List<String>) {
10+
operator fun component1(): String = values[0]
11+
operator fun component2(): String = values[1]
12+
operator fun component3(): String = values[2]
13+
operator fun component4(): String = values[3]
14+
operator fun component5(): String = values[4]
15+
operator fun component6(): String = values[5]
16+
operator fun component7(): String = values[6]
17+
operator fun component8(): String = values[7]
18+
operator fun component9(): String = values[8]
19+
operator fun component10(): String = values[9]
20+
21+
operator fun get(index: Int): String = values[index]
22+
23+
override fun toString(): String = values.toString()
24+
override fun hashCode(): Int = values.hashCode()
25+
override fun equals(other: Any?): Boolean =
26+
this.values == (other as? TableRow)?.values
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.t45k.kotlin_data_table
2+
3+
fun strTableToRowWithName(table: String, delimiter: String = "|"): List<StrTableRowWithName> {
4+
val lines = table.split(System.lineSeparator())
5+
val headerColumns = lines[0].split(delimiter).map { it.trim() }
6+
return lines.subList(1, lines.size)
7+
.map { line -> line.split(delimiter).map { it.trim() } }
8+
.map { headerColumns.zip(it).toMap() }
9+
.map { StrTableRowWithName(it) }
10+
}
11+
12+
class StrTableRowWithName(private val values: Map<String, String>) {
13+
operator fun get(key: String): String = values[key]!!
14+
15+
override fun toString(): String = values.toString()
16+
override fun hashCode(): Int = values.hashCode()
17+
override fun equals(other: Any?): Boolean =
18+
this.values == (other as? TableRow)?.values
19+
}

src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt

+30-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import kotlin.test.Test
44
import kotlin.test.assertEquals
55

66
class TableTest {
7+
private val expected = listOf(
8+
Person("Bob", 27, Gender.MALE),
9+
Person("Alice", 34, Gender.FEMALE),
10+
Person("Alex", 1, Gender.MALE),
11+
)
12+
713
@Test
814
fun tableToRowTest() {
915
val actual = tableToRow {
@@ -14,12 +20,6 @@ class TableTest {
1420
// @formatter:on
1521
}.map { (name: String, age: Int, gender: Gender) -> Person(name, age, gender) }
1622

17-
val expected = listOf(
18-
Person("Bob", 27, Gender.MALE),
19-
Person("Alice", 34, Gender.FEMALE),
20-
Person("Alex", 1, Gender.MALE),
21-
)
22-
2323
assertEquals(expected, actual)
2424
}
2525

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

37-
val expected = listOf(
38-
Person("Bob", 27, Gender.MALE),
39-
Person("Alice", 34, Gender.FEMALE),
40-
Person("Alex", 1, Gender.MALE),
41-
)
37+
assertEquals(expected, actual)
38+
}
39+
40+
@Test
41+
fun strTableToRow() {
42+
val actual = strTableToRow(
43+
"""
44+
Bob | 27 | MALE
45+
Alice | 34 | FEMALE
46+
Alex | 1 | MALE
47+
""".trimIndent()
48+
).map { (name, age, gender) -> Person(name, age.toInt(), Gender.valueOf(gender)) }
4249

4350
assertEquals(expected, actual)
4451
}
4552

53+
@Test
54+
fun strTableToRowWithName() {
55+
strTableToRowWithName(
56+
"""
57+
name | age | gender
58+
Bob | 27 | MALE
59+
Alice | 34 | FEMALE
60+
Alex | 1 | MALE
61+
""".trimIndent()
62+
).map { Person(it["name"], it["age"].toInt(), Gender.valueOf(it["gender"])) }
63+
}
64+
4665
private data class Person(val name: String, val age: Int, val gender: Gender)
4766

4867
private enum class Gender {

0 commit comments

Comments
 (0)