Skip to content

Commit

Permalink
fix: sanitize with nil config (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulb authored May 10, 2024
1 parent d60e145 commit 0517bf7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ import (
type Config map[string]string

// Sanitize removes leading and trailing spaces from all keys and values in the
// configuration.
// configuration, and makes sure it's not nil.
func (c Config) Sanitize() Config {
if c == nil {
return map[string]string{}
}

for key, val := range c {
delete(c, key)
key = strings.TrimSpace(key)
Expand Down
28 changes: 23 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@ import (
)

func TestConfig_Sanitize(t *testing.T) {
is := is.New(t)
have := Config{" key ": " value "}
want := Config{"key": "value"}
tests := []struct {
name string
have Config
want Config
}{
{
name: "nil config",
have: nil,
want: map[string]string{},
},
{
name: "with spaces",
have: Config{" key ": " value "},
want: Config{"key": "value"},
},
}

have.Sanitize()
is.Equal(have, want)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
is := is.New(t)
got := tt.have.Sanitize()
is.Equal(got, tt.want)
})
}
}

func TestConfig_Validate_ParameterType(t *testing.T) {
Expand Down

0 comments on commit 0517bf7

Please sign in to comment.