Skip to content

Commit 39f9e49

Browse files
authored
key: ignore empty values in ValueWithShadows (#316)
1 parent fcd6cc3 commit 39f9e49

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

key.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,24 @@ func (k *Key) Value() string {
110110
return k.value
111111
}
112112

113-
// ValueWithShadows returns raw values of key and its shadows if any.
113+
// ValueWithShadows returns raw values of key and its shadows if any. Shadow
114+
// keys with empty values are ignored from the returned list.
114115
func (k *Key) ValueWithShadows() []string {
115116
if len(k.shadows) == 0 {
116117
if k.value == "" {
117118
return []string{}
118119
}
119120
return []string{k.value}
120121
}
121-
vals := make([]string, len(k.shadows)+1)
122-
vals[0] = k.value
123-
for i := range k.shadows {
124-
vals[i+1] = k.shadows[i].value
122+
123+
vals := make([]string, 0, len(k.shadows)+1)
124+
if k.value != "" {
125+
vals = append(vals, k.value)
126+
}
127+
for _, s := range k.shadows {
128+
if s.value != "" {
129+
vals = append(vals, s.value)
130+
}
125131
}
126132
return vals
127133
}

key_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func TestKey_AddShadow(t *testing.T) {
5757
assert.NoError(t, k.AddShadow("ini"))
5858
assert.Equal(t, []string{"ini", "ini.v1"}, k.ValueWithShadows())
5959
})
60+
61+
t.Run("ignore empty shadow values", func(t *testing.T) {
62+
k := f.Section("").Key("empty")
63+
assert.NoError(t, k.AddShadow(""))
64+
assert.NoError(t, k.AddShadow("ini"))
65+
assert.Equal(t, []string{"ini"}, k.ValueWithShadows())
66+
})
6067
})
6168

6269
t.Run("allow duplicate shadowed values", func(t *testing.T) {

0 commit comments

Comments
 (0)