Skip to content

Commit

Permalink
key: ignore empty values in ValueWithShadows
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Feb 10, 2022
1 parent fcd6cc3 commit 3428ffe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
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

0 comments on commit 3428ffe

Please sign in to comment.