mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Auto style when adding to empty maps/arrays
This commit is contained in:
parent
71242b0c8e
commit
c3d815998a
@ -150,7 +150,10 @@ func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignP
|
||||
|
||||
// merge will pickup the style of the new thing
|
||||
// when autocreating nodes
|
||||
if n.Node.Style == 0 {
|
||||
//
|
||||
// if this is an empty map or empty array, use the style of other node.
|
||||
|
||||
if n.Node.Style == 0 || (n.Node.Kind != yaml.ScalarNode && len(n.Node.Content) == 0) {
|
||||
n.Node.Style = other.Node.Style
|
||||
}
|
||||
|
||||
|
@ -141,7 +141,9 @@ func addScalars(target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) error {
|
||||
|
||||
func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) {
|
||||
target.Node.Kind = yaml.SequenceNode
|
||||
target.Node.Style = lhs.Node.Style
|
||||
if len(lhs.Node.Content) > 0 {
|
||||
target.Node.Style = lhs.Node.Style
|
||||
}
|
||||
target.Node.Tag = lhs.Node.Tag
|
||||
target.Node.Content = make([]*yaml.Node, len(lhs.Node.Content))
|
||||
copy(target.Node.Content, lhs.Node.Content)
|
||||
@ -170,6 +172,8 @@ func addMaps(target *CandidateNode, lhsC *CandidateNode, rhsC *CandidateNode) {
|
||||
}
|
||||
}
|
||||
target.Node.Kind = yaml.MappingNode
|
||||
target.Node.Style = lhs.Style
|
||||
if len(lhs.Content) > 0 {
|
||||
target.Node.Style = lhs.Style
|
||||
}
|
||||
target.Node.Tag = lhs.Tag
|
||||
}
|
||||
|
@ -62,6 +62,78 @@ var addOperatorScenarios = []expressionScenario{
|
||||
"D0, P[a], (!!seq)::[1, 2]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to empty array",
|
||||
document: `{a: []}`,
|
||||
expression: `.a + "cat"`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!seq)::- cat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to existing array",
|
||||
document: `{a: [dog]}`,
|
||||
expression: `.a + "cat"`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!seq)::[dog, cat]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to empty array",
|
||||
document: `a: []`,
|
||||
expression: `.a += "cat"`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a:\n - cat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to existing array",
|
||||
document: `a: [dog]`,
|
||||
expression: `.a += "cat"`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: [dog, cat]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to empty object",
|
||||
document: `{a: {}}`,
|
||||
expression: `.a + {"b": "cat"}`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!map)::b: cat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to existing object",
|
||||
document: `{a: {c: dog}}`,
|
||||
expression: `.a + {"b": "cat"}`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!map)::{c: dog, b: cat}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to empty object in place",
|
||||
document: `a: {}`,
|
||||
expression: `.a += {"b": "cat"}`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a:\n b: cat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Concatenate to existing object in place",
|
||||
document: `a: {c: dog}`,
|
||||
expression: `.a += {"b": "cat"}`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: {c: dog, b: cat}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Add new object to array",
|
||||
document: `a: [{dog: woof}]`,
|
||||
|
@ -19,7 +19,7 @@ var collectObjectOperatorScenarios = []expressionScenario{
|
||||
document: "a: []",
|
||||
expression: `.a += [{"key": "att2", "value": "val2"}]`,
|
||||
expected: []string{
|
||||
"D0, P[], (doc)::a: [{key: att2, value: val2}]\n",
|
||||
"D0, P[], (doc)::a:\n - key: att2\n value: val2\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user