From 42691d328d862093357eef6218b844f79ea629ed Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 16 Nov 2024 15:50:25 -0500 Subject: [PATCH 1/3] Add Check For Data Being a reflect.Value Type Signed-off-by: Mahad Zaryab --- decode_hooks.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/decode_hooks.go b/decode_hooks.go index 1f3c69d..7af1aa9 100644 --- a/decode_hooks.go +++ b/decode_hooks.go @@ -100,7 +100,12 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc { if err != nil { return nil, err } - newFrom = reflect.ValueOf(data) + if v, ok := data.(reflect.Value); ok { + newFrom = v + data = v.Interface() + } else { + newFrom = reflect.ValueOf(data) + } } return data, nil From 8ded786a1df79cb1b9f7badd83dedef6a1f997b9 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 16 Nov 2024 15:50:50 -0500 Subject: [PATCH 2/3] Add Unit Test Signed-off-by: Mahad Zaryab --- decode_hooks_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/decode_hooks_test.go b/decode_hooks_test.go index 6549ae0..a4ec2a0 100644 --- a/decode_hooks_test.go +++ b/decode_hooks_test.go @@ -219,6 +219,36 @@ func TestComposeDecodeHookFunc_safe_nofuncs(t *testing.T) { } } +func TestComposeDecodeHookFunc_ReflectValueHook(t *testing.T) { + reflectValueHook := func( + f reflect.Kind, + t reflect.Kind, + data interface{}, + ) (interface{}, error) { + new := data.(string) + "foo" + return reflect.ValueOf(new), nil + } + + stringHook := func( + f reflect.Kind, + t reflect.Kind, + data interface{}, + ) (interface{}, error) { + return data.(string) + "bar", nil + } + + f := ComposeDecodeHookFunc(reflectValueHook, stringHook) + + result, err := DecodeHookExec( + f, reflect.ValueOf(""), reflect.ValueOf([]byte(""))) + if err != nil { + t.Fatalf("bad: %s", err) + } + if result.(string) != "foobar" { + t.Fatalf("bad: %#v", result) + } +} + func TestStringToSliceHookFunc(t *testing.T) { f := StringToSliceHookFunc(",") From 5d61bd5a6d75d556ad7ffa27f551f0614aed63d8 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 16 Nov 2024 16:06:54 -0500 Subject: [PATCH 3/3] Remove Interface Call Signed-off-by: Mahad Zaryab --- decode_hooks.go | 1 - 1 file changed, 1 deletion(-) diff --git a/decode_hooks.go b/decode_hooks.go index 7af1aa9..76f76bc 100644 --- a/decode_hooks.go +++ b/decode_hooks.go @@ -102,7 +102,6 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc { } if v, ok := data.(reflect.Value); ok { newFrom = v - data = v.Interface() } else { newFrom = reflect.ValueOf(data) }