-
Notifications
You must be signed in to change notification settings - Fork 9
Update examples.md #94
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
Conversation
Case classes to CSV
|
Actually I've needed this several times as well, thank you! :P. I like the pipe approach more personally. I would like the examples to be a bit more "hey why you would do it and this is how" style, so I would phrase it more like this: |
|
With the benefit of hinsight, my original PR was rushed, and perhaps a little sloppy. Will tidy with the suggestions above - thanks for feedback - agreed on all points. Will revise in the next days when I have a few mins at keyboard to devote to it! |
|
Thanks for all the feedback - incorporated and hopefully mergable now :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to say that even if you might think this PR looked rushed, it still helps a lot with just getting the ball rolling. And that is always valuable (especially in open source).
edit: fml github ui bamboozled me, I thought I submitted my suggestions, anyhow see blow
agreed Co-authored-by: zetashift <rskaraya@gmail.com>
Co-authored-by: zetashift <rskaraya@gmail.com>
|
@Quafadas just the last 2 suggestions and besides that it LGTM! |
|
@zetashift I thought I already approved and committed them? Ah... I fear however, that I did not press the "resolve conversation" button ... gonna do that right now. |
Co-authored-by: zetashift <rskaraya@gmail.com>
Co-authored-by: zetashift <rskaraya@gmail.com>
|
@zetashift Oh dear, I honestly thought I'd done them. I've had another go. |
zetashift
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Thank you for this :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a couple of comments in the code ;)
Thanks for your contribution!
|
Here's a runnable example: //> using toolkit typelevel:latest
//> using scala 3.3.1
import fs2.io.file.Files
import fs2.data.csv.*
import fs2.data.csv.generic.semiauto.*
import fs2.io.file.Path
import cats.effect.{IO, IOApp}
import fs2.{Pipe, Stream}
import java.time.LocalDate
/** 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
val input = Seq(
Book(1, "Programming in Scala", "9780997148008"),
Book(2, "Hands-on Scala Programming", "9798387677205"),
Book(3, "Functional Programming in Scala", "9781617299582")
)
def run: IO[Unit] =
Stream
.emits(input)
.through(writeCaseClassToCsv(Path("books.csv")))
.compile
.drain *> IO.println("Finished writing books to books.csv.")
@Quafadas if you don't have time to update the PR, is it okay if I add this in? Would hate for this to sit too long. |
Add runnable example for CSV writing example.
Fix CSV writing example
Fix csv example
| ```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)) | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's scala 3 only, can you add the scala 2 tab too?
I think I've written this method a few times now, and keep having to scratch my head each time :-)...
So here it is - no pressure to merge if it isn't idiomatic :-).