Reported by vacuum issue, this use-case is now handled correctly and prevents a panic.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-11-05 09:16:08 -05:00
parent 3fb4865f08
commit ff40cfad85
2 changed files with 16 additions and 0 deletions

View File

@@ -253,6 +253,9 @@ func FindKeyNode(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode
//numNodes := len(nodes)
for i, v := range nodes {
if i%2 == 0 && key == v.Value {
if len(nodes) <= i+1 {
return v, nodes[i]
}
return v, nodes[i+1] // next node is what we need.
}
for x, j := range v.Content {

View File

@@ -317,6 +317,19 @@ func TestFindKeyNode(t *testing.T) {
assert.Equal(t, 47, k.Line)
}
func TestFindKeyNodeOffByOne(t *testing.T) {
k, v := FindKeyNode("key", []*yaml.Node{
{
Value: "key",
Line: 999,
},
})
assert.NotNil(t, k)
assert.NotNil(t, v)
assert.Equal(t, 999, k.Line)
}
func TestFindKeyNode_ValueIsKey(t *testing.T) {
a := &yaml.Node{