2020-11-03 23:48:43 +00:00
package yqlib
2020-10-19 05:14:29 +00:00
import (
"testing"
)
2021-03-19 01:54:03 +00:00
var doc1 = ` list :
# Hi this is a comment .
# Hello this is another comment .
- "abc" `
var doc2 = ` list2 :
# This is yet another comment .
# Indeed this is yet another comment .
- "123" `
var docExpected = ` D0 , P [ ] , ( ! ! map ) : : list :
# Hi this is a comment .
# Hello this is another comment .
- "abc"
list2 :
# This is yet another comment .
# Indeed this is yet another comment .
- "123"
`
2021-07-22 10:57:47 +00:00
var mergeArrayWithAnchors = ` sample :
- & a
- << : * a
`
2022-02-01 04:11:39 +00:00
var mergeArraysObjectKeysText = `
This is a fairly complex expression - you can use it as is by providing the environment variables as seen in the example below .
It merges in the array provided in the second file into the first - matching on equal keys .
Explanation :
The approach , at a high level , is to reduce into a merged map ( keyed by the unique key )
2021-12-05 02:03:38 +00:00
and then convert that back into an array .
2022-02-01 04:11:39 +00:00
First the expression will create a map from the arrays keyed by the idPath , the unique field we want to merge by .
2021-12-05 02:03:38 +00:00
The reduce operator is merging ' ( { } ; . * $ item ) ' , so array elements with the matching key will be merged together .
Next , we convert the map back to an array , using reduce again , concatenating all the map values together .
2021-11-29 10:28:25 +00:00
Finally , we set the result of the merged array back into the first doc .
2021-11-29 09:46:12 +00:00
2021-11-29 09:56:04 +00:00
Thanks Kev from [ stackoverflow ] ( https : //stackoverflow.com/a/70109529/1168223)
2021-11-29 09:53:36 +00:00
`
2021-11-29 10:06:09 +00:00
var mergeExpression = `
2021-11-29 10:28:25 +00:00
(
2022-02-01 04:11:39 +00:00
( ( ( eval ( strenv ( originalPath ) ) + eval ( strenv ( otherPath ) ) ) | . [ ] | { ( eval ( strenv ( idPath ) ) ) : . } ) as $ item ireduce ( { } ; . * $ item ) ) as $ uniqueMap
2021-11-29 10:06:09 +00:00
| ( $ uniqueMap | to_entries | . [ ] ) as $ item ireduce ( [ ] ; . + $ item . value )
2021-11-29 10:28:25 +00:00
) as $ mergedArray
2022-02-01 04:11:39 +00:00
| select ( fi == 0 ) | ( eval ( strenv ( originalPath ) ) ) = $ mergedArray
2021-04-28 10:35:10 +00:00
`
2021-11-13 23:18:02 +00:00
var docWithHeader = ` # here
2021-08-16 23:54:12 +00:00
a : apple
`
2021-11-13 23:51:18 +00:00
var nodeWithHeader = ` node :
# here
a : apple
2021-08-16 23:54:12 +00:00
`
2021-11-13 23:18:02 +00:00
var docNoComments = ` b : banana
2021-08-16 23:54:12 +00:00
`
2021-11-13 23:18:02 +00:00
var docWithFooter = ` a : apple
2021-08-16 23:54:12 +00:00
# footer
`
2021-11-13 23:18:02 +00:00
var nodeWithFooter = ` a : apple
2021-08-16 23:54:12 +00:00
# footer `
2021-11-13 23:18:02 +00:00
var document = ` a : & cat { name : cat }
2021-10-11 03:46:46 +00:00
b : { name : dog }
c :
<< : * cat
`
2020-10-19 05:14:29 +00:00
var multiplyOperatorScenarios = [ ] expressionScenario {
2021-07-22 10:57:47 +00:00
{
skipDoc : true ,
document : mergeArrayWithAnchors ,
expression : ` . * . ` ,
expected : [ ] string {
"D0, P[], (!!map)::sample:\n - &a\n - !!merge <<: *a\n" ,
} ,
} ,
2022-03-19 07:42:12 +00:00
{
skipDoc : true ,
document : ` [[c], [b]] ` ,
expression : ` .[] | . *+ ["a"] ` ,
expected : [ ] string {
"D0, P[0], (!!seq)::[c, a]\n" ,
"D0, P[1], (!!seq)::[b, a]\n" ,
} ,
} ,
2021-08-16 23:54:12 +00:00
{
skipDoc : true ,
document : docWithHeader ,
document2 : docNoComments ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
expected : [ ] string {
2022-12-08 02:33:06 +00:00
"D0, P[], (!!map)::# here\n\na: apple\nb: banana\n" ,
2021-08-16 23:54:12 +00:00
} ,
} ,
{
skipDoc : true ,
document : nodeWithHeader ,
document2 : docNoComments ,
2021-11-13 23:51:18 +00:00
expression : ` (select(fi == 0) | .node) * select(fi == 1) ` ,
2021-08-16 23:54:12 +00:00
expected : [ ] string {
2021-11-13 23:51:18 +00:00
"D0, P[node], (!!map)::# here\na: apple\nb: banana\n" ,
2021-08-16 23:54:12 +00:00
} ,
} ,
{
skipDoc : true ,
document : docNoComments ,
document2 : docWithHeader ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
expected : [ ] string {
2022-12-08 02:33:06 +00:00
"D0, P[], (!!map)::# here\n\nb: banana\na: apple\n" ,
2021-08-16 23:54:12 +00:00
} ,
} ,
{
skipDoc : true ,
document : docNoComments ,
document2 : nodeWithHeader ,
2021-11-13 23:51:18 +00:00
expression : ` select(fi == 0) * (select(fi == 1) | .node) ` ,
2021-08-16 23:54:12 +00:00
expected : [ ] string {
"D0, P[], (!!map)::b: banana\n# here\na: apple\n" ,
} ,
} ,
{
skipDoc : true ,
document : docWithFooter ,
document2 : docNoComments ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
expected : [ ] string {
2022-05-25 00:54:03 +00:00
"D0, P[], (!!map)::a: apple\nb: banana\n# footer\n" ,
2021-08-16 23:54:12 +00:00
} ,
} ,
{
skipDoc : true ,
document : nodeWithFooter ,
document2 : docNoComments ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
expected : [ ] string { // not sure why there's an extra newline *shrug*
"D0, P[], (!!map)::a: apple\n# footer\n\nb: banana\n" ,
} ,
} ,
{
2022-05-25 00:54:03 +00:00
description : "doc 2 has footer" ,
skipDoc : true ,
document : docNoComments ,
document2 : docWithFooter ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
2021-08-16 23:54:12 +00:00
expected : [ ] string {
2022-05-25 00:54:03 +00:00
"D0, P[], (!!map)::b: banana\na: apple\n# footer\n" ,
2021-08-16 23:54:12 +00:00
} ,
} ,
2021-02-18 00:16:54 +00:00
{
description : "Multiply integers" ,
2022-01-22 05:40:17 +00:00
document : "a: 3\nb: 4" ,
expression : ` .a *= .b ` ,
2021-02-18 00:16:54 +00:00
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: 12\nb: 4\n" ,
2021-02-18 00:16:54 +00:00
} ,
} ,
2021-03-19 01:54:03 +00:00
{
skipDoc : true ,
document : doc1 ,
document2 : doc2 ,
expression : ` select(fi == 0) * select(fi == 1) ` ,
expected : [ ] string {
docExpected ,
} ,
} ,
2021-07-07 04:29:24 +00:00
{
skipDoc : true ,
expression : ` .x = { "things": "whatever"} * { } ` ,
expected : [ ] string {
"D0, P[], ()::x:\n things: whatever\n" ,
} ,
} ,
{
skipDoc : true ,
expression : ` .x = { } * { "things": "whatever"} ` ,
expected : [ ] string {
"D0, P[], ()::x:\n things: whatever\n" ,
} ,
} ,
2021-02-18 00:16:54 +00:00
{
skipDoc : true ,
expression : ` 3 * 4.5 ` ,
expected : [ ] string {
"D0, P[], (!!float)::13.5\n" ,
} ,
} ,
{
skipDoc : true ,
expression : ` 4.5 * 3 ` ,
expected : [ ] string {
"D0, P[], (!!float)::13.5\n" ,
} ,
} ,
2020-10-19 05:14:29 +00:00
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { also: [1]}, b: { also: me}} ` ,
expression : ` . * { "a" : .b} ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
2021-02-08 02:58:46 +00:00
{
skipDoc : true ,
document : "# b\nb:\n # a\n a: cat" ,
expression : "{} * ." ,
expected : [ ] string {
"D0, P[], (!!map)::# b\nb:\n # a\n a: cat\n" ,
} ,
} ,
{
skipDoc : true ,
document : "# b\nb:\n # a\n a: cat" ,
expression : ". * {}" ,
expected : [ ] string {
"D0, P[], (!!map)::# b\nb:\n # a\n a: cat\n" ,
} ,
} ,
2021-02-05 03:40:16 +00:00
{
skipDoc : true ,
document : ` { a: &a { b: &b { c: &c cat } } } ` ,
expression : ` { } * . ` ,
expected : [ ] string {
2022-04-14 22:27:22 +00:00
"D0, P[], (!!map)::a: &a\n b: &b\n c: &c cat\n" ,
2021-02-05 03:40:16 +00:00
} ,
} ,
2021-01-16 03:56:52 +00:00
{
skipDoc : true ,
2021-01-18 02:28:40 +00:00
document : ` { a: 2, b: 5} ` ,
document2 : ` { a: 3, b: 10} ` ,
2021-01-17 23:15:31 +00:00
expression : ` .a * .b ` ,
2021-01-16 03:56:52 +00:00
expected : [ ] string {
2021-01-18 02:28:40 +00:00
"D0, P[a], (!!int)::10\n" ,
"D0, P[a], (!!int)::20\n" ,
"D0, P[a], (!!int)::15\n" ,
"D0, P[a], (!!int)::30\n" ,
} ,
} ,
{
skipDoc : true ,
document : ` { a: 2} ` ,
document2 : ` { b: 10} ` ,
expression : ` select(fi ==0) * select(fi==1) ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: 2, b: 10}\n" ,
2021-01-16 03:56:52 +00:00
} ,
} ,
2020-11-25 04:01:12 +00:00
{
skipDoc : true ,
expression : ` { } * { "cat":"dog"} ` ,
expected : [ ] string {
"D0, P[], (!!map)::cat: dog\n" ,
} ,
} ,
2020-10-27 05:45:16 +00:00
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { also: me}, b: { also: [1]}} ` ,
expression : ` . * { "a":.b} ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-14 23:50:30 +00:00
description : "Merge objects together, returning merged result only" ,
document : ` { a: { field: me, fieldA: cat}, b: { field: { g: wizz}, fieldB: dog}} ` ,
expression : ` .a * .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{field: {g: wizz}, fieldA: cat, fieldB: dog}\n" ,
} ,
} ,
{
description : "Merge objects together, returning parent object" ,
document : ` { a: { field: me, fieldA: cat}, b: { field: { g: wizz}, fieldB: dog}} ` ,
2020-11-06 00:45:18 +00:00
expression : ` . * { "a":.b} ` ,
2020-10-21 02:54:51 +00:00
expected : [ ] string {
2020-11-14 23:50:30 +00:00
"D0, P[], (!!map)::{a: {field: {g: wizz}, fieldA: cat, fieldB: dog}, b: {field: {g: wizz}, fieldB: dog}}\n" ,
2020-10-21 02:54:51 +00:00
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { also: { g: wizz}}, b: { also: me}} ` ,
expression : ` . * { "a":.b} ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { also: { g: wizz}}, b: { also: [1]}} ` ,
expression : ` . * { "a":.b} ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { also: [1]}, b: { also: { g: wizz}}} ` ,
expression : ` . * { "a":.b} ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-06 00:45:18 +00:00
skipDoc : true ,
2020-10-21 02:54:51 +00:00
document : ` { a: { things: great}, b: { also: me}} ` ,
2021-02-08 02:58:46 +00:00
expression : ` . * { "a": .b} ` ,
2020-10-21 02:54:51 +00:00
expected : [ ] string {
"D0, P[], (!!map)::{a: {things: great, also: me}, b: {also: me}}\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-14 23:50:30 +00:00
description : "Merge keeps style of LHS" ,
dontFormatInputForDoc : true ,
2021-02-08 02:58:46 +00:00
document : "a: {things: great}\nb:\n also: \"me\"" ,
expression : ` . * { "a":.b} ` ,
2020-10-21 02:54:51 +00:00
expected : [ ] string {
2021-02-08 02:58:46 +00:00
"D0, P[], (!!map)::a: {things: great, also: \"me\"}\nb:\n also: \"me\"\n" ,
2020-10-21 02:54:51 +00:00
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-06 00:45:18 +00:00
description : "Merge arrays" ,
document : ` { a: [1,2,3], b: [3,4,5]} ` ,
expression : ` . * { "a":.b} ` ,
2020-10-21 02:54:51 +00:00
expected : [ ] string {
"D0, P[], (!!map)::{a: [3, 4, 5], b: [3, 4, 5]}\n" ,
} ,
2020-10-19 05:14:29 +00:00
} ,
2020-11-27 23:41:09 +00:00
{
skipDoc : true ,
document : ` { a: [1], b: [2]} ` ,
expression : ` .a *+ .b ` ,
expected : [ ] string {
"D0, P[a], (!!seq)::[1, 2]\n" ,
} ,
} ,
2021-01-13 05:54:28 +00:00
{
description : "Merge, only existing fields" ,
document : ` { a: { thing: one, cat: frog}, b: { missing: two, thing: two}} ` ,
expression : ` .a *? .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{thing: two, cat: frog}\n" ,
} ,
} ,
2022-01-15 04:48:34 +00:00
{
description : "Merge, only new fields" ,
document : ` { a: { thing: one, cat: frog}, b: { missing: two, thing: two}} ` ,
expression : ` .a *n .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{thing: one, cat: frog, missing: two}\n" ,
} ,
} ,
2021-01-13 05:54:28 +00:00
{
skipDoc : true ,
document : ` { a: [ { thing: one}], b: [ { missing: two, thing: two}]} ` ,
2021-02-18 00:16:54 +00:00
expression : ` .a *?d .b ` ,
2021-01-13 05:54:28 +00:00
expected : [ ] string {
"D0, P[a], (!!seq)::[{thing: two}]\n" ,
} ,
} ,
2022-01-15 04:48:34 +00:00
{
skipDoc : true ,
document : ` { a: [ { thing: one}], b: [ { missing: two, thing: two}]} ` ,
expression : ` .a *nd .b ` ,
expected : [ ] string {
"D0, P[a], (!!seq)::[{thing: one, missing: two}]\n" ,
} ,
} ,
2021-01-18 02:58:46 +00:00
{
skipDoc : true ,
document : ` { a: { array: [1]}, b: { }} ` ,
expression : ` .b *+ .a ` ,
expected : [ ] string {
2022-04-14 22:27:22 +00:00
"D0, P[b], (!!map)::array: [1]\n" ,
2021-01-18 02:58:46 +00:00
} ,
} ,
2020-11-27 23:41:09 +00:00
{
description : "Merge, appending arrays" ,
document : ` { a: { array: [1, 2, animal: dog], value: coconut}, b: { array: [3, 4, animal: cat], value: banana}} ` ,
expression : ` .a *+ .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{array: [1, 2, {animal: dog}, 3, 4, {animal: cat}], value: banana}\n" ,
} ,
} ,
2021-01-13 05:54:28 +00:00
{
description : "Merge, only existing fields, appending arrays" ,
document : ` { a: { thing: [1,2]}, b: { thing: [3,4], another: [1]}} ` ,
expression : ` .a *?+ .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{thing: [1, 2, 3, 4]}\n" ,
} ,
} ,
2022-01-15 04:48:34 +00:00
{
description : "Merge, only new fields, appending arrays" ,
subdescription : "Append (+) with (n) has no effect." ,
skipDoc : true ,
document : ` { a: { thing: [1,2]}, b: { thing: [3,4], another: [1]}} ` ,
expression : ` .a *n+ .b ` ,
expected : [ ] string {
"D0, P[a], (!!map)::{thing: [1, 2], another: [1]}\n" ,
} ,
} ,
2021-02-18 00:16:54 +00:00
{
description : "Merge, deeply merging arrays" ,
2023-03-16 02:39:36 +00:00
subdescription : "Merging arrays deeply means arrays are merged like objects, with indices as their key. In this case, we merge the first item in the array and do nothing with the second." ,
2021-02-18 00:16:54 +00:00
document : ` { a: [ { name: fred, age: 12}, { name: bob, age: 32}], b: [ { name: fred, age: 34}]} ` ,
expression : ` .a *d .b ` ,
expected : [ ] string {
"D0, P[a], (!!seq)::[{name: fred, age: 34}, {name: bob, age: 32}]\n" ,
} ,
} ,
2021-04-29 02:03:56 +00:00
{
2022-02-01 04:11:39 +00:00
description : "Merge arrays of objects together, matching on a key" ,
subdescription : mergeArraysObjectKeysText ,
document : ` { myArray: [ { a: apple, b: appleB}, { a: kiwi, b: kiwiB}, { a: banana, b: bananaB}], something: else} ` ,
document2 : ` newArray: [ { a: banana, c: bananaC}, { a: apple, b: appleB2}, { a: dingo, c: dingoC}] ` ,
environmentVariables : map [ string ] string { "originalPath" : ".myArray" , "otherPath" : ".newArray" , "idPath" : ".a" } ,
expression : mergeExpression ,
2021-04-29 02:03:56 +00:00
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{myArray: [{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC}, {a: dingo, c: dingoC}], something: else}\n" ,
2021-04-29 02:03:56 +00:00
} ,
} ,
2020-10-28 02:00:26 +00:00
{
2020-11-06 00:45:18 +00:00
description : "Merge to prefix an element" ,
document : ` { a: cat, b: dog} ` ,
expression : ` . * { "a": { "c": .a}} ` ,
2020-10-28 02:00:26 +00:00
expected : [ ] string {
2020-11-06 00:45:18 +00:00
"D0, P[], (!!map)::{a: {c: cat}, b: dog}\n" ,
2020-10-28 02:00:26 +00:00
} ,
} ,
2020-10-29 23:56:45 +00:00
{
2020-11-06 00:45:18 +00:00
description : "Merge with simple aliases" ,
document : ` { a: &cat { c: frog}, b: { f: *cat}, c: { g: thongs}} ` ,
expression : ` .c * .b ` ,
2020-10-29 23:56:45 +00:00
expected : [ ] string {
"D0, P[c], (!!map)::{g: thongs, f: *cat}\n" ,
} ,
} ,
{
2021-01-12 22:21:16 +00:00
description : "Merge copies anchor names" ,
2020-11-06 00:45:18 +00:00
document : ` { a: { c: &cat frog}, b: { f: *cat}, c: { g: thongs}} ` ,
expression : ` .c * .a ` ,
2020-10-29 23:56:45 +00:00
expected : [ ] string {
2021-01-12 22:21:16 +00:00
"D0, P[c], (!!map)::{g: thongs, c: &cat frog}\n" ,
2020-10-29 23:56:45 +00:00
} ,
} ,
2020-10-30 01:40:44 +00:00
{
2020-11-06 00:45:18 +00:00
description : "Merge with merge anchors" ,
document : mergeDocSample ,
expression : ` .foobar * .foobarList ` ,
2020-10-30 01:40:44 +00:00
expected : [ ] string {
2021-10-11 03:46:46 +00:00
"D0, P[foobar], (!!map)::c: foobarList_c\n!!merge <<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n" ,
} ,
} ,
{
skipDoc : true ,
document : document ,
expression : ` .b * .c ` ,
expected : [ ] string {
"D0, P[b], (!!map)::{name: dog, <<: *cat}\n" ,
2020-10-30 01:40:44 +00:00
} ,
} ,
2022-01-22 02:47:22 +00:00
{
description : "Custom types: that are really numbers" ,
subdescription : "When custom tags are encountered, yq will try to decode the underlying type." ,
document : "a: !horse 2\nb: !goat 3" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !horse 6\nb: !goat 3\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: that are really numbers" ,
document : "a: !horse 2.5\nb: !goat 3.5" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !horse 8.75\nb: !goat 3.5\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: that are really numbers" ,
document : "a: 2\nb: !goat 3.5" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !!float 7\nb: !goat 3.5\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: that are really arrays" ,
document : "a: !horse [1,2]\nb: !goat [3]" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !horse [3]\nb: !goat [3]\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
{
description : "Custom types: that are really maps" ,
subdescription : "Custom tags will be maintained." ,
document : "a: !horse {cat: meow}\nb: !goat {dog: woof}" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !horse {cat: meow, dog: woof}\nb: !goat {dog: woof}\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
2022-08-29 05:37:25 +00:00
{
description : "Custom types: clobber tags" ,
2023-03-16 02:39:36 +00:00
subdescription : "Use the `c` option to clobber custom tags. Note that the second tag is now used." ,
2022-08-29 05:37:25 +00:00
document : "a: !horse {cat: meow}\nb: !goat {dog: woof}" ,
expression : ".a *=c .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !goat {cat: meow, dog: woof}\nb: !goat {dog: woof}\n" ,
2022-08-29 05:37:25 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: clobber tags - *=" ,
2023-03-16 02:39:36 +00:00
subdescription : "Use the `c` option to clobber custom tags - on both the `=` and `*` operator. Note that the second tag is now used." ,
2022-08-29 05:37:25 +00:00
document : "a: !horse {cat: meow}\nb: !goat {dog: woof}" ,
expression : ".a =c .a *c .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !goat {cat: meow, dog: woof}\nb: !goat {dog: woof}\n" ,
2022-08-29 05:37:25 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: dont clobber tags - *=" ,
2023-03-16 02:39:36 +00:00
subdescription : "Use the `c` option to clobber custom tags - on both the `=` and `*` operator. Note that the second tag is now used." ,
2022-08-29 05:37:25 +00:00
document : "a: !horse {cat: meow}\nb: !goat {dog: woof}" ,
expression : ".a *= .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: !horse {cat: meow, dog: woof}\nb: !goat {dog: woof}\n" ,
2022-08-29 05:37:25 +00:00
} ,
} ,
2022-01-22 02:47:22 +00:00
{
skipDoc : true ,
description : "Custom types: that are really maps" ,
document : "a: {cat: !horse meow}\nb: {cat: 5}" ,
expression : ".a = .a * .b" ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: {cat: !horse 5}\nb: {cat: 5}\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
2022-09-09 02:28:38 +00:00
{
skipDoc : true ,
description : "Relative merge, new fields only" ,
document : "a: {a: original}\n" ,
expression : ` .a *=n load("../../examples/thing.yml") ` ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: {a: original, b: cool.}\n" ,
2022-09-09 02:28:38 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Relative merge" ,
document : "a: {a: original}\n" ,
expression : ` .a *= load("../../examples/thing.yml") ` ,
expected : [ ] string {
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: {a: apple is included, b: cool.}\n" ,
2022-09-09 02:28:38 +00:00
} ,
} ,
2023-01-10 02:48:57 +00:00
{
description : "Merging a null with a map" ,
expression : ` null * { "some": "thing"} ` ,
expected : [ ] string {
"D0, P[], (!!map)::some: thing\n" ,
} ,
} ,
{
description : "Merging a map with null" ,
expression : ` { "some": "thing"} * null ` ,
expected : [ ] string {
"D0, P[], (!!map)::some: thing\n" ,
} ,
} ,
{
description : "Merging an null with an array" ,
expression : ` null * ["some"] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- some\n" ,
} ,
} ,
{
description : "Merging an array with null" ,
expression : ` ["some"] * null ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- some\n" ,
} ,
} ,
{
skipDoc : true ,
expression : ` null * null ` ,
expected : [ ] string {
"D0, P[], (!!null)::null\n" ,
} ,
} ,
2020-10-19 05:14:29 +00:00
}
func TestMultiplyOperatorScenarios ( t * testing . T ) {
for _ , tt := range multiplyOperatorScenarios {
testScenario ( t , & tt )
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios ( t , "multiply-merge" , multiplyOperatorScenarios )
2020-10-19 05:14:29 +00:00
}