From b11075ec9879c0bb6fdb08278d6bb460cd5e9ba5 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 15 Apr 2022 08:27:22 +1000 Subject: [PATCH] now assumes yaml idiomatic formatting when adding to empty maps/arrays --- pkg/yqlib/doc/operators/add.md | 4 +++- pkg/yqlib/doc/operators/assign-update.md | 7 +++++-- pkg/yqlib/operator_assign_test.go | 10 +++++----- pkg/yqlib/operator_collect_object_test.go | 8 ++++---- pkg/yqlib/operator_multiply_test.go | 4 ++-- pkg/yqlib/operator_traverse_path.go | 12 +++++++++++- pkg/yqlib/operator_traverse_path_test.go | 16 ++++++++++++++++ 7 files changed, 46 insertions(+), 15 deletions(-) diff --git a/pkg/yqlib/doc/operators/add.md b/pkg/yqlib/doc/operators/add.md index 52452ef4a2..e6c3e099d2 100644 --- a/pkg/yqlib/doc/operators/add.md +++ b/pkg/yqlib/doc/operators/add.md @@ -135,7 +135,9 @@ a: b: - dog - mouse - a3: {b: [mouse]} + a3: + b: + - mouse ``` ## String concatenation diff --git a/pkg/yqlib/doc/operators/assign-update.md b/pkg/yqlib/doc/operators/assign-update.md index d3a9a6b827..ff6b08601f 100644 --- a/pkg/yqlib/doc/operators/assign-update.md +++ b/pkg/yqlib/doc/operators/assign-update.md @@ -199,7 +199,8 @@ yq '.a.b |= "bogs"' sample.yml ``` will output ```yaml -{a: {b: bogs}} +a: + b: bogs ``` ## Update node value that has an anchor @@ -229,6 +230,8 @@ yq '.a.b.[0] |= "bogs"' sample.yml ``` will output ```yaml -{a: {b: [bogs]}} +a: + b: + - bogs ``` diff --git a/pkg/yqlib/operator_assign_test.go b/pkg/yqlib/operator_assign_test.go index 74b6d77b2b..8b28c7bbd8 100644 --- a/pkg/yqlib/operator_assign_test.go +++ b/pkg/yqlib/operator_assign_test.go @@ -17,7 +17,7 @@ var assignOperatorScenarios = []expressionScenario{ document: "{}", expression: `.a |= .b`, expected: []string{ - "D0, P[], (doc)::{a: null}\n", + "D0, P[], (doc)::a: null\n", }, }, { @@ -25,7 +25,7 @@ var assignOperatorScenarios = []expressionScenario{ document: "{}", expression: `.a = .b`, expected: []string{ - "D0, P[], (doc)::{a: null}\n", + "D0, P[], (doc)::a: null\n", }, }, { @@ -178,7 +178,7 @@ var assignOperatorScenarios = []expressionScenario{ document: `{}`, expression: `.a.b |= "bogs"`, expected: []string{ - "D0, P[], (doc)::{a: {b: bogs}}\n", + "D0, P[], (doc)::a:\n b: bogs\n", }, }, { @@ -197,7 +197,7 @@ var assignOperatorScenarios = []expressionScenario{ document: `{}`, expression: `.a.b.[0] |= "bogs"`, expected: []string{ - "D0, P[], (doc)::{a: {b: [bogs]}}\n", + "D0, P[], (doc)::a:\n b:\n - bogs\n", }, }, { @@ -205,7 +205,7 @@ var assignOperatorScenarios = []expressionScenario{ document: `{}`, expression: `.a.b.[1].c |= "bogs"`, expected: []string{ - "D0, P[], (doc)::{a: {b: [null, {c: bogs}]}}\n", + "D0, P[], (doc)::a:\n b:\n - null\n - c: bogs\n", }, }, } diff --git a/pkg/yqlib/operator_collect_object_test.go b/pkg/yqlib/operator_collect_object_test.go index a58a81635e..6c1cf743a2 100644 --- a/pkg/yqlib/operator_collect_object_test.go +++ b/pkg/yqlib/operator_collect_object_test.go @@ -115,12 +115,12 @@ var collectObjectOperatorScenarios = []expressionScenario{ }, { skipDoc: true, - document: `{name: Mike, pets: {cows: [apl, bba]}}`, - document2: `{name: Rosey, pets: {sheep: [frog, meow]}}`, + document: "name: Mike\npets:\n cows:\n - apl\n - bba", + document2: "name: Rosey\npets:\n sheep:\n - frog\n - meow", expression: `{"a":.name, "b":.pets}`, expected: []string{ - "D0, P[], (!!map)::a: Mike\nb: {cows: [apl, bba]}\n", - "D0, P[], (!!map)::a: Rosey\nb: {sheep: [frog, meow]}\n", + "D0, P[], (!!map)::a: Mike\nb:\n cows:\n - apl\n - bba\n", + "D0, P[], (!!map)::a: Rosey\nb:\n sheep:\n - frog\n - meow\n", }, }, { diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index 8b4236de3d..431a98da53 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -239,7 +239,7 @@ var multiplyOperatorScenarios = []expressionScenario{ document: `{a: &a { b: &b { c: &c cat } } }`, expression: `{} * .`, expected: []string{ - "D0, P[], (!!map)::{a: &a {b: &b {c: &c cat}}}\n", + "D0, P[], (!!map)::a: &a\n b: &b\n c: &c cat\n", }, }, { @@ -388,7 +388,7 @@ var multiplyOperatorScenarios = []expressionScenario{ document: `{a: {array: [1]}, b: {}}`, expression: `.b *+ .a`, expected: []string{ - "D0, P[b], (!!map)::{array: [1]}\n", + "D0, P[b], (!!map)::array: [1]\n", }, }, { diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index 759c00933b..baaa4d27ec 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -198,6 +198,11 @@ func traverseArrayWithIndices(candidate *CandidateNode, indices []*yaml.Node, pr indexToUse := index contentLength := int64(len(node.Content)) for contentLength <= index { + if contentLength == 0 { + // default to nice yaml formating + node.Style = 0 + } + node.Content = append(node.Content, &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode, Value: "null"}) contentLength = int64(len(node.Content)) } @@ -207,7 +212,7 @@ func traverseArrayWithIndices(candidate *CandidateNode, indices []*yaml.Node, pr } if indexToUse < 0 { - return nil, fmt.Errorf("Index [%v] out of range, array size is %v", index, contentLength) + return nil, fmt.Errorf("index [%v] out of range, array size is %v", index, contentLength) } newMatches.PushBack(candidate.CreateChildInArray(int(index), node.Content[indexToUse])) @@ -232,6 +237,11 @@ func traverseMap(context Context, matchingNode *CandidateNode, key string, prefs valueNode := &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode, Value: "null"} keyNode := &yaml.Node{Kind: yaml.ScalarNode, Value: key} node := matchingNode.Node + + if len(node.Content) == 0 { + node.Style = 0 + } + node.Content = append(node.Content, keyNode, valueNode) if prefs.IncludeMapKeys { diff --git a/pkg/yqlib/operator_traverse_path_test.go b/pkg/yqlib/operator_traverse_path_test.go index 94400ba213..d0addbb615 100644 --- a/pkg/yqlib/operator_traverse_path_test.go +++ b/pkg/yqlib/operator_traverse_path_test.go @@ -35,6 +35,22 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[0 0], (!!int)::1\n", }, }, + { + skipDoc: true, + document: `blah: {}`, + expression: `.blah.cat = "cool"`, + expected: []string{ + "D0, P[], (doc)::blah:\n cat: cool\n", + }, + }, + { + skipDoc: true, + document: `blah: []`, + expression: `.blah.0 = "cool"`, + expected: []string{ + "D0, P[], (doc)::blah:\n - cool\n", + }, + }, { skipDoc: true, document: `b: cat`,