Fixed potential npe

This commit is contained in:
Mike Farah 2020-09-13 12:14:20 +10:00
parent 4eaadf98d0
commit e5948c4f16
2 changed files with 32 additions and 6 deletions

View File

@ -617,6 +617,27 @@ pointer: *value-pointer`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeMissingCmd(t *testing.T) {
content := `a:
<<: &anchor
c: d
e: f
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -X %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a:
c: d
e: f
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeKeyCmd(t *testing.T) {
content := `name: &nameField Mike
*nameField: Great Guy`

View File

@ -163,6 +163,9 @@ func setIfNotThere(node *yaml.Node, key string, value *yaml.Node) {
}
func applyAlias(node *yaml.Node, alias *yaml.Node) {
if alias == nil {
return
}
for index := 0; index < len(alias.Content); index = index + 2 {
keyNode := alias.Content[index]
log.Debugf("applying alias key %v", keyNode.Value)
@ -185,12 +188,14 @@ func explodeNode(node *yaml.Node) error {
return nil
case yaml.AliasNode:
log.Debugf("its an alias!")
node.Kind = node.Alias.Kind
node.Style = node.Alias.Style
node.Tag = node.Alias.Tag
node.Content = node.Alias.Content
node.Value = node.Alias.Value
node.Alias = nil
if node.Alias != nil {
node.Kind = node.Alias.Kind
node.Style = node.Alias.Style
node.Tag = node.Alias.Tag
node.Content = node.Alias.Content
node.Value = node.Alias.Value
node.Alias = nil
}
return nil
case yaml.MappingNode:
for index := 0; index < len(node.Content); index = index + 2 {