kantan.csv
kantan.csv copied to clipboard
Custom CellCodec for Option
Hi! I'm trying to create a CellCodec
that encodes/decodes Option[E]
values in a way that:
- Option[E].empty values are encoded to
-
instead of an empty cell -
-
values are decoded asOption[E].empty
Going through the source code, I imagined that it would be possible to do something using the already existent optionEncoder
and optionDecoder
, but I couldn't figure out how exactly to do it. Can you guys tell me if it is possible and give me any advice on how to do it?
Thanks!
I'm so very sorry - I had answered this the day you posted it, but apparently I had no coverage and never realised my answer never actually got posted. I hope you managed to work it out on your own. Otherwise, I fear I might have lost a user, but here's a possible solution just in case somebody else needs it:
scala> implicit def opt[E: CellEncoder]: CellEncoder[Option[E]] = CellEncoder.from {
| case Some(e) => CellEncoder[E].encode(e)
| case None => "-"
| }
opt: [E](implicit evidence$1: kantan.csv.CellEncoder[E])kantan.csv.CellEncoder[Option[E]]
scala> CellEncoder[Option[Int]].encode(Some(2))
res1: String = 2
scala> CellEncoder[Option[Int]].encode(None)
res2: String = -