record apply for filters
Run a record of closures on the matching fields of a record.
Command Type
built-in
Signature
> record apply {flags} (transforms)
Flags
--strict, -s: Error when a transform names a field the input does not have.
Parameters
transforms: A record of closures, keyed by the fields they transform.
Input/output types:
| input | output |
|---|---|
| record | record |
| table | table |
| list<any> | list<any> |
Examples
Convert the fields of a parsed row.
> {name: ' alice ', age: '42'} | record apply {name: {str trim}, age: {into int}}
╭──────┬───────╮
│ name │ alice │
│ age │ 42 │
╰──────┴───────╯Transform a nested field with a nested record of closures.
> {server: {host: localhost, port: '8080'}} | record apply {server: {port: {into int}}}
╭────────┬──────────────────────╮
│ │ ╭──────┬───────────╮ │
│ server │ │ host │ localhost │ │
│ │ │ port │ 8080 │ │
│ │ ╰──────┴───────────╯ │
╰────────┴──────────────────────╯Transforms for missing fields are skipped.
> {a: 1} | record apply {a: {|x| $x + 1}, b: {|x| $x * 2}}
╭───┬───╮
│ a │ 2 │
╰───┴───╯Convert a column of every row of a table.
> [{name: a, size: '1'} {name: b, size: '2'}] | record apply {size: {into int}}
╭───┬──────┬──────╮
│ # │ name │ size │
├───┼──────┼──────┤
│ 0 │ a │ 1 │
│ 1 │ b │ 2 │
╰───┴──────┴──────╯Notes
Each closure runs on the input field with the same key. It receives the field's value as its argument and as $in, and its result replaces the value. A nested record of closures applies to a nested record field in the same way. Fields without a transform pass through unchanged. Transforms for fields the input does not have are skipped, unless --strict is given. The transforms record is checked before any closure runs. Given a list of records, the transforms are applied to each record in turn and the results are streamed.