Skip to content

ValueWithShadows returning a non empty array when there are no values #310

Description

@NDagestad

When getting the values and shadows of a key, if there are no values at all, StringsWithShadows will return an array with an empty string. This does not seem like the right behaviour.
I would expect StringsWithShadows to return an empty array if there are no values. The documentation says that ValueWithShadows returns the raw value so there is a case to be made for it to return an empty string, but I would argue it is more intuitive to return an empty array even there.

Something like this should be enough to fix it from what I can tell (it is good enough in my case at least)

diff --git a/key.go b/key.go
index 0302c29..2147974 100644
--- a/key.go
+++ b/key.go
@@ -113,6 +113,9 @@ func (k *Key) Value() string {
 // ValueWithShadows returns raw values of key and its shadows if any.
 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions