kantan.csv icon indicating copy to clipboard operation
kantan.csv copied to clipboard

CellDecoder[LocalDate] not allow white space.

Open keiSunagawa opened this issue 5 years ago • 5 comments

this code become to decode error.

import kantan.csv.java8._
import kantan.csv._
import kantan.csv.ops._

val x =
  """a, 2019-10-12""".stripMargin.readCsvRow[(String, LocalDate)](rfc)

println(x) // Left(TypeError: ' 2019-10-12' is not a valid LocalDate)

CellDecoder[LocalDate] can only parse for a,2019-10-12 or a, "2019-10-12".

but, CellDecoder[Long] can parse for a, 123, because StringCodec[Long] call to String#trim ref https://github.com/nrinaudo/kantan.codecs/blob/master/core/shared/src/main/scala/kantan/codecs/strings/codecs.scala#L234

why do not apply some solution for LocalDate?

keiSunagawa avatar Feb 01 '20 04:02 keiSunagawa

No reason other than I forgot, I'm afraid. I'll fix that in a future release, but that doesn't help you right now.

If you've already dug through the code enough to reach kantan.codecs, you probably worked out the workaround for yourself, but just in case: you can provide your own CellDecoder[LocalDate] by grabbing the StringDecoder[LocalDate], contramapping a trim into it and tagging it to the right phantom type.

nrinaudo avatar Feb 01 '20 21:02 nrinaudo

thanks. I will try 👍

keiSunagawa avatar Feb 03 '20 17:02 keiSunagawa

Reopening because I do want to fix this properly :)

nrinaudo avatar Feb 03 '20 17:02 nrinaudo

Hello! Sorry to come here for support. I'm facing a similar issue with an enumeratum and I'm trying to implement a similar workaround.

I succeeded by changing the string decoder globally, but that's not what I really want.

My two questions are:

  • How do you "grab" decoders that are otherwise implicitly generated (such as the StringDecoder[LocalDate] you mention above)
  • What do you mean by "tagging it to the right phantom type"

Thanks a lot!

Cheers

rgugliel avatar Apr 24 '20 07:04 rgugliel

Well, implicitly allows you to summon implicit values, such as:

implicitly[StringDecoder[LocalDate]]

Decoders are tagged with a type - kantan.csv.codecs for CSV decoders, so to turn a StringDecoder[LocalDate] into a CellDecoder[LocalDate], you can call .tag[kantan.csv.codecs].

nrinaudo avatar Apr 24 '20 08:04 nrinaudo