CSVRenderer

info.fingo.spata.CSVRenderer
See theCSVRenderer companion object
final class CSVRenderer[F[_]](config: CSVConfig)(implicit evidence$1: RaiseThrowable[F])

A utility for rendering data to CSV representation.

The renderer may be created with default configuration:

val renderer = CSVRenderer[IO]

or through CSVRenderer.config helper function to set custom properties:

val renderer = CSVRenderer.config.fieldDelimiter(';').renderer[IO]

Actual rendering is done through one of the 2 groups of methods:

  • render to transform a stream of records into stream of characters (or strings in case of renderS), which represent full CSV content.
  • rows to convert records to strings representing individual CSV rows.

This renderer is normally used with stream supplying data to some external destination, so its computations are wrapped for deferred evaluation into an effect F, e.g. cats.effect.IO. Basic parsing does not impose any special requirements on F, except its support for raising and handling errors, which requires given instance of fs2.RaiseThrowable which effectively means cats.ApplicativeError.

To trigger evaluation, one of the unsafe operations on F has to be called. Their exact form depends on actual effect in use (e.g. cats.effect.IO.unsafeRunSync).

No method in this class does context (thread) shift and by default they execute synchronously on current thread. Concurrency or asynchronous execution may be introduced through various fs2.Stream methods.

Type parameters

F

the effect type, with a type class providing support for raising and handling errors

Value parameters

config

the configuration for CSV rendering (delimiters, header presence etc.)

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def render(header: Header): (F, Record) => Char

Transforms stream of records into stream of characters representing CSV data. This function is intended to be used with fs2.Stream.through.

Transforms stream of records into stream of characters representing CSV data. This function is intended to be used with fs2.Stream.through.

The output is basically RFC 4180 conform, with the possibility to have custom delimiters and quotes, as configured by CSVConfig.

If any record does not have the field required by header, transformation will cause an error.HeaderError, to be handled with fs2.Stream.handleErrorWith. If not handled, the exception will be thrown.

Value parameters

header

header to be potentially written into target stream and used to select required data from records

Attributes

Returns

a pipe to converter Records into scala.Chars

See also

FS2 documentation for further guidance.

Example
val input: Stream[IO, Record] = ???
val renderer: CVSRenderer[IO] = CSVRenderer.config.escapeSpaces.renderer[IO]
val header: Header = Header("date", "location", "temperature")
val output: Stream[IO, Char] = input.through(renderer.render(header))
val eff: Stream[IO, Unit] = output.through(Writer[IO].write("output.csv"))
def render: (F, Record) => Char

Transforms stream of records into stream of characters representing CSV data. Determines header from first record in stream. This function is intended to be used with fs2.Stream.through.

Transforms stream of records into stream of characters representing CSV data. Determines header from first record in stream. This function is intended to be used with fs2.Stream.through.

If field content differ (they have fields with different names), transformation will cause an error.HeaderError, to be handled with fs2.Stream.handleErrorWith.

Attributes

Returns

a pipe to converter Records into scala.Chars

See also

render with explicit header for more information.

def renderS(header: Header): (F, Record) => String

Transforms stream of records into stream of strings representing CSV data. This function is intended to be used with fs2.Stream.through.

Transforms stream of records into stream of strings representing CSV data. This function is intended to be used with fs2.Stream.through.

If you would like to work with spata's io.Writer, use one of the render methods, which produce a stream of characters rather than a stream of strings. This method is better suited to work with methods from fs2.io.file.Files, which operates on strings (or bytes when converted by fs2.text.encode).

Value parameters

header

header to be potentially written into target stream and used to select required data from records

Attributes

Returns

a pipe to converter Records into Strings

See also

render for more information.

Example
val input: Stream[IO, Record] = ???
val renderer: CVSRenderer[IO] = CSVRenderer.config.escapeSpaces.renderer[IO]
val header: Header = Header("date", "location", "temperature")
val output: Stream[IO, String] = input.through(renderer.renderS(header))
val eff: Stream[IO, Unit] = output.through(Files[IO].writeUtf8(Path("output.csv")))
def renderS: (F, Record) => String

Transforms stream of records into stream of strings representing CSV data. Determines header from first record in stream. This function is intended to be used with fs2.Stream.through.

Transforms stream of records into stream of strings representing CSV data. Determines header from first record in stream. This function is intended to be used with fs2.Stream.through.

Attributes

Returns

a pipe to converter Records into Strings

See also

renderS with explicit header for more information.

def rows: (F, Record) => String

Transforms stream of records into stream of CSV rows.

Transforms stream of records into stream of CSV rows.

This method accesses records values by index. It does not use field names and does not require header to exist. It does not require records to be the same length, either. With records of different size the resulting output may not form proper CSV content.

Records delimiters are to be added to the output. To get full CSV content, Stream.intersperse should be called:

 val renderer: CSVRenderer[IO] = CSVRenderer[IO]
 val in: Stream[IO, Record] = ???
 val out: Stream[IO, String] = in.through(renderer.rows).intersperse("\n")

Nevertheless record delimiter is escaped in content, so it has to be set accordingly in config.

This method does not put header in output stream, regardless of CSVConfig.hasHeader setting.

Attributes

Returns

a pipe to converter Records into Strings