简体   繁体   中英

Generic way of reading CSV of class hierarchy in scala

I know there are various libraries around to read CSV in scala. I have tried the shapeless way , but I am having trouble reading csv in generic way for a hierarchy . For eg I need something like this :

abstract class A
case class ChildOneOfA(i:Int,s:String) extends A
case class ChildTwoOfA(i:Int,os:Option[String]) extends A`



//  Requires generic implementation of T which is subtype of A

def genericCSVReader[T]:GenericCsvRecordReader[T] = {
//Generic implementation to return csv record iterator/reader
}

First of all, your example is a bit weird - each row is either an int and a string or an int and an optional string? This is the same thing as saying that each row is an int and an optional string, you don't need the two alternatives for that.

But for a useful example, let's say that each row is either an int and a boolean or an int and an optional float (and let's assume that you don't want to use Eiter , \\/ or Xor to represent the disjunction):

sealed trait A
case class Alternative1(i: Int, b: Boolean) extends A
case class Alternative2(i: Int, of: Option[Float]) extends A

Using kantan.csv and its shapeless module, you can actually parse that pretty trivially:

import kantan.csv.ops._
import kantan.csv.generic._

"""1,true
2,3.14
3,""".asCsvReader[A](',', false).foreach(println _)

asCsvReader is brought in scope by the import statement. It takes a type parameter, the type as which to decode each row, and two value parameters, the column separator and a flag indicating whether the first row should be skipped.

This code outputs:

Success(Alternative1(1,true))
Success(Alternative2(2,Some(3.14)))
Success(Alternative2(3,None))

Note that:

  • the return value of asCsvReader is an Iterator like structure, which means you never need to load the whole CSV in memory.
  • each row is wrapped in either a Success or Failure , and decoding never throws (unless you need it do, in which case you can use asUnsafeCsvReader ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM