(fix): An off-by-one issue caused by a value exactly mapping a key #43

Non breaking change fix for #43
This commit is contained in:
Dave Shanley
2022-12-13 08:52:30 -05:00
parent a9eb20a375
commit f61e731567

View File

@@ -203,8 +203,10 @@ type KeyNodeSearch struct {
// FindKeyNodeTop is a non-recursive search of top level nodes for a key, will not look at content. // FindKeyNodeTop is a non-recursive search of top level nodes for a key, will not look at content.
// Returns the key and value // Returns the key and value
func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) { func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) {
for i, v := range nodes { for i, v := range nodes {
if i%2 != 0 {
continue
}
if strings.ToLower(key) == strings.ToLower(v.Value) { if strings.ToLower(key) == strings.ToLower(v.Value) {
return v, nodes[i+1] // next node is what we need. return v, nodes[i+1] // next node is what we need.
} }
@@ -270,6 +272,9 @@ func FindKeyNodeFull(key string, nodes []*yaml.Node) (keyNode *yaml.Node, labelN
// level of the node and not the children. // level of the node and not the children.
func FindKeyNodeFullTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, labelNode *yaml.Node, valueNode *yaml.Node) { func FindKeyNodeFullTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, labelNode *yaml.Node, valueNode *yaml.Node) {
for i := range nodes { for i := range nodes {
if i%2 != 0 {
continue
}
if i%2 == 0 && key == nodes[i].Value { if i%2 == 0 && key == nodes[i].Value {
return nodes[i], nodes[i], nodes[i+1] // next node is what we need. return nodes[i], nodes[i], nodes[i+1] // next node is what we need.
} }