Skip to content

Commit

Permalink
now assumes yaml idiomatic formatting when adding to empty maps/arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Apr 14, 2022
1 parent ea66a73 commit b11075e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
4 changes: 3 additions & 1 deletion pkg/yqlib/doc/operators/add.md
Expand Up @@ -135,7 +135,9 @@ a:
b:
- dog
- mouse
a3: {b: [mouse]}
a3:
b:
- mouse
```

## String concatenation
Expand Down
7 changes: 5 additions & 2 deletions pkg/yqlib/doc/operators/assign-update.md
Expand Up @@ -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
Expand Down Expand Up @@ -229,6 +230,8 @@ yq '.a.b.[0] |= "bogs"' sample.yml
```
will output
```yaml
{a: {b: [bogs]}}
a:
b:
- bogs
```

10 changes: 5 additions & 5 deletions pkg/yqlib/operator_assign_test.go
Expand Up @@ -17,15 +17,15 @@ var assignOperatorScenarios = []expressionScenario{
document: "{}",
expression: `.a |= .b`,
expected: []string{
"D0, P[], (doc)::{a: null}\n",
"D0, P[], (doc)::a: null\n",
},
},
{
skipDoc: true,
document: "{}",
expression: `.a = .b`,
expected: []string{
"D0, P[], (doc)::{a: null}\n",
"D0, P[], (doc)::a: null\n",
},
},
{
Expand Down Expand Up @@ -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",
},
},
{
Expand All @@ -197,15 +197,15 @@ 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",
},
},
{
skipDoc: true,
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",
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/yqlib/operator_collect_object_test.go
Expand Up @@ -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",
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/operator_multiply_test.go
Expand Up @@ -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",
},
},
{
Expand Down Expand Up @@ -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",
},
},
{
Expand Down
12 changes: 11 additions & 1 deletion pkg/yqlib/operator_traverse_path.go
Expand Up @@ -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))
}
Expand All @@ -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]))
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/yqlib/operator_traverse_path_test.go
Expand Up @@ -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`,
Expand Down

0 comments on commit b11075e

Please sign in to comment.