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@6b9a789

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

// 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(101f83b5-3854-4005-b71a-af31c4dd995b),2021-02-03T15:51:55.292701Z), 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,101f83b5-3854-4005-b71a-af31c4dd995b,2021-02-03T15:51:55.292701Z
// 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(101f83b5-3854-4005-b71a-af31c4dd995b),2021-02-03T15:51:55.292701Z), Bar(Boo,7,6,None,+1000000000-12-31T23:59:59.999999999Z)))