diff --git a/util/gconv/gconv_convert.go b/util/gconv/gconv_convert.go index 5bc99eccf..f13b05272 100644 --- a/util/gconv/gconv_convert.go +++ b/util/gconv/gconv_convert.go @@ -269,7 +269,7 @@ func doConvert(in doConvertInput) (convertedValue interface{}) { case "[]map[string]interface{}": return Maps(in.FromValue) - case "json.RawMessage": + case "RawMessage", "json.RawMessage": return Bytes(in.FromValue) default: diff --git a/util/gconv/gconv_z_unit_converter_test.go b/util/gconv/gconv_z_unit_converter_test.go index c839f48b9..04f1d43ef 100644 --- a/util/gconv/gconv_z_unit_converter_test.go +++ b/util/gconv/gconv_z_unit_converter_test.go @@ -7,6 +7,7 @@ package gconv_test import ( + "encoding/json" "testing" "time" @@ -51,6 +52,12 @@ func TestConverter_Struct(t *testing.T) { Val3 *time.Time `json:"val3"` } + type tFF struct { + Val1 json.RawMessage `json:"val1"` + Val2 []json.RawMessage `json:"val2"` + Val3 map[string]json.RawMessage `json:"val3"` + } + gtest.C(t, func(t *gtest.T) { a := &tA{ Val: 1, @@ -199,6 +206,25 @@ func TestConverter_Struct(t *testing.T) { t.Assert(aa.Val2.Local(), gtime.New("2023-04-15 19:10:00 +0800 CST").Local().Time) t.Assert(aa.Val3.Local(), gtime.New("2006-01-02T15:04:05Z07:00").Local().Time) }) + + // fix: https://github.com/gogf/gf/issues/3006 + gtest.C(t, func(t *gtest.T) { + ff := &tFF{} + var tmp = map[string]any{ + "val1": map[string]any{"hello": "world"}, + "val2": []any{map[string]string{"hello": "world"}}, + "val3": map[string]map[string]string{"val3": {"hello": "world"}}, + } + + err := gconv.Struct(tmp, ff) + t.AssertNil(err) + t.AssertNE(ff, nil) + t.Assert(ff.Val1, []byte(`{"hello":"world"}`)) + t.AssertEQ(len(ff.Val2), 1) + t.Assert(ff.Val2[0], []byte(`{"hello":"world"}`)) + t.AssertEQ(len(ff.Val3), 1) + t.Assert(ff.Val3["val3"], []byte(`{"hello":"world"}`)) + }) } func TestConverter_CustomBasicType_ToStruct(t *testing.T) {