Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring about #97 #108

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide this method for kotlin-csv users to put the internal modifier, because there didn't seem to be many use cases for using this method.

val baos = ByteArrayOutputStream()
open(baos, write)
return String(baos.toByteArray(), Charset.forName(ctx.charset))
Expand Down Expand Up @@ -144,7 +144,7 @@ actual class CsvWriter actual constructor(
/**
* write all rows to string
*/
fun writeAll(rows: List<List<Any?>>): String {
return writeString { writeRows(rows) }
fun writeAllAsString(rows: List<List<Any?>>): String {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this method into writeAllAsString because there are many other writeAll methods and their behavior is slightly different from the behavior of this method.

return writeAsString { writeRows(rows) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ class CsvWriterTest : WordSpec({
}

"write data to String" {
val actual = csvWriter().writeAll(rows)
val actual = csvWriter().writeAllAsString(rows)
actual shouldBe expected
}
}
Expand Down