A utility for parsing comma-separated values (CSV) sources. The source is assumed to be RFC 4180 conform, although some aspects of its format are configurable.
The parser may be created with default configuration:
val parser = CSVParser[IO]
or through CSVParser.config helper function to set custom properties:
val parser = CSVParser.config.fieldDelimiter(';').parser[IO]
Actual parsing is done through one of the 3 groups of methods:
parse to transform a stream of characters (or stream of strings in case of parseS) into records and process data in a functional way, which is the recommended approach,
get to fetch whole source data at once into a list,
process to deal with individual records through a callback function.
This parser is normally used with stream fetching data from some external source, 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 suspended execution, which requires given instance of cats.effect.Sync.
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. There is also supporting class CSVParser#Async available, which provides method for asynchronous callbacks.
Type parameters
F
the effect type, with a type class providing support for suspended execution (typically cats.effect.IO) and logging (provided internally by spata)
Value parameters
config
the configuration for CSV parsing (delimiters, header presence etc.)
Fetches requested number of CSV records into a list.
Fetches requested number of CSV records into a list.
This functions stops processing source data as soon as the limit is reached. It mustn't be called twice for the same data source however - first call may consume more elements than requested, leaving the source pointer at an unpredictable position.
Transforms stream of characters representing CSV data into records. This function is intended to be used with fs2.Stream.through. The transformed fs2.Stream allows further input processing in a very flexible, purely functional manner.
Transforms stream of characters representing CSV data into records. This function is intended to be used with fs2.Stream.through. The transformed fs2.Stream allows further input processing in a very flexible, purely functional manner.
Processing of data sources may be achieved by combining this function with io.Reader, e.g.:
Transforms stream of strings representing CSV data into records. This function is intended to be used with fs2.Stream.through. The transformed fs2.Stream allows further input processing in a very flexible, purely functional manner.
Transforms stream of strings representing CSV data into records. This function is intended to be used with fs2.Stream.through. The transformed fs2.Stream allows further input processing in a very flexible, purely functional manner.
If you would like to work with spata's io.Reader, use the parse methods, which consumes 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 to be converted by fs2.text.decodeWithCharset).
Processes each CSV record with provided callback functions to execute some side effects. Stops processing input as soon as the callback function returns false or stream is exhausted.
Processes each CSV record with provided callback functions to execute some side effects. Stops processing input as soon as the callback function returns false or stream is exhausted.
Value parameters
cb
the callback function to operate on each CSV record and produce some side effect. It should return true to continue the process with next record or false to stop processing the input.
stream
the source stream containing CSV content
Attributes
Returns
unit effect, used as a handle to trigger evaluation