From cc74b7fe235b35360fdb4a131c36ba52cb18c54f Mon Sep 17 00:00:00 2001 From: fioepq9 Date: Mon, 29 May 2023 21:14:24 +0800 Subject: [PATCH] fix: StringToSliceHookFunc incorrectly triggered when parsing []byte --- decode_hooks.go | 9 ++++++--- decode_hooks_test.go | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/decode_hooks.go b/decode_hooks.go index 3a754ca7..f4941ac5 100644 --- a/decode_hooks.go +++ b/decode_hooks.go @@ -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 } diff --git a/decode_hooks_test.go b/decode_hooks_test.go index bf029526..0478a2bb 100644 --- a/decode_hooks_test.go +++ b/decode_hooks_test.go @@ -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"),