Overview
Records to CSV rows and back: def/csv-type, and readers and writers of records.
(import (text csv) (text csv-codec))
(def/csv-type
(: Sale (Record (: id int)
(: customer string #:csv "Customer Name")
(: price decimal)
(: note (Option string) #:optional))))
(: sales (Result CsvError (Vec Sale)))
(def sales (parse-csv-records (csv-file "sales.csv")))
(csv-for-each-record (csv-file "sales.csv")
(fun (s) (println (record-ref s customer))))
(write-csv-records port sales-vec) ; the header, then a row for each
def/csv-type declares a record and its CsvRecord
instance, as def/json-type does for JSON. The readers and writers
here then take records of that type. They are built on
(text csv), and take its options as well as #:header?.
Columns. With #:header? #t, the default, the first row
is the header, and each field’s column is found in it by name: the
field’s own, or the one #:csv gives. The order of the columns
does not matter, columns no field names are not read, and when two
columns have one name the first is used. With #:header? #f there
is no header, and the fields are the columns in order. The writers write
the header from the same names.
Fields. A field reads and writes through csv-field->
and ->csv-field, which have instances for string,
int, long, double, decimal, bool
and (Option %a). A number is read in the invariant culture, with
a point and not a comma, and may have space around it. A bool is
true or false in any case. An (Option %a) is
None for an empty field, and is written as one. A type of your
own reads and writes as a field when it has the two instances.
Errors. Everything is a CsvError, at the line where the
row starts, in the row and field the column is in, and named by the
column: line 12, row 11, field 3: price: "12,5" is not a decimal.
A header without a required column is an error before the first record,
in row 1 and field 0. The records before an error have been read when
it is answered. A call that cannot tell which record type it reads
says so with an annotation, as sales does above.
See also: def/csv-type, parse-csv-records, csv-for-each-record, csv-records->string, CsvRecord
Reference
Types
CsvRecord
trait ↑ Types
A record that reads from and writes to a CSV row: what def/csv-type writes.
%a- The record.
def/csv-type writes an instance for its record, and is the way
to get one. An instance by hand gives the columns, a way to read a row
given where each column is, and a way to write a row.
See also: def/csv-type, CsvColumns
CsvColumn
record ↑ Types
A column a record reads.
name- The name of the column in the header.
required- Whether the header has to have it. A field with a default does not.
Functions
->csv-field
function ↑ Functions
(: ->csv-field (-> %a string))
A value as the text of a CSV field.
%a- The type.
value- The value.
- returns
- The text. An Option writes None as the empty field.
(->csv-field 1.5M) ; "1.5"
(->csv-field (Some 3)) ; "3"
See also: csv-field->
csv-field->
function ↑ Functions
(: csv-field-> (-> string (Result string %a)))
The value the text of a CSV field is.
%a- The type.
text- The text of the field.
- returns
- The value, or what is wrong with the text.
The error says what is wrong with the text and not where: a reader
adds the line, the row and the column. "12,5" is not a decimal.
See also: ->csv-field
csv-columns
function ↑ Functions
(: csv-columns (-> (CsvColumns %a)))
A record’s columns, one for each field, in the order of the fields.
- returns
- The columns. The record's type says whose they are: annotate the result to choose.
See also: CsvColumns, CsvRecord
csv-record->
function ↑ Functions
(: csv-record-> (-> (IArray int) (IArray string) (Result (Tuple int string) %a)))
A record read from a row.
at- For each field, where its column is in the row: -1 for a column the header does not have.
row- The row.
- returns
- The record, or the field that does not read, from 0 in the record's order, and why.
See also: CsvRecord
->csv-record
function ↑ Functions
(: ->csv-record (-> %a (IArray string)))
A record written as a row.
v- The record.
- returns
- One field for each of its fields, in their order.
See also: CsvRecord
csv-required
function ↑ Functions
(: csv-required (-> string CsvColumn))
(csv-required name)
A column the header has to have. For code def/csv-type writes.
name- The column's name.
- returns
- The column.
csv-optional
function ↑ Functions
(: csv-optional (-> string CsvColumn))
(csv-optional name)
A column the header need not have. For code def/csv-type writes.
name- The column's name.
- returns
- The column.
csv-columns-of
function ↑ Functions
(: csv-columns-of (-> (List CsvColumn) (CsvColumns %a)))
(csv-columns-of cols)
Columns of a record. For code def/csv-type writes.
cols- The columns, in the order of the fields.
- returns
- The columns.
csv-row-of
function ↑ Functions
(: csv-row-of (-> (List string) (IArray string)))
(csv-row-of fields)
A row of fields. For code def/csv-type writes.
fields- The fields.
- returns
- The row.
csv-decode-field
function ↑ Functions
(: csv-decode-field (-> (IArray int) int (IArray string) (Result (Tuple int string) %a)))
(csv-decode-field at k row)
A record’s field, read from its column. For code def/csv-type writes.
at- Where each field's column is.
k- The field, from 0.
row- The row.
- returns
- The value, or the field and why it does not read.
csv-decode-field/default
function ↑ Functions
(: csv-decode-field/default (-> (IArray int) int (IArray string) %a (Result (Tuple int string) %a)))
(csv-decode-field/default at k row fallback)
A record’s field with a default, read from its column. For code def/csv-type writes.
at- Where each field's column is.
k- The field, from 0.
row- The row.
fallback- The value when the header has no such column, the row ends before it, or the field is empty.
- returns
- The value, or the field and why it does not read.
csv-fold-records
function ↑ Functions
(: csv-fold-records (-> %s %seed (-> %a %seed %seed) (#:header? bool) (#:separator char) (#:quote-char (Option char)) (#:strict? bool) (Result CsvError %seed)))
(csv-fold-records source seed f
#:header? #t #:separator #\, #:quote-char (Some #\") #:strict? #t)
A seed threaded through the records of an input.
source- What to read: anything with a CsvSource implementation.
seed- The seed to begin with.
f- Called with each record and the seed; answers the next seed.
#:header?- Whether the first row is the header. With #f the fields are the columns in order.
#:separator- As for parse-csv.
#:quote-char- As for parse-csv.
#:strict?- As for parse-csv.
- returns
- The final seed, or the first error.
- raises
System.ArgumentException - When the separator or the quote is not allowed.
- raises
System.IO.IOException - When the source is a csv-file that cannot be opened.
;; The total of the price column.
(csv-fold-records (csv-file "sales.csv") 0M
(fun (s total) (+ total (record-ref s price))))
One record is held at a time. The records before an error have been folded when it is answered.
See also: csv-for-each-record, parse-csv-records
csv-for-each-record
function ↑ Functions
(: csv-for-each-record (-> %s (-> %a void) (#:header? bool) (#:separator char) (#:quote-char (Option char)) (#:strict? bool) (Result CsvError Unit)))
(csv-for-each-record source f
#:header? #t #:separator #\, #:quote-char (Some #\") #:strict? #t)
Calls a function with each record of an input.
source- What to read.
f- Called with each record, in order.
#:header?- Whether the first row is the header.
#:separator- As for parse-csv.
#:quote-char- As for parse-csv.
#:strict?- As for parse-csv.
- returns
- Ok at the end of the input, or the first error.
- raises
System.ArgumentException - When the separator or the quote is not allowed.
- raises
System.IO.IOException - When the source is a csv-file that cannot be opened.
See also: csv-fold-records
parse-csv-records
function ↑ Functions
(: parse-csv-records (-> %s (#:header? bool) (#:separator char) (#:quote-char (Option char)) (#:strict? bool) (Result CsvError (Vec %a))))
(parse-csv-records source
#:header? #t #:separator #\, #:quote-char (Some #\") #:strict? #t)
Reads every record of an input.
source- What to read.
#:header?- Whether the first row is the header.
#:separator- As for parse-csv.
#:quote-char- As for parse-csv.
#:strict?- As for parse-csv.
- returns
- The records, or the first error.
- raises
System.ArgumentException - When the separator or the quote is not allowed.
- raises
System.IO.IOException - When the source is a csv-file that cannot be opened.
(: sales (Result CsvError (Vec Sale)))
(def sales (parse-csv-records "id,Customer Name,price\n7,Ada,12.50\n"))
See also: csv-fold-records
csv-records->string
function ↑ Functions
(: csv-records->string (-> %rs (#:header? bool) (#:separator char) (#:quote-char (Option char)) (#:quote-all? bool) (#:crlf? bool) string))
(csv-records->string records
#:header? #t #:separator #\, #:quote-char (Some #\") #:quote-all? #f
#:crlf? #f)
Records as CSV text: the header, then a row for each.
records- The records: a vec, a list, a Seq, or any other Iterable of them.
#:header?- Whether to write the header first: #t by default.
#:separator- As for csv->string.
#:quote-char- As for csv->string.
#:quote-all?- As for csv->string.
#:crlf?- As for csv->string.
- returns
- The text. parse-csv-records reads it back as the same records.
See also: write-csv-records, parse-csv-records
write-csv-records
function ↑ Functions
(: write-csv-records (-> OutputPort %rs (#:header? bool) (#:separator char) (#:quote-char (Option char)) (#:quote-all? bool) (#:crlf? bool) void))
(write-csv-records port records
#:header? #t #:separator #\, #:quote-char (Some #\") #:quote-all? #f
#:crlf? #f)
Writes records to a port: the header, then a row for each.
port- The port.
records- The records: a vec, a list, a Seq, or any other Iterable of them.
#:header?- Whether to write the header first: #t by default.
#:separator- As for write-csv.
#:quote-char- As for write-csv.
#:quote-all?- As for write-csv.
#:crlf?- As for write-csv.
The records are read once, in order, and written in pieces, as
write-csv writes rows.
See also: csv-records->string, write-csv
Macros
def/csv-type
macro ↑ Macros
(def/csv-type declaration)
Declares a record, and how it reads from and writes to a CSV row.
declaration- A record declaration, as type takes one, whose fields may carry the markers below.
(def/csv-type
(: Sale (Record (: id int)
(: customer string #:csv "Customer Name")
(: price decimal)
(: currency string #:default "EUR")
(: note (Option string) #:optional))))
(def/csv-type
(: (Tagged %a) (Record (: tag string) (: value %a))))
(def/csv-type (: Name (Record (: field type marker ...) ...)))
declares the record Name as type would, and a
CsvRecord instance for it. A record with type parameters gets an
instance that needs each of them to read and write as a field.
| marker | what it does |
|---|---|
#:csv "Name" | the column’s name in the header; the field’s name by default |
#:default expr | the value when the header has no such column, the row ends before it, or the field is empty |
#:optional | #:default None, for an (Option %a) |
#:mutable | kept: the field is mutable in the record |
A field without a default needs its column in the header. An
(Option %a) field without #:optional still needs its
column, and is None when the field is empty.
See also: CsvRecord, parse-csv-records, csv-records->string
Other
CsvColumns
newtype ↑ Other
A record’s columns. The type parameter is the record they are the columns of.
See also: csv-columns, CsvColumn