Skip to content
Myr

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

1 file+46 added−0 removed

modified:docs/content/3.manual/manual.yml+46 added
2508 unmodified lines
2509 - 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}']25122513 - 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.25182519 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:25292530 .resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$user_id, $ts}]} | {$user_id, $kind, $id, $ts}25312532 Or, if we aren't sure if the input is an array of values or an object:25332534 .[] as [$id, $kind, $user_id, $ts] ?// {$id, $kind, $user_id, $ts} | ...25352536 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`:25392540 .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}25412542 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.25452546 [[3]] | .[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end25472548 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}']25582559 - title: 'Defining Functions'2560 body: |25612562 You can give a filter a name using "def" syntax:
725 unmodified lines