I'm trying to create a parser of CSV files using Scala into a case class and I'm trying to make it generic using Shapeless.
I want my parser to allow the user to specify an extraction function extract: CsvRow => String
rather than having a a 1-to-1 correspondence and typeclasses for specific field types because the files I'm parsing are in different formats and a parsing operation that works for one file doesn't work for another.
I thought if I can avoid this step by first doing a "dumb" parsing and then applying the conversion functions but at the end of the day I'm simply shifting where this problem needs to be solved.
After this I want to convert my resulting HList into a tuple and do a mapN to create a new instance of a case class
I tried several strategies but I always have something that doesn't work. I found very helpful this specific answer that I adapted below.
At the moment I keep getting
could not find implicit value for parameter tupler
when I try to convert the resulting HList into a tuple. I notice that my resulting HList terminates in HNil.type
instead of HNil
that is what I would expect
UPDATE: I added a more complete code on Scastie with more comments and data.
Versions:
import CsvDefinitions._
import CsvTypeParser._
import CsvConverter._
import cats.data._
import cats.implicits._
import scala.util.Try
import shapeless._
import shapeless.ops.hlist.RightFolder
import shapeless.syntax.std.tuple._
object CsvDefinitions {
type CsvRow = List[String]
type CsvValidated[A] = ValidatedNec[Throwable, A]
// A parser is a function that given a row returns a validted result
type CsvRowParser[A] = Kleisli[CsvValidated, CsvRow, A]
}
trait CsvTypeParser[A] {
def parseCell(cell: String): CsvValidated[A]
}
object CsvTypeParser {
def parse[A](extract: CsvRow => String)(implicit parser: CsvTypeParser[A]): CsvRowParser[A] =
Kleisli { row =>
val extracted = Try(extract(row)).toEither.toValidatedNec
val parsed = parser.parseCell _
(extracted) andThen parsed
}
def apply[A](f: String => A): CsvTypeParser[A] = new CsvTypeParser[A] {
def parseCell(cell: String): CsvValidated[A] = Try(f(cell)).toEither.toValidatedNec
}
implicit val stringParser: CsvTypeParser[String] = CsvTypeParser[String] {
_.trim
}
implicit val intParser: CsvTypeParser[Int] = CsvTypeParser[Int] {
_.trim.toInt
}
implicit val doubleParser: CsvTypeParser[Double] = CsvTypeParser[Double] {
_.trim.toDouble
}
}
object CsvConverter {
// The following has been adapted from https://stackoverflow.com/a/25316124:
private object ApplyRow extends Poly2 {
// The "trick" here is to pass the row as the initial value of the fold and carry it along
// during the computation. Inside the computation we apply a parser using row as parameter and
// then we append it to the accumulator.
implicit def aDefault[T, V <: HList] = at[CsvRowParser[T], (CsvRow, V)] {
case (rowParser, (row, accumulator)) => (row, rowParser(row) :: accumulator)
}
}
def convertRowGeneric[
HP <: HList, // HList Parsers
HV <: HList]( // HList Validated
input: HP,
row: CsvRow)(
implicit
// I tried to use the RightFolder.Aux reading https://stackoverflow.com/a/54417915
folder: RightFolder.Aux[
HP, // Input type
(CsvRow, HNil.type), // Initial value of accumulator
ApplyRow.type, // Polymorphic function
(CsvRow, HV) // Result type
]
): HV = {
input.foldRight((row, HNil))(ApplyRow)._2
}
}
// Case class containing the final result of the conversion
case class FinalData(id: Int, name: String, score: Double)
object Main extends App {
// Definition of parsers and how they obtain the value to parse
val parsers =
parse[Int ](r => r(0)) :: // Extract field 0 and convert it to Int
parse[String](r => r(1)+" "+r(2)) :: // Get field 1 and 2 together
parse[Double](r => r(3).trim) :: // Trim field 3 before converting to double
HNil
// One line in the CSV file
val row = List("123", "Matt", "Smith", "45.67")
val validated = convertRowGeneric(parsers, row)
println(validated)
// Could not find implicit value for parameter tupler
// The "validated" HList terminates with HNil.type
val finalData = validated
.tupled
.mapN(FinalData)
println(finalData)
}
Fix convertRowGeneric
(replace type HNil.type
with HNil
and value HNil
with ascription HNil: HNil
)
def convertRowGeneric[
HP <: HList, // HList Parsers
HV <: HList]( // HList Validated
input: HP,
row: CsvRow)(
implicit
folder: RightFolder.Aux[
HP, // Input type
(CsvRow, HNil), // Initial value of accumulator
ApplyRow.type, // Polymorphic function
(CsvRow, HV) // Result type
]
): HV = {
input.foldRight((row, HNil: HNil))(ApplyRow)._2
}