fs2-data icon indicating copy to clipboard operation
fs2-data copied to clipboard

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

Open vreuter opened this issue 1 year ago • 4 comments

Hello,

I'm new to fs2-data and am confused by the last section of the documentation for the generic module. Specifically, it's mentioned that...

Both automatic and semi-automatic decoders support also default values when decoding

...but then it looks in fact like if a row's missing a value in a slot that could be populated by a default value when building an instance of the target case class, that value's not used and instead a DecoderError arises. Here's the snippet:

import fs2.data.csv.generic.auto._

case class MyRowDefault(i: Int = 42, j: Int, s: String)

val decoded = stream.through(decodeUsingHeaders[MyRowDefault]())
// decoded: Stream[[x]Fallible[x], MyRowDefault] = Stream(..)
decoded.compile.toList
// res11: Either[Throwable, List[MyRowDefault]] = Left(
//   value = fs2.data.csv.DecoderError: unable to decode '' as an integer in line 3
// )

That doesn't feel like "support" for default values, but rather something like "tolerance" of their existence and incompatibility with an encounter of a row missing a value in a slot for which there's a default. Is the example actually the intended behavior?

vreuter avatar Jun 13 '24 08:06 vreuter

Hi, thanks for reporting.

This looks like a regression. I will have a look at it. Or maybe @ybasket knows what could have caused this?

satabin avatar Jun 15 '24 15:06 satabin

OK, thank you @satabin !

vreuter avatar Jun 15 '24 17:06 vreuter

I can confirm this is a bug, default values are only working partially. If the missing field has type String, it succeeds, see https://scastie.scala-lang.org/Hu5LZ0nIRyC18LqFkC8nWQ

But it fails with fields of type Int. I'm not yet sure whether that's a regression or whether it just never came up, but I'll try to find time for attempting a fix during the next days. Thank you for reporting!

ybasket avatar Jun 17 '24 13:06 ybasket

Many thanks in advance @ybasket !

vreuter avatar Jun 17 '24 13:06 vreuter