Skip to content

Commit

Permalink
Merge pull request #21 from ybasket/fix/csv-name-on-options
Browse files Browse the repository at this point in the history
Fix CsvName on Option types
  • Loading branch information
satabin authored May 5, 2020
2 parents 6bf404b + 0b6453f commit 0fefd5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object MapShapedCsvRowDecoder extends LowPriorityMapShapedCsvRowDecoder1 {
def fromWithDefault(row: CsvRow[String],
default: Option[Option[Head]] :: DefaultTail,
anno: Anno :: AnnoTail): DecoderResult[FieldType[Key, Option[Head]] :: Tail] = {
val head = row(witness.value.name) match {
val head = row(anno.head.fold(witness.value.name)(_.name)) match {
case Some(head) if head.nonEmpty => Head(head).map(Some(_))
case _ => Right(default.head.flatten)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ class CsvRowDecoderTest extends FlatSpec with Matchers {
case class Test(i: Int = 0, s: String, j: Option[Int])
case class TestOrder(s: String, j: Int, i: Int)
case class TestRename(s: String, @CsvName("j") k: Int, i: Int)
case class TestOptionRename(s: String, @CsvName("j") k: Option[Int], i: Int)

val testDecoder = deriveCsvRowDecoder[Test]
val testOrderDecoder = deriveCsvRowDecoder[TestOrder]
val testRenameDecoder = deriveCsvRowDecoder[TestRename]
val testOptionRenameDecoder = deriveCsvRowDecoder[TestOptionRename]

"case classes" should "be decoded properly by header name and not position" in {
testDecoder(csvRow) shouldBe Right(Test(1, "test", Some(42)))
Expand Down Expand Up @@ -65,4 +67,9 @@ class CsvRowDecoderTest extends FlatSpec with Matchers {
testRenameDecoder(csvRow) shouldBe Right(TestRename("test", 42, 1))
}

it should "be decoded according to their field renames if value is optional" in {
testOptionRenameDecoder(csvRow) shouldBe Right(TestOptionRename("test", Some(42), 1))
testOptionRenameDecoder(csvRowNoJ) shouldBe Right(TestOptionRename("test", None, 1))
}

}

0 comments on commit 0fefd5d

Please sign in to comment.