Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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