Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

TransformExperimental

std:cast #

Converts members of each record to a JSON type, exactly or not at all.

For example: turn the strings of a CSV file into numbers and booleans.

A pipe is a data processing step with declared inputs and outputs; standard pipes are built into the runner.

Ports #

std:cast takes in and produces out, rejected.std:castinrecordsoutrecordsrejectedrecords · optional
std:cast takes in and produces out, rejected.std:castinrecordsoutrecordsrejectedrecords · optional
Named ports from the latest published contract. Dashed lines are optional ports.
Ports of std:cast
PortDirectionKindRequiredDescription
ininputrecordsyes
outoutputrecordsyes
rejectedoutputrecordsnoItems with un-castable members, wrapped as {item, errors}.

Parameters #

Parameters of std:cast
NameTypeDefaultRequiredDescription
fieldsobjectnoneyesMap of member name to string|integer|number|boolean.
onErrorstring"fail"no One of: fail, reject.

When to use it #

  • After std:csv-decode, where every value is a string: declare which members are integer, number or boolean.
  • Keep large identifiers and amounts exact: "12345678901234567890" becomes that integer, not a rounded one.
  • Collect rows with a value that cannot be converted on rejected (onError: reject) instead of stopping the run.

When to use something else

A tested example #

Nothing here is invented: this is the test “casts strings to typed values and keeps literals” from the pipe's own portable test file, which every conformant runner must pass.

Parameters
{
  "fields": {
    "n": "integer",
    "x": "number",
    "ok": "boolean",
    "id": "string"
  }
}
Input: in
{"n":"12345678901234567890","x":"0.10","ok":"true","id":7,"other":1.0}
Expected output: out
{"n":12345678901234567890,"x":0.10,"ok":true,"id":"7","other":1.0}
Expected output: rejected

Not connected in this test.

The same step as a node of a pipeline.yaml:

pipeline.yaml
nodes:
  cast:
    pipe: std:cast
    inputs:
      in: {from: inputs.in}
    parameters:
      fields: {n: "integer", x: "number", ok: "boolean", id: "string"}

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.

Command
printf '{"n":"12345678901234567890","x":"0.10","ok":"true","id":7,"other":1.0}\n' \
  | openflange run std:cast \
      --param 'fields={"n":"integer","x":"number","ok":"boolean","id":"string"}'
Standard output (out)
{"n":12345678901234567890,"x":0.10,"ok":true,"id":"7","other":1.0}

Executed 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).

Converts the top-level members named in fields to the given JSON type. All other members, and values that already have the target type, are copied as raw bytes. A listed member that is absent or null is left as it is.

Conversions are exact or they do not happen:

target accepted result
string string; number; boolean the string; the number's literal text (1.50 → "1.50"); "true"/"false"
integer integer number; number with a zero fractional part (3.0, 2e3); string matching [-+]?[0-9]+ the integer, exactly, at any size ("12345678901234567890123" → 12345678901234567890123, "007" → 7)
number number; string that is a JSON number literal (no leading +) the literal, unchanged ("0.10" → 0.10)
boolean boolean; the strings "true" and "false" (exactly) true / false

Everything else is un-castable — for example "1.5" or "1e3" → integer, "abc" → number, 1 or "TRUE" → boolean, objects and arrays. Strings are not trimmed (" 42" is un-castable).

When an item has at least one un-castable member:

  • onError: fail (default): the run fails with E_RUNTIME.
  • onError: reject: the whole original item is emitted on the optional output rejected as {"item": <original>, "errors": [{"field": "<name>", "message": "<text>"}]} and nothing is emitted on out for it. If rejected is not connected the run fails as with fail — errors are never dropped silently.

Message texts are informative, not portable. A non-object item fails the run.

yaml
nodes:
  typed:
    pipe: std:cast
    inputs: {in: {from: csv.outputs.out}}
    parameters:
      fields: {age: integer, score: number, active: boolean}

Reference #

Maturity
Experimental
Processing scope
record
Manifest version
0.1.0