Skip to content

[Bug] gconv not works for array/map of json.RawMessage  #3006

Closed
@kele1997

Description

@kele1997

1. What version of Go and system type/arch are you using?

go version go1.19.12 windows/amd64

2. What version of GoFrame are you using?

v2.5.4

3. Can this issue be re-produced with the latest release?

Yes

4. What did you do?

package main

import (
        "encoding/json"
        "testing"

        "github.com/gogf/gf/v2/test/gtest"
        "github.com/gogf/gf/v2/util/gconv"
)

type tFF struct {
        Val1 json.RawMessage            `json:"val1"`
        Val2 []json.RawMessage          `json:"val2"`
        Val3 map[string]json.RawMessage `json:"val3"`
}

func Test_gconv_RawMessage(t *testing.T) {
        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"}`))
        })
}

5. What did you expect to see?

all test passed

6. What did you see instead?

gconv.Struct prefer to use reflect.Type.Name() to get type name rather than reflect.Type.String().

elemTypeName = elemType.Name()
if elemTypeName == "" {
elemTypeName = elemType.String()
}

reflect.Type(json.RawMessage).Name() should be RawMessage
reflect.Type(json.RawMessage).String() should be json.RawMessage

case "Duration", "time.Duration":
return Duration(in.FromValue)
case "*time.Duration":
if _, ok := in.FromValue.(*time.Duration); ok {
return in.FromValue
}
v := Duration(in.FromValue)
return &v
case "map[string]string":
return MapStrStr(in.FromValue)
case "map[string]interface{}":
return Map(in.FromValue)
case "[]map[string]interface{}":
return Maps(in.FromValue)
case "json.RawMessage":
return Bytes(in.FromValue)

so gconv_convert.go:272 case json.RawMessage should be replaced by case RawMessage, json.RawMessage

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions