nrinaudo / kantan.csv

CSV handling library for Scala
http://nrinaudo.github.io/kantan.csv/
Apache License 2.0
345 stars 36 forks source link

CellDecoder[LocalDate] not allow white space. #217

Open keiSunagawa opened 4 years ago

keiSunagawa commented 4 years ago

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?

nrinaudo commented 4 years ago

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.

keiSunagawa commented 4 years ago

thanks. I will try 👍

nrinaudo commented 4 years ago

Reopening because I do want to fix this properly :)

rgugliel commented 4 years ago

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:

Thanks a lot!

Cheers

nrinaudo commented 4 years ago

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].