I have a big flat denormalized csv file containing multiples objects on single row like this:
a1, a2, a3, b1, b2, b3 ...
...
and I have objects:
case class A(a1: Int, a2: String, a3: Float)
case class B...
...
and the legacy is writing complicated adapters for extracting each class. I recently read some talks about shapeless and I know I can solve this with generic programming with shapeless.
and there's even a csv parser example, perfect. my thoughts would be:
- parse the csv into List[String]
- filter the ListString with object field information
- using the filtered ListString and fed it with the Csv Parser example
- thus I can extract multiple objects a row from the csv file.
problems I have:
I am still on scala 2.10, I seem to have configured the compiler plugin correctly( egg. mvn clean install works properly). but intellij fails to compile occasionally, throws exceptions.
<groupId>org.scala-lang.plugins</groupId> <artifactId>macro-paradise_2.10</artifactId> <version>2.0.0-SNAPSHOT</version>
this code is from
shapeless
exampleimplicit def deriveHConsOption[V, T <: HList]( implicit scv: Lazy[CSVConverter[V]], sct: Lazy[CSVConverter[T]] ):CSVConverter[Option[V] :: T] = new CSVConverter[Option[V] :: T] { override def from(s: String): Try[shapeless.::[Option[V], T]] = s.span(_ != ',')
however I'm having following compiler errors:
Error:(70, 28) wrong number of type arguments for ::, should be 1
):CSVConverter[Option[V] :: T] = new CSVConverter[Option[V] :: T] { ^
my attempts to filter the csv using shapeless:
//code to filter and extract one object def extractCSVColumnsAndParse[T]: val labl = LabelledGeneric[T] val keys = Keys[labl.Repr].apply val keyNames = keys.toList.map(_.name)
however it seems T could only be concrete Class Type
Error:(86, 35) could not find implicit value for parameter lgen: shapeless.LabelledGeneric[T] val labl = LabelledGeneric[T]
shapeless.::
, otherwise, it uses the list constructor, that only has one type parameter. – Cyrille Corpet