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

Add ability to build CellEncoder from cats.Show #616

Merged
merged 4 commits into from
Jun 25, 2024
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
3 changes: 3 additions & 0 deletions csv/shared/src/main/scala/fs2/data/csv/CellEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ object CellEncoder
@inline
def fromToString[A]: CellEncoder[A] = _.toString

@inline
def fromShow[A](implicit ev: Show[A]): CellEncoder[A] = instance(ev.show)

// Primitives
implicit val unitEncoder: CellEncoder[Unit] = _ => ""
implicit val booleanEncoder: CellEncoder[Boolean] = fromToString(_)
Expand Down
13 changes: 12 additions & 1 deletion csv/shared/src/test/scala/fs2/data/csv/CellEncoderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import weaver._

import scala.concurrent.duration._

import cats.Show

object CellEncoderTest extends SimpleIOSuite {

// CellEncoder should have implicit instances available for standard types
Expand Down Expand Up @@ -53,7 +55,7 @@ object CellEncoderTest extends SimpleIOSuite {
CellEncoder[java.time.ZoneId]
CellEncoder[java.time.ZoneOffset]

pureTest("CellEncoder should decode standard types correctly") {
pureTest("CellEncoder should encode standard types correctly") {
expect(CellEncoder[Unit].apply(()) == "") and
expect(CellEncoder[Int].apply(78) == "78") and
expect(CellEncoder[Boolean].apply(true) == "true") and
Expand All @@ -73,4 +75,13 @@ object CellEncoderTest extends SimpleIOSuite {
.apply(java.time.LocalTime.of(13, 4, 29)) == "13:04:29")
}

pureTest("CellEncoder instance can be built from native cats.Show instance") {
expect(CellEncoder.fromShow[Double].apply(3.54) == "3.54")
}

pureTest("CellEncoder instance can be built from local cats.Show instance") {
implicit val showInt42: Show[Int] = Show.show(_ => "42")
expect(CellEncoder.fromShow[Int].apply(78) == "42")
}

}
Loading