Skip to content

Commit b514d22

Browse files
committed
Fix issue in FindEntry that causes extensions and alias crash
1 parent 554250b commit b514d22

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

internal/config/config_map.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,18 @@ func (cm *ConfigMap) FindEntry(key string) (ce *ConfigEntry, err error) {
6868

6969
ce = &ConfigEntry{}
7070

71-
topLevelKeys := cm.Root.Content
72-
for i, v := range topLevelKeys {
71+
// Content slice goes [key1, value1, key2, value2, ...]
72+
topLevelPairs := cm.Root.Content
73+
for i, v := range topLevelPairs {
74+
// Skip every other slice item since we only want to check against keys
75+
if i%2 != 0 {
76+
continue
77+
}
7378
if v.Value == key {
7479
ce.KeyNode = v
7580
ce.Index = i
76-
if i+1 < len(topLevelKeys) {
77-
ce.ValueNode = topLevelKeys[i+1]
81+
if i+1 < len(topLevelPairs) {
82+
ce.ValueNode = topLevelPairs[i+1]
7883
}
7984
return
8085
}

internal/config/config_map_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"gopkg.in/yaml.v3"
9+
)
10+
11+
func TestFindEntry(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
key string
15+
output string
16+
wantErr bool
17+
}{
18+
{
19+
name: "find key",
20+
key: "valid",
21+
output: "present",
22+
},
23+
{
24+
name: "find key that is not present",
25+
key: "invalid",
26+
wantErr: true,
27+
},
28+
{
29+
name: "find key with blank value",
30+
key: "blank",
31+
output: "",
32+
},
33+
{
34+
name: "find key that has same content as a value",
35+
key: "same",
36+
output: "logical",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
cm := ConfigMap{Root: testYaml()}
42+
t.Run(tt.name, func(t *testing.T) {
43+
out, err := cm.FindEntry(tt.key)
44+
if tt.wantErr {
45+
assert.EqualError(t, err, "not found")
46+
return
47+
}
48+
assert.NoError(t, err)
49+
fmt.Println(out)
50+
assert.Equal(t, tt.output, out.ValueNode.Value)
51+
})
52+
}
53+
}
54+
55+
func testYaml() *yaml.Node {
56+
var root yaml.Node
57+
var data = `
58+
valid: present
59+
erroneous: same
60+
blank:
61+
same: logical
62+
`
63+
_ = yaml.Unmarshal([]byte(data), &root)
64+
return root.Content[0]
65+
}

0 commit comments

Comments
 (0)