Add documentation for destructuring alternation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by William Langford · committed by William Langford · parent 0c845aa · Browse files · 1 file·+46 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+46 added−0 removed
- other:+46 added−0 removed
Provenance
- Commit
39a3e2c- Tree
518e644- Author
William Langford <wlangfor@gmail.com>- Committer
William Langford <wlangfor@gmail.com>- Parent
0c845aa
1 file changed+46 added−0 removed
modified:docs/content/3.manual/manual.yml+46 added
2508 unmodified lines2509 - program: '.[] as [$a, $b] | {a: $a, b: $b}'2510 input: '[[0], [0, 1], [2, 1, 0]]'2511 output: ['{"a":0,"b":null}', '{"a":0,"b":1}', '{"a":2,"b":1}']2512 2513+ - title: 'Destructuring Alternative Operator: `?//`'2514+ body: |2515+ 2516+ The destructuring alternative operator provides a concise mechanism2517+ for destructuring an input that can take one of several forms.2518+2519+ Suppose we have an API that returns a list of resources and events2520+ associated with them, and we want to get the user_id and timestamp of2521+ the first event for each resource. The API (having been clumsily2522+ converted from XML) will only wrap the events in an array if the resource2523+ has multiple events:2524+ 2525+ {"resources": [{"id": 1, "kind": "widget", "events": {"action": "create", "user_id": 1, "ts": 13}},2526+ {"id": 2, "kind": "widget", "events": [{"action": "create", "user_id": 1, "ts": 14}, {"action": "destroy", "user_id": 1, "ts": 15}]}]}2527+ 2528+ We can use the destructuring alternative operator to handle this structural change simply:2529+2530+ .resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$user_id, $ts}]} | {$user_id, $kind, $id, $ts}2531+2532+ Or, if we aren't sure if the input is an array of values or an object:2533+2534+ .[] as [$id, $kind, $user_id, $ts] ?// {$id, $kind, $user_id, $ts} | ...2535+2536+ Each alternative need not define all of the same variables, but all named2537+ variables will be available to the subsequent expression. Variables not2538+ matched in the alternative that succeeded will be `null`:2539+2540+ .resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$first_user_id, $first_ts}]} | {$user_id, $first_user_id, $kind, $id, $ts, $first_ts}2541+2542+ Additionally, if the subsequent expression returns an error, the2543+ alternative operator will attempt to try the next binding. Errors2544+ that occur during the final alternative are passed through.2545+2546+ [[3]] | .[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end2547+2548+ examples:2549+ - program: '.[] as {$a, $b, c: {$d, $e}} ?// {$a, $b, c: [{$d, $e}]} | {$a, $b, $d, $e}'2550+ input: '[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]'2551+ output: ['{"a":1,"b":2,"d":3,"e":4}', '{"a":1,"b":2,"d":3,"e":4}']2552+ - program: '.[] as {$a, $b, c: {$d}} ?// {$a, $b, c: [{$e}]} | {$a, $b, $d, $e}'2553+ input: '[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]'2554+ output: ['{"a":1,"b":2,"d":3,"e":null}', '{"a":1,"b":2,"d":null,"e":4}']2555+ - program: '.[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end'2556+ input: '[[3]]'2557+ output: ['{"a":null,"b":3}']2558+2559 - title: 'Defining Functions'2560 body: |2561 2562 You can give a filter a name using "def" syntax:725 unmodified lines