diff --git a/decode_hooks.go b/decode_hooks.go index 1f3c69d4..76f76bc1 100644 --- a/decode_hooks.go +++ b/decode_hooks.go @@ -100,7 +100,11 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc { if err != nil { return nil, err } - newFrom = reflect.ValueOf(data) + if v, ok := data.(reflect.Value); ok { + newFrom = v + } else { + newFrom = reflect.ValueOf(data) + } } return data, nil diff --git a/decode_hooks_test.go b/decode_hooks_test.go index 6549ae09..a4ec2a00 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(",")