CSV record representation. A record is basically a map from string to string. Values are indexed by header provided explicitly or read from header row in source data. If no header is provided nor can be scanned from source, a tuple-style header "_1"
, "_2"
etc. is generated.
Position information is always available for parsed data - for records created through CSVParser. It is missing for records created explicitly in application code (in order to be rendered to CSV).
Value parameters
- hdr
-
indexing header (field names)
- position
-
record position in source data
- values
-
core record data
Attributes
- Companion
- object
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
Members list
Type members
Classlikes
Intermediary to delegate parsing to in order to infer type of formatter used by parser. Provides exception-free parsing method.
Intermediary to delegate parsing to in order to infer type of formatter used by parser. Provides exception-free parsing method.
Type parameters
- A
-
target type for parsing
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Value members
Concrete methods
Creates new record with value at given key updated by provided function. The function receives existing, typed value as an argument.
Creates new record with value at given key updated by provided function. The function receives existing, typed value as an argument.
This method may fail if incorrect key is provided or existing value cannot be parsed to requested type.
The new record shares the header with the old one.
Type parameters
- A
-
type of existing value
- B
-
type of new value
Value parameters
- f
-
the function to be applied to existing value
- key
-
the key (name) of field to be updated
Attributes
- Returns
-
a new record with updated value or an error
- Example
-
val kmToMile = record.altered[Double,Double]("distance")(km => 0.621371 * km)
Gets field value.
Gets field value.
Value parameters
- key
-
the key of retrieved field
Attributes
- Returns
-
field value in its original, string format if correct key is provided or
None
otherwise.
Gets field value.
Gets field value.
Value parameters
- idx
-
the index of retrieved field, starting from
0
.
Attributes
- Returns
-
field value in original, string format or
None
if index is out of bounds.
Safely gets typed record value.
Safely gets typed record value.
Parsers for basic types are provided through StringParser object.
To parse optional values provide Option[?]
as type parameter. Parsing empty value to simple type will result in an error.
If wrong header key is provided this function will return Left[error.HeaderError,?]
. If parsing fails Left[error.DataError,?]
will be returned.
Type parameters
- A
-
type to parse the field to
Value parameters
- key
-
the key of retrieved field
Attributes
- Returns
-
either parsed value or an error
- See also
-
StringParser for information on providing custom parsers.
- Note
-
When relying on default string parsers, this function assumes "standard" string formatting, without any locale support, e.g. point as decimal separator or ISO date and time formats. Use get or provide own parser if more control over source format is required.
Safely gets typed record value.
Safely gets typed record value.
Type parameters
- A
-
type to parse the field to
Value parameters
- idx
-
index of retrieved field
Attributes
- Returns
-
either parsed value or an error
- See also
-
get by key for details.
Safely gets typed record value.
Safely gets typed record value.
The combination of get
, Field constructor and apply
method allows value retrieval in following form:
val date: Decoded[LocalDate] = record.get[LocalDate]("key", DateTimeFormatter.ofPattern("dd.MM.yy"))
(type of formatter is inferred based on target type).
Parsers for basic types are available through StringParser object. Additional ones may be provided as given instances.
To parse optional values provide Option[?]
as type parameter. Parsing empty value to simple type will result in an error.
Type parameters
- A
-
type to parse the field to
Attributes
- Returns
-
intermediary to retrieve value according to custom format
- See also
-
StringParser for information on providing custom parsers.
Creates a builder, initialized with content of this record. A builder may be used to enhance or reduce record.
Creates a builder, initialized with content of this record. A builder may be used to enhance or reduce record.
Please note, that this method creates new header for each patched record.
Attributes
- Returns
-
record builder
- See also
- Example
-
val imperial = record.patch.remove("m").add("foot", foot).add("yard", yard).get
Converts this record to scala.Product, e.g. case class.
Converts this record to scala.Product, e.g. case class.
For example:
// Assume following CSV source
// ----------------
// name,born,died
// Nicolaus Copernicus,1473-02-19,1543-05-24
// Johannes Hevelius,1611-01-28,
// ----------------
// and a Record created based on it
val record: Record = ???
case class Person(name: String, born: LocalDate, died: Option[LocalDate])
val person: Decoded[Person] = record.to[Person]
Please note, that the conversion for case classes is name-based (case class field names have to match record (CSV) header) and is case sensitive. The order of fields does not matter. Case class may be narrower and effectively retrieve only a subset of record's fields.
It is possible to use a tuple instead of case class - see the tuple-optimized version of to.
Current implementation supports only shallow conversion - each product field has to be retrieved from single record field through StringParser.
Because conversion to product requires parsing of all fields through StringParser, there is no way to provide custom formatter, like while using get method. If other then the default formatting has to be handled, a custom given instance stringParser
has to be provided:
// Assume following CSV source
// ----------------
// name,born,died
// Nicolaus Copernicus,19.02.1473,24.05.1543
// Johannes Hevelius,28.01.1611,
// ----------------
// and a Record created based on it
val record: Record = ???
case class Person(name: String, born: LocalDate, died: Option[LocalDate])
given ldsp: StringParser[LocalDate] with
def apply(str: String) = LocalDate.parse(str.strip, DateTimeFormatter.ofPattern("dd.MM.yyyy"))
val person: Decoded[Person] = record.to[Person]
Type parameters
- P
-
the scala.Product type to converter this record to, with given type class providing support for conversion (arranged internally by spata, assuming
StringParser
is available for all product field types)
Attributes
- Returns
-
either converted product or an error
Converts this record to scala.Tuple. Althogh the product conversion to method works for tuples too, this tuple-optimized version is more efficient.
Converts this record to scala.Tuple. Althogh the product conversion to method works for tuples too, this tuple-optimized version is more efficient.
For example:
// Assume following CSV source
// ----------------
// name,born,died
// Nicolaus Copernicus,1473-02-19,1543-05-24
// Johannes Hevelius,1611-01-28,
// ----------------
// and a Record created based on it
val record: Record = ???
tyoe T3 = (String, LocalDate, Option[LocalDate])
val person: Decoded[T3] = record.to[T3]
Please note, that the conversion to tuples is index-based and header names are not taken into account. Tuple may be narrower and effectively retrieve only an initial subset of record's fields.
Current implementation supports only shallow conversion - each tuple field has to be retrieved from single record field through StringParser.
Because conversion to tuple requires parsing of all fields through StringParser, there is no way to provide custom formatter, like while using get method. If other then the default formatting has to be handled, a custom given instnce of stringParser
has to be provided:
// Assume following CSV source
// ----------------
// name,born,died
// Nicolaus Copernicus,19.02.1473,24.05.1543
// Johannes Hevelius,28.01.1611,
// ----------------
// and a Record created based on it
val record: Record = ???
tyoe T3 = (String, LocalDate, Option[LocalDate])
given ldsp: StringParser[LocalDate] with
def apply(str: String) = LocalDate.parse(str.strip, DateTimeFormatter.ofPattern("dd.MM.yyyy"))
val person: Decoded[T3] = record.to[T3]
Type parameters
- T
-
the scala.Tuple type to converter this record to, with given type class providing support for conversion (arranged internally by spata, assuming
StringParser
is available for all tuple field types)
Attributes
- Returns
-
either converted tuple or an error
Gets text representation of record, with fields separated by comma.
Gets text representation of record, with fields separated by comma.
Attributes
- Definition Classes
-
Any
Creates new record with value at given key updated with provided one. Original record is returned if the key does not match any header key.
Creates new record with value at given key updated with provided one. Original record is returned if the key does not match any header key.
The new record shares the header with the old one.
Value parameters
- key
-
the key (name) of field to be updated
- value
-
the new value
Attributes
- Returns
-
a new record with updated value
Creates new record with value at given index updated with provided one. Original record is returned if the index is out of bounds.
Creates new record with value at given index updated with provided one. Original record is returned if the index is out of bounds.
The new record shares the header with the old one.
Value parameters
- idx
-
the index of field to be updated
- value
-
the new value
Attributes
- Returns
-
a new record with updated value
Creates new record with value at given key updated by provided function. The function receives existing value as an argument. Original record is returned if the key does not match any header key.
Creates new record with value at given key updated by provided function. The function receives existing value as an argument. Original record is returned if the key does not match any header key.
The new record shares the header with the old one.
Value parameters
- f
-
the function to be applied to existing value
- key
-
the key (name) of field to be updated
Attributes
- Returns
-
a new record with updated value
- Example
-
val updated = record.updatedWith("name")(s => s.toLowerCase)
Creates new record with value at given index updated by provided function. The function receives existing value as an argument. Original record is returned if the index is out of bounds.
Creates new record with value at given index updated by provided function. The function receives existing value as an argument. Original record is returned if the index is out of bounds.
The new record shares the header with the old one.
Value parameters
- f
-
the function to be applied to existing value
- idx
-
the index of field to be updated
Attributes
- Returns
-
a new record with updated value
Concrete fields
Indexing header - provided explicitly or generated in tuple style: "_1"
, "_2"
etc.
Indexing header - provided explicitly or generated in tuple style: "_1"
, "_2"
etc.
Attributes
Last line number in source data this record is built from or 0
if this information is not available (i.e. record is not created through CSV parsing).
Last line number in source data this record is built from or 0
if this information is not available (i.e. record is not created through CSV parsing).
Attributes
- See also
-
Position for line number description.
Row number in source data this record comes from or 0
if this information is not available (i.e. record is not created through CSV parsing).
Row number in source data this record comes from or 0
if this information is not available (i.e. record is not created through CSV parsing).
Attributes
- See also
-
Position for row number description.