-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: main
Are you sure you want to change the base?
Conversation
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
} | ||
|
||
pureTest("case classes should be handled properly with default string value and empty cell") { | ||
expect(testDefaultsDecoder(csvRowEmptyJ) == Right(TestDefaults(1, "foo"))) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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:
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)) |
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