Ports #
| Port | Direction | Kind | Required | Description |
|---|---|---|---|---|
in | input | records | yes | |
out | output | records | yes | |
rejected | output | records | no | Items with un-castable members, wrapped as {item, errors}. |
Parameters #
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
fields | object | none | yes | Map of member name to string|integer|number|boolean. |
onError | string | "fail" | no | One of: fail, reject. |
When to use it #
- After
std:csv-decode, where every value is a string: declare which members areinteger,numberorboolean. - 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
- To check the structure of records without converting anything, use
std:validate-schema.
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.
{
"fields": {
"n": "integer",
"x": "number",
"ok": "boolean",
"id": "string"
}
}{"n":"12345678901234567890","x":"0.10","ok":"true","id":7,"other":1.0}{"n":12345678901234567890,"x":0.10,"ok":true,"id":"7","other":1.0}Not connected in this test.
The same step as a node of a 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.
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"}'{"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 withE_RUNTIME.onError: reject: the whole original item is emitted on the optional outputrejectedas{"item": <original>, "errors": [{"field": "<name>", "message": "<text>"}]}and nothing is emitted onoutfor it. Ifrejectedis not connected the run fails as withfail— errors are never dropped silently.
Message texts are informative, not portable. A non-object item fails the run.
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
- Normative definition: std:cast in the standard library specification
- Manifest and tests: std/0.1/cast in the specification repository