From 5717e0f32d96ec49286eb008100abca85ee06393 Mon Sep 17 00:00:00 2001 From: kenta koyama Date: Sun, 7 Aug 2022 17:11:37 +0900 Subject: [PATCH 1/2] refs #97 rename method --- .../com/github/doyaaaaaken/kotlincsv/client/CsvWriter.kt | 6 +++--- .../github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriter.kt b/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriter.kt index 97eab7b..ffddad9 100644 --- a/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriter.kt +++ b/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriter.kt @@ -51,7 +51,7 @@ actual class CsvWriter actual constructor( writer.use { it.write() } } - fun writeString(write: ICsvFileWriter.() -> Unit): String { + internal fun writeAsString(write: ICsvFileWriter.() -> Unit): String { val baos = ByteArrayOutputStream() open(baos, write) return String(baos.toByteArray(), Charset.forName(ctx.charset)) @@ -144,7 +144,7 @@ actual class CsvWriter actual constructor( /** * write all rows to string */ - fun writeAll(rows: List>): String { - return writeString { writeRows(rows) } + fun writeAllAsString(rows: List>): String { + return writeAsString { writeRows(rows) } } } diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt index fb70c66..2b6b3ef 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt @@ -91,13 +91,13 @@ class CsvWriterTest : WordSpec({ } } - "writeString method" should { + "writeAsString method" should { val row1 = listOf("a", "b", null) val row2 = listOf("d", "2", "1.0") val expected = "a,b,\r\nd,2,1.0\r\n" "write simple csv data to String" { - val actual = csvWriter().writeString { + val actual = csvWriter().writeAsString { writeRow(row1) writeRow(row2) } @@ -128,7 +128,7 @@ class CsvWriterTest : WordSpec({ } "write data to String" { - val actual = csvWriter().writeAll(rows) + val actual = csvWriter().writeAllAsString(rows) actual shouldBe expected } } From a6287034164f9300fc30b7e5b14f4e79f41d0ebd Mon Sep 17 00:00:00 2001 From: kenta koyama Date: Sun, 7 Aug 2022 17:22:28 +0900 Subject: [PATCH 2/2] refs #97 add readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 0d1147a..d364581 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,13 @@ csvWriter().openAsync(testFileName) { } ``` +#### Write as String + +```kotlin +val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f")) +val csvString: String = csvWriter().writeAllAsString(rows) //a,b,c\r\nd,e,f\r\n +``` + #### long-running write (manual control for file close) If you want to close a file writer manually for performance reasons (e.g. streaming scenario), you can