From b942b7a0745869d5392b6e331476530dbf4b212b Mon Sep 17 00:00:00 2001 From: Chao Cai <113407372+supermigo@users.noreply.github.com> Date: Sat, 25 May 2024 20:05:38 +0800 Subject: [PATCH] fix:convertor struct to map by struct ptr (#221) Co-authored-by: emrysechobygo --- convertor/convertor.go | 1 + convertor/convertor_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/convertor/convertor.go b/convertor/convertor.go index 9556a4c..4b10528 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -197,6 +197,7 @@ func StructToMap(value interface{}) (map[string]interface{}, error) { if t.Kind() == reflect.Ptr { t = t.Elem() + v = v.Elem() } if t.Kind() != reflect.Struct { return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value) diff --git a/convertor/convertor_test.go b/convertor/convertor_test.go index 4e6f1d6..c452f52 100644 --- a/convertor/convertor_test.go +++ b/convertor/convertor_test.go @@ -156,13 +156,17 @@ func TestStructToMap(t *testing.T) { Name string `json:"name"` age int } - p := People{ + p := &People{ "test", 100, } pm, _ := StructToMap(p) - var expected = map[string]interface{}{"name": "test"} + data, _ := StructToMap(p) + var expected = map[string]interface{}{ + "name": "test", + } assert.Equal(expected, pm) + assert.Equal(expected, data) } func TestColorHexToRGB(t *testing.T) {