diff --git a/pkg/yqlib/doc/operators/add.md b/pkg/yqlib/doc/operators/add.md index 52452ef4..e6c3e099 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 d3a9a6b8..ff6b0860 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 74b6d77b..8b28c7bb 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 a58a8163..6c1cf743 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 8b4236de..431a98da 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 759c0093..baaa4d27 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 94400ba2..d0addbb6 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`,