cormorant Build Status Maven Central

Project Goals

Cormorant is a CSV Library for Scala

Quick Start

To use cormorant in an existing SBT project with Scala 2.12 or a later version, add the following dependencies to your build.sbt depending on your needs:

libraryDependencies ++= Seq(
  "io.chrisdavenport" %% "cormorant-core"     % "<version>",
  "io.chrisdavenport" %% "cormorant-generic"  % "<version>",
  "io.chrisdavenport" %% "cormorant-parser"   % "<version>",
  "io.chrisdavenport" %% "cormorant-fs2"      % "<version>",
  "io.chrisdavenport" %% "cormorant-http4s"   % "<version>",
  "io.chrisdavenport" %% "cormorant-refined"  % "<version>"
)

First the imports

import io.chrisdavenport.cormorant._
import io.chrisdavenport.cormorant.generic.semiauto._
import io.chrisdavenport.cormorant.parser._
import io.chrisdavenport.cormorant.implicits._
import cats.implicits._
import java.util.UUID
import java.time.Instant

Then some basic operations

case class Bar(a: String, b: Int, c: Long, d: Option[UUID], e: Instant)
// defined class Bar

implicit val lr: LabelledRead[Bar] = deriveLabelledRead
// lr: io.chrisdavenport.cormorant.LabelledRead[Bar] = io.chrisdavenport.cormorant.generic.semiauto$$anon$4@19135e4d

implicit val lw: LabelledWrite[Bar] = deriveLabelledWrite
// lw: io.chrisdavenport.cormorant.LabelledWrite[Bar] = io.chrisdavenport.cormorant.generic.semiauto$$anon$2@4c23ecc5

// A List of A given derived type
// Don't use Instant.Now or UUID.randomUUID in pure code in the real world please.
val l : List[Bar] = List(
  Bar("Yellow", 3, 5L, UUID.randomUUID.some, Instant.now),
  Bar("Boo", 7, 6L, None, Instant.MAX)
)
// l: List[Bar] = List(Bar(Yellow,3,5,Some(a9c6cb83-6721-4b15-838f-d22a21231160),2019-10-15T13:38:02.718Z), Bar(Boo,7,6,None,+1000000000-12-31T23:59:59.999999999Z))

// From Type to String
val csv = l.writeComplete.print(Printer.default)
// csv: String =
// a,b,c,d,e
// Yellow,3,5,a9c6cb83-6721-4b15-838f-d22a21231160,2019-10-15T13:38:02.718Z
// Boo,7,6,,+1000000000-12-31T23:59:59.999999999Z

// From String to Type
val decoded : Either[Error, List[Bar]] = {
  parseComplete(csv).leftWiden[Error]
  .flatMap(_.readLabelled[Bar].sequence)
}
// decoded: Either[io.chrisdavenport.cormorant.Error,List[Bar]] = Right(List(Bar(Yellow,3,5,Some(a9c6cb83-6721-4b15-838f-d22a21231160),2019-10-15T13:38:02.718Z), Bar(Boo,7,6,None,+1000000000-12-31T23:59:59.999999999Z)))