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

Commit 1ceb09e

Browse files
committed
test to verify map => struct
1 parent 0029644 commit 1ceb09e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

mapstructure_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,36 @@ func TestDecode_StructTaggedWithOmitempty_KeepNonEmptyValues(t *testing.T) {
24202420
}
24212421
}
24222422

2423+
func TestDecode_mapToStruct(t *testing.T) {
2424+
type Target struct {
2425+
String string
2426+
StringPtr *string
2427+
}
2428+
2429+
expected := Target{
2430+
String: "hello",
2431+
}
2432+
2433+
var target Target
2434+
err := Decode(map[string]interface{}{
2435+
"string": "hello",
2436+
"StringPtr": "goodbye",
2437+
}, &target)
2438+
if err != nil {
2439+
t.Fatalf("got error: %s", err)
2440+
}
2441+
2442+
// Pointers fail reflect test so do those manually
2443+
if target.StringPtr == nil || *target.StringPtr != "goodbye" {
2444+
t.Fatalf("bad: %#v", target)
2445+
}
2446+
target.StringPtr = nil
2447+
2448+
if !reflect.DeepEqual(target, expected) {
2449+
t.Fatalf("bad: %#v", target)
2450+
}
2451+
}
2452+
24232453
func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) {
24242454
var result Slice
24252455
err := Decode(input, &result)

0 commit comments

Comments
 (0)