diff --git a/pkg/yqlib/doc/Multiply (Merge).md b/pkg/yqlib/doc/Multiply (Merge).md index 181c2b62..32d52e9c 100644 --- a/pkg/yqlib/doc/Multiply (Merge).md +++ b/pkg/yqlib/doc/Multiply (Merge).md @@ -29,46 +29,6 @@ will output 12 ``` -## Merge arrays of objects together, matching on a key -It's a complex command, the trickyness comes from needing to have the right context in the expressions. -First we save the second array into a variable '$two' which lets us reference it later. -We then need to update the first array. We will use the relative update (|=) because we need to update relative to the current element of the array in the LHS in the RHS expression. -We set the current element of the first array as $cur. Now we multiply (merge) $cur with the matching entry in $two, by passing $two through a select filter. - - -Given a sample.yml file of: -```yaml -- a: apple - b: appleB -- a: kiwi - b: kiwiB -- a: banana - b: bananaB -``` -And another sample another.yml file of: -```yaml -- a: banana - c: bananaC -- a: apple - b: appleB2 -- a: dingo - c: dingoC -``` -then -```bash -yq eval-all '(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))' sample.yml another.yml -``` -will output -```yaml -- a: apple - b: appleB2 -- a: kiwi - b: kiwiB -- a: banana - b: bananaB - c: bananaC -``` - ## Merge objects together, returning merged result only Given a sample.yml file of: ```yaml @@ -271,6 +231,46 @@ will output age: 32 ``` +## Merge arrays of objects together, matching on a key +It's a complex command, the trickyness comes from needing to have the right context in the expressions. +First we save the second array into a variable '$two' which lets us reference it later. +We then need to update the first array. We will use the relative update (|=) because we need to update relative to the current element of the array in the LHS in the RHS expression. +We set the current element of the first array as $cur. Now we multiply (merge) $cur with the matching entry in $two, by passing $two through a select filter. + + +Given a sample.yml file of: +```yaml +- a: apple + b: appleB +- a: kiwi + b: kiwiB +- a: banana + b: bananaB +``` +And another sample another.yml file of: +```yaml +- a: banana + c: bananaC +- a: apple + b: appleB2 +- a: dingo + c: dingoC +``` +then +```bash +yq eval-all '(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))' sample.yml another.yml +``` +will output +```yaml +- a: apple + b: appleB2 +- a: kiwi + b: kiwiB +- a: banana + b: bananaB + c: bananaC +``` + ## Merge to prefix an element Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index a9adf8da..1393801f 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -129,16 +129,6 @@ var multiplyOperatorScenarios = []expressionScenario{ "D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n", }, }, - { - description: "Merge arrays of objects together, matching on a key", - subdescription: mergeArraysObjectKeysText, - document: `[{a: apple, b: appleB}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB}]`, - document2: `[{a: banana, c: bananaC}, {a: apple, b: appleB2}, {a: dingo, c: dingoC}]`, - expression: `(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))`, - expected: []string{ - "D0, P[], (doc)::[{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC}]\n", - }, - }, { description: "Merge objects together, returning merged result only", document: `{a: {field: me, fieldA: cat}, b: {field: {g: wizz}, fieldB: dog}}`, @@ -261,6 +251,16 @@ var multiplyOperatorScenarios = []expressionScenario{ "D0, P[a], (!!seq)::[{name: fred, age: 34}, {name: bob, age: 32}]\n", }, }, + { + description: "Merge arrays of objects together, matching on a key", + subdescription: mergeArraysObjectKeysText, + document: `[{a: apple, b: appleB}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB}]`, + document2: `[{a: banana, c: bananaC}, {a: apple, b: appleB2}, {a: dingo, c: dingoC}]`, + expression: `(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))`, + expected: []string{ + "D0, P[], (doc)::[{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC}]\n", + }, + }, { description: "Merge to prefix an element", document: `{a: cat, b: dog}`,