Skip to content

Commit 320c8c9

Browse files
committed
test: cover unmarshaler to map
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
1 parent 5b22829 commit 320c8c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mapstructure_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,3 +4648,28 @@ func TestUnmarshaler_MapType(t *testing.T) {
46484648
t.Errorf("expected key:bob=100, got %d", result.Scores["key:bob"])
46494649
}
46504650
}
4651+
4652+
func TestUnmarshaler_StructToMap(t *testing.T) {
4653+
t.Parallel()
4654+
4655+
// Input is a struct that implements Unmarshaler
4656+
// Output is a map - Unmarshaler should NOT be involved
4657+
input := PersonWithUnmarshaler{
4658+
Name: "Alice",
4659+
Age: 30,
4660+
}
4661+
4662+
var result map[string]any
4663+
err := Decode(input, &result)
4664+
if err != nil {
4665+
t.Fatalf("unexpected error: %s", err)
4666+
}
4667+
4668+
// Should use normal struct-to-map decoding
4669+
if result["Name"] != "Alice" {
4670+
t.Errorf("expected Name 'Alice', got %v", result["Name"])
4671+
}
4672+
if result["Age"] != 30 {
4673+
t.Errorf("expected Age 30, got %v", result["Age"])
4674+
}
4675+
}

0 commit comments

Comments
 (0)