mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
now assumes yaml idiomatic formatting when adding to empty maps/arrays
This commit is contained in:
parent
ea66a73f43
commit
b11075ec98
@ -135,7 +135,9 @@ a:
|
||||
b:
|
||||
- dog
|
||||
- mouse
|
||||
a3: {b: [mouse]}
|
||||
a3:
|
||||
b:
|
||||
- mouse
|
||||
```
|
||||
|
||||
## String concatenation
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
@ -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",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -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 {
|
||||
|
@ -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`,
|
||||
|
Loading…
Reference in New Issue
Block a user