WeakDecode
diff --git a/mapstructure.go b/mapstructure.go index 62fe1be..381ba5d 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -104,6 +104,23 @@ return decoder.Decode(m) } +// WeakDecode is the same as Decode but is shorthand to enable +// WeaklyTypedInput. See DecoderConfig for more info. +func WeakDecode(input, output interface{}) error { + config := &DecoderConfig{ + Metadata: nil, + Result: output, + WeaklyTypedInput: true, + } + + decoder, err := NewDecoder(config) + if err != nil { + return err + } + + return decoder.Decode(input) +} + // NewDecoder returns a new decoder for the given configuration. Once // a decoder has been returned, the same configuration must not be used // again.
diff --git a/mapstructure_test.go b/mapstructure_test.go index 3147f55..23029c7 100644 --- a/mapstructure_test.go +++ b/mapstructure_test.go
@@ -775,6 +775,30 @@ } } +func TestWeakDecode(t *testing.T) { + t.Parallel() + + input := map[string]interface{}{ + "foo": "4", + "bar": "value", + } + + var result struct { + Foo int + Bar string + } + + if err := WeakDecode(input, &result); err != nil { + t.Fatalf("err: %s", err) + } + if result.Foo != 4 { + t.Fatalf("bad: %#v", result) + } + if result.Bar != "value" { + t.Fatalf("bad: %#v", result) + } +} + func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) { var result Slice err := Decode(input, &result)