Skip to content
Merged
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
44 changes: 18 additions & 26 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,23 @@ object PerftConverter extends IOApp.Simple {

## Writing data to a CSV file

If you want to save a list of a case class into a CSV file this helper method may aid you:
If you want to save a list of a case class into a CSV file this utility may aid you:

```scala
// Define your case class and derive an encoder for it
case class YourCaseClass(n: String, i: Int)
given CsvRowEncoder[YourCaseClass, String] = deriveCsvRowEncoder

// Writes a case class as a csv given a path.
def writeCaseClassToCsv[A](
path: Path
)(using CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] =
_.through(encodeUsingFirstHeaders(fullRows = true))
.through(fs2.text.utf8.encode)
.through(Files[IO].writeAll(path))
```

As an example, let's imagine we have a `Book` class we would like to write to a `.csv` file.

@:select(scala-version)

Expand All @@ -436,22 +452,14 @@ import fs2.io.file.{Files, Path}
import cats.effect.{IO, IOApp}
import fs2.{Pipe, Stream}

/** Define your case class and derive an encoder for it */
case class YourCaseClass(n: String, i: Int)
given CsvRowEncoder[YourCaseClass, String] = deriveCsvRowEncoder

/** This is our helper function that writes a case class as a csv given a path. */
def writeCaseClassToCsv[A](
path: Path
)(using CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] =
_.through(encodeUsingFirstHeaders(fullRows = true))
.through(fs2.text.utf8.encode)
.through(Files[IO].writeAll(path))
.drain

/** Let's imagine we have a `Book` case class we would like to write to
* a .csv file.
*/

object WriteBooksToCsv extends IOApp.Simple:
case class Book(id: Long, name: String, isbn: String)
given CsvRowEncoder[Book, String] = deriveCsvRowEncoder
Expand Down Expand Up @@ -481,31 +489,15 @@ import fs2.io.file.{Files, Path}
import cats.effect.{IO, IOApp}
import fs2.{Pipe, Stream}

/** Define your case class and derive an encoder for it */
case class YourCaseClass(n: String, i: Int)
object YourCaseClass {
implicit val csvRowEncoder: CsvRowEncoder[YourCaseClass, String] =
deriveCsvRowEncoder
}

object Helpers {

/** This is our helper function that writes a case class as a csv given a
* path.
*/
def writeCaseClassToCsv[A](
path: Path
)(implicit encoder: CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] =
_.through(encodeUsingFirstHeaders(fullRows = true))
.through(fs2.text.utf8.encode)
.through(Files[IO].writeAll(path))
.drain

}

/** Let's imagine we have a `Book` case class we would like to write to a .csv
* file.
*/
object WriteBooksToCsv extends IOApp.Simple {
case class Book(id: Long, name: String, isbn: String)
implicit val csvRowEncoder: CsvRowEncoder[Book, String] = deriveCsvRowEncoder
Expand Down