gnieh / fs2-data

streaming data parsing and transformation library
https://fs2-data.gnieh.org
Apache License 2.0
152 stars 27 forks source link

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

Closed vreuter closed 3 months ago

vreuter commented 4 months ago

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?

satabin commented 4 months ago

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?

vreuter commented 4 months ago

OK, thank you @satabin !

ybasket commented 4 months ago

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!

vreuter commented 4 months ago

Many thanks in advance @ybasket !