Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/zamblauskas/scala-csv-parser

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/zamblauskas/scala-csv-parser

  • v0.13.1
  • Source
  • Go
  • Socket score

Version published
Created
Source

Build Status Bintray Download Codacy Badge Codacy Coverage Badge

About

CSV parser library for Scala. Easiest way to convert CSV string representation into a case class.

Usage

import zamblauskas.csv.parser._

case class Person(name: String, age: Int, city: Option[String])

val csv = """
            |name,age,height,city
            |Emily,33,169,London
            |Thomas,25,,
          """.stripMargin

val result = Parser.parse[Person](csv)

result shouldBe Right(List(Person("Emily",33,Some("London")), Person("Thomas",25,None)))

ColumnReads[T]

Example above used a macro generated ColumnReads[Person]. You can define one manually if the generated one does not fit your use case (e.g. column names differ from case class parameter names).

This is identical to what the macro generates for a Person case class:

import zamblauskas.csv.parser._
import zamblauskas.functional._

case class Person(name: String, age: Int, city: Option[String])

implicit val personReads: ColumnReads[Person] = (
  column("name").as[String]    and
  column("age").as[Int]        and
  column("city").asOpt[String]
)(Person)

val csv = """
            |name,age,height,city
            |Emily,33,169,London
            |Thomas,25,,
          """.stripMargin

val result = Parser.parse[Person](csv)

result shouldBe Right(List(Person("Emily",33,Some("London")), Person("Thomas",25,None)))

Alternative column names

If columns have two or more alternative names (e.g. in different languages), you can use an or combinator.

import zamblauskas.csv.parser._
import zamblauskas.functional._
import Parser.parse

case class Person(age: Int, city: String)

implicit val personReads: ColumnReads[Person] = (
  (column("age").as[Int] or column("alter").as[Int]) and
  (column("city").as[String] or column("stadt").as[String])
)(Person)

val englishCsv =
  """
    |age,city
    |33,London
  """.stripMargin

val germanCsv =
  """
    |alter,stadt
    |33,London
  """.stripMargin

parse[Person](englishCsv) shouldBe parse[Person](germanCsv)

Alternative reads

Example above can be rewritten to use alternative ColumnReads instead of alternative column names on single ColumnReads.

import zamblauskas.csv.parser._
import zamblauskas.functional._
import Parser.parse

case class Person(age: Int, city: String)

val englishPersonReads: ColumnReads[Person] = (
  column("age").as[Int] and
  column("city").as[String]
)(Person)

val germanPersonReads: ColumnReads[Person] = (
  column("alter").as[Int] and
  column("stadt").as[String]
)(Person)

implicit val personReads = englishPersonReads or germanPersonReads

val englishCsv =
  """
    |age,city
    |33,London
  """.stripMargin

val germanCsv =
  """
    |alter,stadt
    |33,London
  """.stripMargin

parse[Person](englishCsv) shouldBe parse[Person](germanCsv)

SBT dependency

Package is available at Bintray. Check for the latest version and add to your build.sbt:

resolvers += Resolver.bintrayRepo("zamblauskas", "maven")

libraryDependencies += "zamblauskas" %% "scala-csv-parser" % "<latest_version>"

FAQs

Package last updated on 23 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc