mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22: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:
|
b:
|
||||||
- dog
|
- dog
|
||||||
- mouse
|
- mouse
|
||||||
a3: {b: [mouse]}
|
a3:
|
||||||
|
b:
|
||||||
|
- mouse
|
||||||
```
|
```
|
||||||
|
|
||||||
## String concatenation
|
## String concatenation
|
||||||
|
@ -199,7 +199,8 @@ yq '.a.b |= "bogs"' sample.yml
|
|||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
{a: {b: bogs}}
|
a:
|
||||||
|
b: bogs
|
||||||
```
|
```
|
||||||
|
|
||||||
## Update node value that has an anchor
|
## Update node value that has an anchor
|
||||||
@ -229,6 +230,8 @@ yq '.a.b.[0] |= "bogs"' sample.yml
|
|||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
{a: {b: [bogs]}}
|
a:
|
||||||
|
b:
|
||||||
|
- bogs
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ var assignOperatorScenarios = []expressionScenario{
|
|||||||
document: "{}",
|
document: "{}",
|
||||||
expression: `.a |= .b`,
|
expression: `.a |= .b`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (doc)::{a: null}\n",
|
"D0, P[], (doc)::a: null\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -25,7 +25,7 @@ var assignOperatorScenarios = []expressionScenario{
|
|||||||
document: "{}",
|
document: "{}",
|
||||||
expression: `.a = .b`,
|
expression: `.a = .b`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (doc)::{a: null}\n",
|
"D0, P[], (doc)::a: null\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -178,7 +178,7 @@ var assignOperatorScenarios = []expressionScenario{
|
|||||||
document: `{}`,
|
document: `{}`,
|
||||||
expression: `.a.b |= "bogs"`,
|
expression: `.a.b |= "bogs"`,
|
||||||
expected: []string{
|
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: `{}`,
|
document: `{}`,
|
||||||
expression: `.a.b.[0] |= "bogs"`,
|
expression: `.a.b.[0] |= "bogs"`,
|
||||||
expected: []string{
|
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: `{}`,
|
document: `{}`,
|
||||||
expression: `.a.b.[1].c |= "bogs"`,
|
expression: `.a.b.[1].c |= "bogs"`,
|
||||||
expected: []string{
|
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,
|
skipDoc: true,
|
||||||
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
document: "name: Mike\npets:\n cows:\n - apl\n - bba",
|
||||||
document2: `{name: Rosey, pets: {sheep: [frog, meow]}}`,
|
document2: "name: Rosey\npets:\n sheep:\n - frog\n - meow",
|
||||||
expression: `{"a":.name, "b":.pets}`,
|
expression: `{"a":.name, "b":.pets}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::a: Mike\nb: {cows: [apl, bba]}\n",
|
"D0, P[], (!!map)::a: Mike\nb:\n cows:\n - apl\n - bba\n",
|
||||||
"D0, P[], (!!map)::a: Rosey\nb: {sheep: [frog, meow]}\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 } } }`,
|
document: `{a: &a { b: &b { c: &c cat } } }`,
|
||||||
expression: `{} * .`,
|
expression: `{} * .`,
|
||||||
expected: []string{
|
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: {}}`,
|
document: `{a: {array: [1]}, b: {}}`,
|
||||||
expression: `.b *+ .a`,
|
expression: `.b *+ .a`,
|
||||||
expected: []string{
|
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
|
indexToUse := index
|
||||||
contentLength := int64(len(node.Content))
|
contentLength := int64(len(node.Content))
|
||||||
for contentLength <= index {
|
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"})
|
node.Content = append(node.Content, &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode, Value: "null"})
|
||||||
contentLength = int64(len(node.Content))
|
contentLength = int64(len(node.Content))
|
||||||
}
|
}
|
||||||
@ -207,7 +212,7 @@ func traverseArrayWithIndices(candidate *CandidateNode, indices []*yaml.Node, pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
if indexToUse < 0 {
|
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]))
|
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"}
|
valueNode := &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode, Value: "null"}
|
||||||
keyNode := &yaml.Node{Kind: yaml.ScalarNode, Value: key}
|
keyNode := &yaml.Node{Kind: yaml.ScalarNode, Value: key}
|
||||||
node := matchingNode.Node
|
node := matchingNode.Node
|
||||||
|
|
||||||
|
if len(node.Content) == 0 {
|
||||||
|
node.Style = 0
|
||||||
|
}
|
||||||
|
|
||||||
node.Content = append(node.Content, keyNode, valueNode)
|
node.Content = append(node.Content, keyNode, valueNode)
|
||||||
|
|
||||||
if prefs.IncludeMapKeys {
|
if prefs.IncludeMapKeys {
|
||||||
|
@ -35,6 +35,22 @@ var traversePathOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[0 0], (!!int)::1\n",
|
"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,
|
skipDoc: true,
|
||||||
document: `b: cat`,
|
document: `b: cat`,
|
||||||
|
Loading…
Reference in New Issue
Block a user