fingo / spata Goto Github PK
View Code? Open in Web Editor NEWFunctional, stream-based CSV processor for Scala
License: Apache License 2.0
Functional, stream-based CSV processor for Scala
License: Apache License 2.0
I have a use case at work where I parse CSV into my data type. I make use of the Record API to extract certain fields that need to go through additional runtime validations before calling record.to[MyCaseClass].
Is it possible to expose some failure constructors (like Decoded.fail(message, record)) that would allow the line number and other relevant info to be preserved in the exception or should I be utilizing a different API in order to do this? Thank you very much
While parsing the following source:
header1,header2<CRLF>
value;<CRLF>
spata reports:
info.fingo.spata.error.StructureException: Error occurred at row 1 (line 2) while parsing CSV source. Number of values doesn't match header size or previous records.
This is a correct CSV file and shouldn't report any errors.
Rendering a CSV for consumption via http4s requires a Stream[F, Byte]
. However, the current API only exposes a render method that builds Stream[F, Char].
The only safe way to convert that to Byte is to do something like:
.render
.through(fs2.text.char2string)
.through(fs2.text.utf8.encode)
Can we refactor this to expose a renderString
method. We already have a Stream[String] up to the last line but then convert to chars.
def render: Pipe[F, Record, Char] = (in: Stream[F, Record]) =>
def loop(in: Stream[F, Record], header: Header): Pull[F, Either[HeaderError, String], Unit] =
in.pull.uncons.flatMap:
case Some((rc, t)) => Pull.output(rc.map(renderRow(_, header))) >> loop(t, header)
case None => Pull.done
val pull = in.pull.uncons1.flatMap:
case Some((r, t)) =>
val headerRow = if config.hasHeader then Pull.output1(renderHeader(r.header)) else Pull.pure(())
val firstRow = Pull.output1(renderRow(r, r.header))
headerRow >> firstRow >> loop(t, r.header)
case None => Pull.done
pull.stream.through(toChars)
private def toChars: Pipe[F, EHE[String], Char] = (in: Stream[F, EHE[String]]) =>
in.rethrow
.intersperse(srd)
.map(s => Chunk.array[Char](s.toCharArray))
.flatMap(Stream.chunk)
Hello! Was just wondering if there were plans to publish versions for fs2-3.x.x and cats-effect-3, or if contributions would be welcome for the same. We're migrating our systems and spata is one of our last holdouts.
Header mapping, set through CSVConfig.mapHeader
is ignored by CSVRenderer
(although it works forCSVParser
).
Regardless of the new names provided through mapHeader
, the actual CSV file has the original header (set directly or obtained from the first record). E.g. for the snippet in readme.md:
val stream: Stream[IO, Record] = ???
val renderer: CSVRenderer[IO] =
CSVRenderer.config.mapHeader(Map("tempMax" -> "max temparature", "tempMin" -> "min temparature")).renderer[IO]
val frosty: Stream[IO, Char] = stream.filter(_.get[Double]("minTemp").exists(_ < 0)).through(renderer.render)
the resulting header is
tempMax;tempMin
instead of
max temperature;min temperature
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.