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

Return an empty array from ValueWithShadows if there is none #313

Merged
merged 7 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*_test.go]
trim_trailing_whitespace = false
unknwon marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func (k *Key) Value() string {

// ValueWithShadows returns raw values of key and its shadows if any.
func (k *Key) ValueWithShadows() []string {
if !k.s.HasKey(k.name) {
return []string{}
}
if len(k.shadows) == 0 {
return []string{k.value}
}
Expand Down
19 changes: 19 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,25 @@ func TestKey_Helpers(t *testing.T) {
})
}

func testKey_ValueWithShadows(t *testing.T) {
t.Run("", func(t *testing.T) {
f, err := ShadowLoad([]byte(`
keyName = value1
keyName = value2
`))
require.NoError(t, err)
require.NotNil(t, f)

k := f.Section("").Key("FakeKey")
require.NotNil(t, k)
assert.Equal(t, []string{}, k.ValueWithShadows())

k = f.Section("").Key("keyName")
require.NotNil(t, k)
assert.Equal(t, []string{"value1", "value2"}, k.ValueWithShadows())
})
}

func TestKey_StringsWithShadows(t *testing.T) {
t.Run("get strings of shadows of a key", func(t *testing.T) {
f, err := ShadowLoad([]byte(""))
Expand Down