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

Handle case class defaults better #612

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ybasket
Copy link
Collaborator

@ybasket ybasket commented Jun 23, 2024

Always parse empty cells to their default values if one is defined, allowing to use defaults with types that can't be parsed from an empty cell, like numbers. Comes at the expense of not being able to parse empty cells as-is any longer if a default exist.

Closes #607

Always parse empty cells to their default values if one is defined, allowing to use defaults with types that can't be parsed from an empty cell, like numbers. Comes at the expense of not being able to parse empty cells as-is any longer if a default exist.

Closes #607
@ybasket ybasket requested a review from a team as a code owner June 23, 2024 18:09
}

pureTest("case classes should be handled properly with default string value and empty cell") {
expect(testDefaultsDecoder(csvRowEmptyJ) == Right(TestDefaults(1, "foo")))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the default "foo" is expected even though a String cell could handle the empty cell. While I think it's the better behaviour for users as they now can handle Int or any other type that can't be parsed from empty input via default, it is a change that we should highlight in the release notes. Users that need to handle empty cells may no longer define a default (except for "" itself) for those in their case class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, I would add a clear note in the documentation for this caveat.

@@ -74,6 +74,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.isEmpty && default.head.nonEmpty => default.head.get.asRight
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to avoid the .get if possible. Something like:

Suggested change
case Some(head) if head.isEmpty && default.head.nonEmpty => default.head.get.asRight
case Some(head) if head.isEmpty && default.head.nonEmpty => default.head.toRight(new DecoderError("Should not happen", row.line))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for / respect of default arguments in case class decoders?
2 participants