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

Keep pointers and interfaces returned by hook #230

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
69 changes: 40 additions & 29 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,46 +425,57 @@ func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) e
return nil
}

hooked := false
if d.config.DecodeHook != nil {
// We have a DecodeHook, so let's pre-process the input.
var err error
input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal)
if err != nil {
return fmt.Errorf("error decoding '%s': %s", name, err)
}
if input != nil && reflect.TypeOf(input).Comparable() &&
inputVal.Type().Comparable() &&
input != inputVal.Interface() {
//hook was changed input value
hooked = true
}
}

var err error
outputKind := getKind(outVal)
addMetaKey := true
switch outputKind {
case reflect.Bool:
err = d.decodeBool(name, input, outVal)
case reflect.Interface:
err = d.decodeBasic(name, input, outVal)
case reflect.String:
err = d.decodeString(name, input, outVal)
case reflect.Int:
err = d.decodeInt(name, input, outVal)
case reflect.Uint:
err = d.decodeUint(name, input, outVal)
case reflect.Float32:
err = d.decodeFloat(name, input, outVal)
case reflect.Struct:
err = d.decodeStruct(name, input, outVal)
case reflect.Map:
err = d.decodeMap(name, input, outVal)
case reflect.Ptr:
addMetaKey, err = d.decodePtr(name, input, outVal)
case reflect.Slice:
err = d.decodeSlice(name, input, outVal)
case reflect.Array:
err = d.decodeArray(name, input, outVal)
case reflect.Func:
err = d.decodeFunc(name, input, outVal)
default:
// If we reached this point then we weren't able to decode it
return fmt.Errorf("%s: unsupported type: %s", name, outputKind)
if hooked && reflect.TypeOf(input).AssignableTo(outVal.Type()) {
outVal.Set(reflect.ValueOf(input))
} else {
outputKind := getKind(outVal)
switch outputKind {
case reflect.Bool:
err = d.decodeBool(name, input, outVal)
case reflect.Interface:
err = d.decodeBasic(name, input, outVal)
case reflect.String:
err = d.decodeString(name, input, outVal)
case reflect.Int:
err = d.decodeInt(name, input, outVal)
case reflect.Uint:
err = d.decodeUint(name, input, outVal)
case reflect.Float32:
err = d.decodeFloat(name, input, outVal)
case reflect.Struct:
err = d.decodeStruct(name, input, outVal)
case reflect.Map:
err = d.decodeMap(name, input, outVal)
case reflect.Ptr:
addMetaKey, err = d.decodePtr(name, input, outVal)
case reflect.Slice:
err = d.decodeSlice(name, input, outVal)
case reflect.Array:
err = d.decodeArray(name, input, outVal)
case reflect.Func:
err = d.decodeFunc(name, input, outVal)
default:
// If we reached this point then we weren't able to decode it
return fmt.Errorf("%s: unsupported type: %s", name, outputKind)
}
}

// If we reached here, then we successfully decoded SOMETHING, so
Expand Down
82 changes: 82 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,88 @@ func TestDecode_FuncHook(t *testing.T) {
}
}

func TestDecode_SamePointerHook(t *testing.T) {
t.Parallel()

input := map[string]interface{}{
"A": "foo",
"B": "foo",
}

ptr := stringPtr("bar")

decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
if str, ok := v.(string); ok && str == "foo" {
return ptr, nil
}
return v, nil
}

var result struct {
A *string
B *string
}
config := &DecoderConfig{
DecodeHook: decodeHook,
Result: &result,
}

decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}

err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err)
}

if result.A != result.B {
t.Errorf("decoded pointers should be the same")
}
}

func TestDecode_SameInterfaceHook(t *testing.T) {
t.Parallel()

input := map[string]interface{}{
"A": "foo",
"B": "foo",
}

var intf io.Reader = strings.NewReader("bar")

decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
if str, ok := v.(string); ok && str == "foo" {
return intf, nil
}
return v, nil
}

var result struct {
A io.Reader
B io.Reader
}
config := &DecoderConfig{
DecodeHook: decodeHook,
Result: &result,
}

decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}

err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err)
}

if result.A != result.B {
t.Errorf("decoded interfaces should be the same")
}
}

func TestDecode_NonStruct(t *testing.T) {
t.Parallel()

Expand Down