fix issue gconv struct slice/map of json.RawMessage (#3006) (#3008)

This commit is contained in:
kele1997
2023-10-11 21:35:27 +08:00
committed by GitHub
parent 8c309ac9fe
commit 65192a7f92
2 changed files with 27 additions and 1 deletions

View File

@ -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:

View File

@ -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) {