Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion util/gconv/gconv_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions util/gconv/gconv_z_unit_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gconv_test

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down