https://github.com/retheviper/kotlin-seed
Kotlin CSV writer
https://github.com/retheviper/kotlin-seed
csv-export kotlin kotlin-csv kotlin-grass
Last synced: 8 months ago
JSON representation
Kotlin CSV writer
- Host: GitHub
- URL: https://github.com/retheviper/kotlin-seed
- Owner: retheviper
- License: apache-2.0
- Created: 2022-08-27T16:25:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-08T10:26:03.000Z (over 2 years ago)
- Last Synced: 2025-02-16T06:44:49.581Z (10 months ago)
- Topics: csv-export, kotlin, kotlin-csv, kotlin-grass
- Language: Kotlin
- Homepage:
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kotlin-Seed
> Kotlin Data Class to CSV Converter
> Uses [kotlin-csv by doyaaaaaken](https://github.com/doyaaaaaken/kotlin-csv/)
> Inspired by [Kotlin-Grass](https://github.com/blackmo18/kotlin-grass)
## Requirements
- Java 17
## Examples
### Declare data class
```kotlin
data class TestData(
val id: Int,
val name: String,
val birth: LocalDateTime,
val joined: LocalDate,
val total: LocalTime,
)
```
### Init seed
```kotlin
val seed = seed {
dateFormat = "yyyy-MM-dd" // default
timeFormat = "HH:mm" // default
dateTime = " " // default
trimWhiteSpace = true // default
headerNamingStrategy = HeaderNamingStrategies.CAMEL_TO_SPACE // nullable
}
```
### Write to CSV
```kotlin
val datas = listOf(
TestData(
id = 1,
name = "John",
birth = LocalDateTime.of(1980, 3, 1, 12, 0),
joined = LocalDate.of(2020, 4, 15),
total = LocalTime.of(12, 30, 10)
),
TestData(
id = 2,
name = "Jane",
birth = LocalDateTime.of(1981, 4, 1, 13, 0),
joined = LocalDate.of(2021, 5, 15),
total = LocalTime.of(13, 30, 20)
),
TestData(
id = 3,
name = "Jack",
birth = LocalDateTime.of(1982, 5, 1, 14, 0),
joined = LocalDate.of(2022, 6, 15),
total = LocalTime.of(14, 30, 30)
)
)
seed.plant(
seeds = datas,
targetFile = File("datas.csv")
)
```
### CSV results
| id | name | birth | joined | total |
|-----|------|------------------|------------|-------|
| 1 | John | 1980-03-01 12:00 | 2020-04-15 | 12:30 |
| 2 | Jane | 1981-04-01 13:00 | 2021-05-15 | 13:30 |
| 3 | Jack | 1982-05-01 14:00 | 2022-06-15 | 14:30 |
### Use annotation for header
#### Declare data class with annotation
```kotlin
data class TestData(
@CsvHeaderName("id")
val a: Int,
@CsvHeaderName("name")
val b: String,
@CsvHeaderName("birth")
val c: LocalDateTime,
@CsvHeaderName("joined date")
val d: LocalDate,
@CsvHeaderName("total time")
val e: LocalTime,
)
```
#### Write to CSV
```kotlin
val datas = listOf(
TestData(
a = 1,
b = "John",
c = LocalDateTime.of(1980, 3, 1, 12, 0),
d = LocalDate.of(2020, 4, 15),
e = LocalTime.of(12, 30, 10)
),
TestData(
a = 2,
b = "Jane",
c = LocalDateTime.of(1981, 4, 1, 13, 0),
d = LocalDate.of(2021, 5, 15),
e = LocalTime.of(13, 30, 20)
),
TestData(
a = 3,
b = "Jack",
c = LocalDateTime.of(1982, 5, 1, 14, 0),
d = LocalDate.of(2022, 6, 15),
e = LocalTime.of(14, 30, 30)
)
)
seed().plant(
seeds = datas,
targetFile = File("datas.csv")
)
```
#### CSV Results
| id | name | birth | joined date | total time |
|-----|------|------------------|-------------|------------|
| 1 | John | 1980-03-01 12:00 | 2020-04-15 | 12:30 |
| 2 | Jane | 1981-04-01 13:00 | 2021-05-15 | 13:30 |
| 3 | Jack | 1982-05-01 14:00 | 2022-06-15 | 14:30 |
or you can find more examples in [src/test/kotlin](./src/test/kotlin)