|
| 1 | +package com.jsoizo.kotlincsv.client |
| 2 | + |
| 3 | +import com.jsoizo.kotlincsv.dsl.CsvReaderScope |
| 4 | +import com.jsoizo.kotlincsv.dsl.context.CsvReaderContext |
| 5 | +import com.jsoizo.kotlincsv.dsl.context.ICsvReaderContext |
| 6 | +import kotlinx.io.Buffer |
| 7 | +import kotlinx.io.Source |
| 8 | +import kotlinx.io.buffered |
| 9 | +import kotlinx.io.files.Path |
| 10 | +import kotlinx.io.files.SystemFileSystem |
| 11 | + |
| 12 | +/** |
| 13 | + * CSVReader implementation. |
| 14 | + * |
| 15 | + * @author gsteckman |
| 16 | + */ |
| 17 | +internal class CsvReaderImpl( |
| 18 | + private val ctx: CsvReaderContext = CsvReaderContext() |
| 19 | +) : CsvReader, ICsvReaderContext by ctx { |
| 20 | + override fun readAll(data: String): List<List<String>> = data.csvReadAll(ctx) |
| 21 | + |
| 22 | + override fun readAll(data: Source): List<List<String>> = data.csvReadAll(ctx) |
| 23 | + |
| 24 | + override fun readAll(path: Path): List<List<String>> = path.csvReadAll(ctx) |
| 25 | + |
| 26 | + override fun readAllWithHeader(data: String): List<Map<String, String>> = |
| 27 | + data.csvReadAllWithHeader(ctx) |
| 28 | + |
| 29 | + override fun readAllWithHeader(data: Source): List<Map<String, String>> = data.csvReadAllWithHeader(ctx) |
| 30 | + |
| 31 | + override fun readAllWithHeader(path: Path): List<Map<String, String>> = path.csvReadAllWithHeader(ctx) |
| 32 | + |
| 33 | + override fun <T> open(source: Source, read: CsvReaderScope.() -> T): T = |
| 34 | + SourceCsvReaderScope(ctx, source, ctx.logger).read() |
| 35 | + |
| 36 | + override fun <T> open(path: Path, read: CsvReaderScope.() -> T): T = |
| 37 | + SystemFileSystem.source(path).buffered().use { |
| 38 | + return open(it, read) |
| 39 | + } |
| 40 | + |
| 41 | + override suspend fun <T> openAsync(source: Source, read: suspend CsvReaderScope.() -> T): T = |
| 42 | + SourceCsvReaderScope(ctx, source, ctx.logger).read() |
| 43 | + |
| 44 | + override suspend fun <T> openAsync(path: Path, read: suspend CsvReaderScope.() -> T): T = |
| 45 | + SystemFileSystem.source(path).buffered().use { |
| 46 | + return openAsync(it, read) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * read csv data as String, and convert into List<List<String>> |
| 52 | + */ |
| 53 | +fun String.csvReadAll(ctx: CsvReaderContext = CsvReaderContext()): List<List<String>> = |
| 54 | + Buffer().apply{ write(encodeToByteArray()) }.use { |
| 55 | + it.csvReadAll(ctx) |
| 56 | + } |
| 57 | + |
| 58 | +/** |
| 59 | + * read csv data with header, and convert into List<Map<String, String>> |
| 60 | + */ |
| 61 | +fun String.csvReadAllWithHeader(ctx: CsvReaderContext = CsvReaderContext()): List<Map<String, String>> = |
| 62 | + Buffer().apply{ write(encodeToByteArray()) }.use { |
| 63 | + it.csvReadAllWithHeader(ctx) |
| 64 | + } |
| 65 | + |
| 66 | +/** |
| 67 | + * read csv data from a Source, and convert into List<List<String>>. |
| 68 | + */ |
| 69 | +fun Source.csvReadAll(ctx: CsvReaderContext = CsvReaderContext()): List<List<String>> = |
| 70 | + SourceCsvReaderScope(ctx, this, ctx.logger).readAllAsSequence().toList() |
| 71 | + |
| 72 | +/** |
| 73 | + * read csv data from a Path, and convert into List<List<String>>. |
| 74 | + * |
| 75 | + * No need to close the Path when calling this method. |
| 76 | + */ |
| 77 | +fun Path.csvReadAll(ctx: CsvReaderContext = CsvReaderContext()): List<List<String>> = |
| 78 | + SystemFileSystem.source(this).buffered().use { |
| 79 | + return it.csvReadAll(ctx) |
| 80 | + } |
| 81 | + |
| 82 | +/** |
| 83 | + * read csv data with a header from a Source, and convert into List<List<String>>. |
| 84 | + */ |
| 85 | +fun Source.csvReadAllWithHeader(ctx: CsvReaderContext = CsvReaderContext()): List<Map<String, String>> = |
| 86 | + SourceCsvReaderScope(ctx, this, ctx.logger).readAllWithHeaderAsSequence().toList() |
| 87 | + |
| 88 | +/** |
| 89 | + * read csv data with header, and convert into List<Map<String, String>> |
| 90 | + * |
| 91 | + * No need to close Path when calling this method. |
| 92 | + */ |
| 93 | +fun Path.csvReadAllWithHeader(ctx: CsvReaderContext = CsvReaderContext()): List<Map<String, String>> = |
| 94 | + SystemFileSystem.source(this).buffered().use { |
| 95 | + return it.csvReadAllWithHeader(ctx) |
| 96 | + } |
| 97 | + |
| 98 | +/** |
| 99 | + * read all csv rows as Sequence |
| 100 | + */ |
| 101 | +fun Source.csvReadAllAsSequence(fieldsNum: Int? = null, ctx: CsvReaderContext = CsvReaderContext()) |
| 102 | + : Sequence<List<String>> = SourceCsvReaderScope(ctx, this, ctx.logger).readAllAsSequence(fieldsNum) |
| 103 | + |
| 104 | +/** |
| 105 | + * read all csv rows as Sequence with header information |
| 106 | + */ |
| 107 | +fun Source.csvReadAllWithHeaderAsSequence(ctx: CsvReaderContext = CsvReaderContext()) |
| 108 | + : Sequence<Map<String, String>> = SourceCsvReaderScope(ctx, this, ctx.logger).readAllWithHeaderAsSequence() |
0 commit comments