Skip to content

Commit 2cb620d

Browse files
Merge pull request #52 from mahadzaryab1/reflect-value-hook
[enhancement] Add check for `reflect.Value` in `ComposeDecodeHookFunc`
2 parents 0b972d9 + 5d61bd5 commit 2cb620d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

decode_hooks.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
100100
if err != nil {
101101
return nil, err
102102
}
103-
newFrom = reflect.ValueOf(data)
103+
if v, ok := data.(reflect.Value); ok {
104+
newFrom = v
105+
} else {
106+
newFrom = reflect.ValueOf(data)
107+
}
104108
}
105109

106110
return data, nil

decode_hooks_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,36 @@ func TestComposeDecodeHookFunc_safe_nofuncs(t *testing.T) {
219219
}
220220
}
221221

222+
func TestComposeDecodeHookFunc_ReflectValueHook(t *testing.T) {
223+
reflectValueHook := func(
224+
f reflect.Kind,
225+
t reflect.Kind,
226+
data interface{},
227+
) (interface{}, error) {
228+
new := data.(string) + "foo"
229+
return reflect.ValueOf(new), nil
230+
}
231+
232+
stringHook := func(
233+
f reflect.Kind,
234+
t reflect.Kind,
235+
data interface{},
236+
) (interface{}, error) {
237+
return data.(string) + "bar", nil
238+
}
239+
240+
f := ComposeDecodeHookFunc(reflectValueHook, stringHook)
241+
242+
result, err := DecodeHookExec(
243+
f, reflect.ValueOf(""), reflect.ValueOf([]byte("")))
244+
if err != nil {
245+
t.Fatalf("bad: %s", err)
246+
}
247+
if result.(string) != "foobar" {
248+
t.Fatalf("bad: %#v", result)
249+
}
250+
}
251+
222252
func TestStringToSliceHookFunc(t *testing.T) {
223253
f := StringToSliceHookFunc(",")
224254

0 commit comments

Comments
 (0)