8000 section: return a zero-value Key for empty names by team-humaki · Pull Request #395 · go-ini/ini · GitHub
[go: up one dir, main page]

Skip to content
Open
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
9 changes: 6 additions & 3 deletions section.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ func (s *Section) HasValue(value string) bool {
func (s *Section) Key(name string) *Key {
key, err := s.GetKey(name)
if err != nil {
// It's OK here because the only possible error is empty key name,
// but if it's empty, this piece of code won't be executed.
key, _ = s.NewKey(name, "")
key, err = s.NewKey(name, "")
if err != nil {
// NewKey rejects empty names. Still honor the zero-value
// contract so Key("") is not nil.
return newKey(s, name, "")
}
return key
}
return key
Expand Down
8 changes: 8 additions & 0 deletions section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func TestSection_Key(t *testing.T) {
assert.Equal(t, "404", k.Name())
})

t.Run("empty key name", func(t *testing.T) {
k := f.Section("").Key("")
require.NotNil(t, k)
assert.Equal(t, "", k.Name())
assert.Equal(t, "", k.Value())
assert.False(t, f.Section("").HasKey(""))
})

t.Run("key exists in parent section", func(t *testing.T) {
k, err := f.Section("parent").NewKey("AGE", "18")
require.NoError(t, err)
Expand Down
0