Probably a way to handle this, but getting implicit errors when attempting to use kantan asCsvReader
and asCsvWriter
against generics. I suspect this is either a limitation or there is some niceness I can try with implicits to move forward, but right now my solution is to simply leave the trait methods unimplemented and do so in the implementing case classes.
If anyone has a better way of doing this, I'm all ears.
Code follows errors.
Information:6/25/16, 5:24 PM - Compilation completed with 6 errors and 0 warnings in 1s 749ms
...
Error:(24, 40) could not find implicit value for evidence parameter of type kantan.csv.RowEncoder[T]
val writer = out.asCsvWriter[T](',', line.headers)
^
Error:(24, 40) not enough arguments for method asCsvWriter: (implicit evidence$1: kantan.csv.RowEncoder[T], implicit oa: kantan.csv.CsvOutput[java.io.ByteArrayOutputStream], implicit e: kantan.csv.engine.WriterEngine)kantan.csv.CsvWriter[T].
Unspecified value parameters evidence$1, oa, e.
val writer = out.asCsvWriter[T](',', line.headers)
^
Error:(45, 66) could not find implicit value for evidence parameter of type kantan.csv.RowDecoder[T]
val results = getClass.getResource(source).asCsvReader[T](',', false)
^
Error:(45, 66) not enough arguments for method asCsvReader: (implicit evidence$1: kantan.csv.RowDecoder[T], implicit ia: kantan.csv.CsvInput[java.net.URL], implicit e: kantan.csv.engine.ReaderEngine)kantan.csv.CsvReader[kantan.csv.ReadResult[T]].
Unspecified value parameters evidence$1, ia, e.
val results = getClass.getResource(source).asCsvReader[T](',', false)
^
Error:(54, 65) could not find implicit value for evidence parameter of type kantan.csv.RowDecoder[T]
val results : List[ReadResult[T]] = line.readCsv[List,T](sep, header)
^
Error:(54, 65) not enough arguments for method readCsv: (implicit evidence$3: kantan.csv.RowDecoder[T], implicit ia: kantan.csv.CsvInput[String], implicit e: kantan.csv.engine.ReaderEngine, implicit cbf: scala.collection.generic.CanBuildFrom[Nothing,kantan.csv.ReadResult[T],List[kantan.csv.ReadResult[T]]])List[kantan.csv.ReadResult[T]].
Unspecified value parameters evidence$3, ia, e...
val results : List[ReadResult[T]] = line.readCsv[List,T](sep, header)
^ ^
import java.io.ByteArrayOutputStream
import kantan.csv._
import kantan.csv.ops._
import kantan.csv.generic._
/**
* Created by revprez on 6/25/16.
*/
trait CsvLine {
def headers : List[String]
}
trait CsvParsable[T <: CsvLine] {
def line : T
def toCsvString(header: Boolean = true) : String = {
val out : ByteArrayOutputStream = new ByteArrayOutputStream()
val writer = out.asCsvWriter[T](',', line.headers)
writer.write(line).close
val string = new String(out.toByteArray)
out.close
header match {
case true => return string.stripLineEnd
case false => {
return string.split("\\r?\\n")(1).stripLineEnd
}
}
}
}
trait CsvParser[T] {
implicit val codec = scala.io.Codec.ISO8859
def parseFile(source: String, sep : Char = ',', header : Boolean = true) : Stream[T] = {
val results = getClass.getResource(source).asCsvReader[T](',', false)
.toStream
.filter( _.isSuccess )
.map(_.get)
return results
}
def parse(line : String, sep : Char = ',', header : Boolean = false) : Option[T] = {
val results : List[ReadResult[T]] = line.readCsv[List,T](sep, header)
results match {
case Nil => None
case or::ors if (or.isFailure) => None
case or::ors if (or.isSuccess) => Some(or.get)
}
}
}