Skip to content

Commit

Permalink
fix: StringToSliceHookFunc incorrectly triggered when parsing []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
tlipoca9 committed May 29, 2023
1 parent bf980b3 commit cc74b7f
Show file tree
Hide file tree
Showing 2 changed files with 8 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 @@ -103,10 +103,13 @@ 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
4 changes: 2 additions & 2 deletions decode_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ 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},
{strValue, strValue, "42", false},
{
reflect.ValueOf("foo,bar,baz"),
Expand Down

0 comments on commit cc74b7f

Please sign in to comment.