Skip to content

Commit

Permalink
Merge pull request #616 from vreuter/feature/609-CellEncoder-from-Show
Browse files Browse the repository at this point in the history
  • Loading branch information
ybasket authored Jun 25, 2024
2 parents f3a82b0 + 3f35c4c commit b42cfa0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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")
}

}

0 comments on commit b42cfa0

Please sign in to comment.