fix panic when fieldspec refers an empty value

This commit is contained in:
Donny Xia
2020-08-07 10:47:25 -07:00
parent 6afabf26ae
commit a2693d0249
3 changed files with 61 additions and 0 deletions

View File

@@ -467,6 +467,10 @@ func (s FieldSetter) Filter(rn *RNode) (*RNode, error) {
if err := ErrorIfInvalid(rn, yaml.ScalarNode); err != nil {
return rn, err
}
// value is nil or null
if IsMissingOrNull(s.Value) {
return rn, nil
}
// only apply the style if there is not an existing style
// or we want to override it
if !s.OverrideStyle || s.Value.YNode().Style == 0 {

View File

@@ -395,6 +395,19 @@ bar: buz
assert.Equal(t, `baz
`, assertNoErrorString(t)(k.String()))
// Empty value
node, err = Parse(`
foo
`)
assert.NoError(t, err)
instance = FieldSetter{}
k, err = instance.Filter(node)
assert.NoError(t, err)
assert.Equal(t, `foo
`, assertNoErrorString(t)(node.String()))
assert.Equal(t, `foo
`, assertNoErrorString(t)(k.String()))
// Encounter error
node, err = Parse(`
-a