Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,24 @@ func (k *Key) Value() string {
return k.value
}

// ValueWithShadows returns raw values of key and its shadows if any.
// ValueWithShadows returns raw values of key and its shadows if any. Shadow
// keys with empty values are ignored from the returned list.
func (k *Key) ValueWithShadows() []string {
if len(k.shadows) == 0 {
if k.value == "" {
return []string{}
}
return []string{k.value}
}
vals := make([]string, len(k.shadows)+1)
vals[0] = k.value
for i := range k.shadows {
vals[i+1] = k.shadows[i].value

vals := make([]string, 0, len(k.shadows)+1)
if k.value != "" {
vals = append(vals, k.value)
}
for _, s := range k.shadows {
if s.value != "" {
vals = append(vals, s.value)
}
}
return vals
}
Expand Down
7 changes: 7 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func TestKey_AddShadow(t *testing.T) {
assert.NoError(t, k.AddShadow("ini"))
assert.Equal(t, []string{"ini", "ini.v1"}, k.ValueWithShadows())
})

t.Run("ignore empty shadow values", func(t *testing.T) {
k := f.Section("").Key("empty")
assert.NoError(t, k.AddShadow(""))
assert.NoError(t, k.AddShadow("ini"))
assert.Equal(t, []string{"ini"}, k.ValueWithShadows())
})
})

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