Skip to content

Commit

Permalink
Merge pull request mitchellh#6 from go-viper/fix-string-to-slice
Browse files Browse the repository at this point in the history
fix: StringToSliceHookFunc incorrectly triggered when parsing []byte
  • Loading branch information
sagikazarmark committed Dec 18, 2023
2 parents 69bb54a + eee8faa commit 524a9c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 6 additions & 3 deletions decode_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ func OrComposeDecodeHookFunc(ff ...DecodeHookFunc) DecodeHookFunc {
// string to []string by splitting on the given sep.
func StringToSliceHookFunc(sep string) DecodeHookFunc {
return func(
f reflect.Kind,
t reflect.Kind,
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
if f != reflect.String || t != reflect.Slice {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.SliceOf(f) {
return data, nil
}

Expand Down
5 changes: 3 additions & 2 deletions decode_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,14 @@ func TestStringToSliceHookFunc(t *testing.T) {
f := StringToSliceHookFunc(",")

strValue := reflect.ValueOf("42")
sliceValue := reflect.ValueOf([]byte("42"))
sliceValue := reflect.ValueOf([]string{"42"})
cases := []struct {
f, t reflect.Value
result interface{}
err bool
}{
{sliceValue, sliceValue, []byte("42"), false},
{sliceValue, sliceValue, []string{"42"}, false},
{reflect.ValueOf([]byte("42")), reflect.ValueOf([]byte{}), []byte("42"), false},
{strValue, strValue, "42", false},
{
reflect.ValueOf("foo,bar,baz"),
Expand Down

0 comments on commit 524a9c9

Please sign in to comment.