Ports #
| Port | Direction | Kind | Required | Description |
|---|---|---|---|---|
in | input | records | yes | |
out | output | text | yes | Media types: text/csv, text/plain; charset=utf-8 |
Parameters #
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
header | boolean | true | no | |
delimiter | string | "," | no | |
fields | array | none | no | Column names. Default is the first record's member names. |
When to use it #
- The last step of a report: records in, CSV text with a header row out.
- Choose and order the columns explicitly with
fields; members that are not listed are left out.
When to use something else
- CSV loses types — everything becomes text, and
nulland an empty string look the same. A member that holds an array or an object fails the run: flatten it first withstd:map.
A tested example #
Nothing here is invented: this is the test “columns from the first record” from the pipe's own portable test file, which every conformant runner must pass.
This test sets no parameters.
{"id":1,"name":"Bond, James"}
{"name":"Q","id":2}id,name
1,"Bond, James"
2,QThe same step as a node of a pipeline.yaml:
nodes:
csv-encode:
pipe: std:csv-encode
inputs:
in: {from: inputs.in}Run it #
The pipe runs on its own from a shell. The input is the test input above. The quickstart shows how to get the openflange command.
printf '%s\n' '{"id":1,"name":"Bond, James"}' '{"name":"Q","id":2}' \
| openflange run std:csv-encodeid,name
1,"Bond, James"
2,QExecuted with openflange 0.0.1 (go1.27.1 linux/amd64), built from commit 11cf8cc of the reference implementation; the output shown is what it printed, and it matches the test.
Notes from the manifest #
The pipe's manifest, OPENFLANGE.md, carries these notes.
Part of the OpenFlange standard library (std, version 0.1).
The explicit — and lossy with respect to types — conversion from records to
CSV text (it is never inserted automatically: in CSV everything is text, and
null and "" coincide).
Columns are fields, or the member names of the first record in their
order. With header: true (default) a header row is written first.
- Rows end with LF (
\n), including the last one. - A string is written as it is, quoted with
"(embedded quotes doubled) only when it contains the delimiter, a quote, CR or LF. - A number is written as its original literal;
true/falseas those words;nulland absent members as an empty field. - A member holding an array or an object fails the run (
E_RUNTIME): CSV cannot represent it losslessly, convert it first. - When the columns came from the first record, a later record with a member
that is not a column fails the run instead of losing it. With
fields, other members are ignored (an explicit projection). - An item that is not an object fails.
- Empty input: with
fieldsandheader: trueonly the header row is written; otherwise the output is empty.
CSV injection. Values beginning with =, +, - or @ can be
evaluated as formulas by spreadsheet software. The encoder never alters data
to prevent this; sanitise before handing CSV to spreadsheet users.
Reference #
- Maturity
- Experimental
- Processing scope
record- Manifest version
0.1.0
- Normative definition: std:csv-encode in the standard library specification
- Manifest and tests: std/0.1/csv-encode in the specification repository