Fixed handling of merging null #1501

This commit is contained in:
Mike Farah 2023-01-10 13:48:57 +11:00
parent ef2064c753
commit 9af55d555b
3 changed files with 83 additions and 2 deletions

View File

@ -499,3 +499,43 @@ b: !goat
dog: woof dog: woof
``` ```
## Merging a null with a map
Running
```bash
yq --null-input 'null * {"some": "thing"}'
```
will output
```yaml
some: thing
```
## Merging a map with null
Running
```bash
yq --null-input '{"some": "thing"} * null'
```
will output
```yaml
some: thing
```
## Merging an null with an array
Running
```bash
yq --null-input 'null * ["some"]'
```
will output
```yaml
- some
```
## Merging an array with null
Running
```bash
yq --null-input '["some"] * null'
```
will output
```yaml
- some
```

View File

@ -60,8 +60,14 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex
log.Debugf("Multiplying LHS: %v", lhs.Node.Tag) log.Debugf("Multiplying LHS: %v", lhs.Node.Tag)
log.Debugf("- RHS: %v", rhs.Node.Tag) log.Debugf("- RHS: %v", rhs.Node.Tag)
if lhs.Node.Kind == yaml.MappingNode && rhs.Node.Kind == yaml.MappingNode || if rhs.Node.Tag == "!!null" {
(lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) { return lhs.Copy()
}
if (lhs.Node.Kind == yaml.MappingNode && rhs.Node.Kind == yaml.MappingNode) ||
(lhs.Node.Tag == "!!null" && rhs.Node.Kind == yaml.MappingNode) ||
(lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) ||
(lhs.Node.Tag == "!!null" && rhs.Node.Kind == yaml.SequenceNode) {
var newBlank = CandidateNode{} var newBlank = CandidateNode{}
err := copier.CopyWithOption(&newBlank, lhs, copier.Option{IgnoreEmpty: true, DeepCopy: true}) err := copier.CopyWithOption(&newBlank, lhs, copier.Option{IgnoreEmpty: true, DeepCopy: true})
if err != nil { if err != nil {

View File

@ -579,6 +579,41 @@ var multiplyOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::a: {a: apple is included, b: cool.}\n", "D0, P[], (doc)::a: {a: apple is included, b: cool.}\n",
}, },
}, },
{
description: "Merging a null with a map",
expression: `null * {"some": "thing"}`,
expected: []string{
"D0, P[], (!!map)::some: thing\n",
},
},
{
description: "Merging a map with null",
expression: `{"some": "thing"} * null`,
expected: []string{
"D0, P[], (!!map)::some: thing\n",
},
},
{
description: "Merging an null with an array",
expression: `null * ["some"]`,
expected: []string{
"D0, P[], (!!seq)::- some\n",
},
},
{
description: "Merging an array with null",
expression: `["some"] * null`,
expected: []string{
"D0, P[], (!!seq)::- some\n",
},
},
{
skipDoc: true,
expression: `null * null`,
expected: []string{
"D0, P[], (!!null)::null\n",
},
},
} }
func TestMultiplyOperatorScenarios(t *testing.T) { func TestMultiplyOperatorScenarios(t *testing.T) {