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

fix: StringToSliceHookFunc incorrectly triggered when parsing []byte #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions decode_hooks.go
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
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