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

Allow empty csv cell strings in generic derivation #391

Merged
merged 2 commits into from
Oct 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private[generic] trait LowPriorityMapShapedCsvRowDecoder1 {
default: Option[Head] :: DefaultTail,
anno: Anno :: AnnoTail): DecoderResult[FieldType[Key, Head] :: Tail] = {
val head = row(anno.head.fold(witness.value.name)(_.name)) match {
case Some(head) if head.nonEmpty =>
case Some(head) =>
Head(head).leftMap(_.withLine(row.line))
case _ =>
default.head.liftTo[DecoderResult](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ sealed trait OptCellDecoder[T] {

object OptCellDecoder extends LowPrioOptCellDecoders {
given makeNonOpt[A: CellDecoder]: OptCellDecoder[A] = new OptCellDecoder[A] {
override def apply(name: String, value: Option[String]): DecoderResult[A] =
value.toRight(new DecoderError(s"Missing column $name")).flatMap(CellDecoder[A].apply)
override def apply(name: String, value: Option[String]): DecoderResult[A] = {
CellDecoder[A].apply(value.orEmpty)
}
}
}

trait LowPrioOptCellDecoders {
given makeOpt[A: CellDecoder]: OptCellDecoder[Option[A]] = new OptCellDecoder[Option[A]] {
override def apply(name: String, value: Option[String]): DecoderResult[Option[A]] =
value.traverse(CellDecoder[A].apply(_))
value.filter(_.nonEmpty).traverse(CellDecoder[A].apply(_))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object semiauto {
ic.unfold[(Int, List[String], Option[DecoderError])]((0, row.values.toList, None))(
[t] =>
(acc: (Int, List[String], Option[DecoderError]), cd: OptCellDecoder[t]) => {
val result = cd(s"at index ${acc._1}", acc._2.headOption.filter(_.nonEmpty))
val result = cd(s"at index ${acc._1}", acc._2.headOption)
((acc._1 + 1, acc._2.tail, result.left.toOption), result.toOption)
}
)
Expand Down Expand Up @@ -67,7 +67,7 @@ object semiauto {
[t] =>
(acc: (Int, CsvRow[String], Option[DecoderError]), cd: OptCellDecoder[t]) => {
val columnName = names(acc._1)
val result = cd.apply(columnName, acc._2(columnName).filter(_.nonEmpty))
val result = cd.apply(columnName, acc._2(columnName))
((acc._1 + 1, row, result.left.toOption), result.toOption)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ object CsvRowDecoderTest extends SimpleIOSuite {
CsvRow.unsafe(NonEmptyList.of("1", "test", ""), NonEmptyList.of("i", "s", "j"))
val csvRowNoJ =
CsvRow.unsafe(NonEmptyList.of("1", "test"), NonEmptyList.of("i", "s"))
val csvRowEmptyCell = CsvRow.unsafe(NonEmptyList.of("1", "", "42"), NonEmptyList.of("i", "s", "j"))

case class Test(i: Int = 0, s: String, j: Option[Int])
case class TestOrder(s: String, j: Int, i: Int = 8888888)
case class TestRename(s: String, @CsvName("j") k: Int, i: Int)
case class TestOptionRename(s: String, @CsvName("j") k: Option[Int], i: Int)
case class TestOptionalString(i: Int, s: Option[String], j: Int)

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

pureTest("case classes should be decoded properly by header name and not position") {
expect(testDecoder(csvRow) == Right(Test(1, "test", Some(42)))) and
Expand Down Expand Up @@ -74,4 +77,9 @@ object CsvRowDecoderTest extends SimpleIOSuite {
expect(testOptionRenameDecoder(csvRowNoJ) == Right(TestOptionRename("test", None, 1)))
}

pureTest("allow empty strings as string cell values") {
expect(testDecoder(csvRowEmptyCell) == Right(Test(1, "", Some(42)))) and
expect(testOptionalStringDecoder(csvRowEmptyCell) == Right(TestOptionalString(1, None, 42)))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object RowDecoderTest extends SimpleIOSuite {
val csvRow = Row(NonEmptyList.of("1", "test", "42"))
val csvRowEmptyI = Row(NonEmptyList.of("", "test", "42"))
val csvRowEmptyJ = Row(NonEmptyList.of("1", "test", ""))
val csvRowEmptyS = Row(NonEmptyList.of("1", "", "42"))

case class Test(i: Int, s: String, j: Int)
case class TestOrder(s: String, i: Int, j: Int)
Expand All @@ -52,4 +53,8 @@ object RowDecoderTest extends SimpleIOSuite {
expect(testOptJDecoder(csvRowEmptyJ) == Right(TestOptJ(1, "test", None)))
}

pureTest("allow empty strings as string cell values") {
expect(testDecoder(csvRowEmptyS) == Right(Test(1, "", 42)))
}

}