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
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)
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
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.
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.
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.