Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key: ignore empty values in ValueWithShadows #316

Merged
merged 1 commit into from Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions key.go
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
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