fix: doMapConvertForMapOrStructValue Struct Marshaler

This commit is contained in:
hailaz
2023-11-01 17:30:52 +08:00
parent 9aa2745684
commit 10573cd694
2 changed files with 13 additions and 0 deletions

View File

@ -289,6 +289,10 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in
case reflect.Struct:
var dataMap = make(map[string]interface{})
if _, ok := reflectValue.Interface().(Marshaler); ok {
dataMap[reflectValue.Type().Name()] = reflectValue.Interface()
return dataMap
}
// Map converting interface check.
if v, ok := in.Value.(iMapStrAny); ok {
// Value copy, in case of concurrent safety.

View File

@ -632,6 +632,15 @@ func (i Issue3108String) MarshalJSON() ([]byte, error) {
}
func Test_Issue3108(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
obj := &Issue3108String{Name: "test"}
converted := gconv.MapDeep(obj)
jsonData, err := json.Marshal(converted)
t.AssertNil(err)
t.Assert(string(jsonData), `{"Issue3108String":"[test]"}`)
})
gtest.C(t, func(t *gtest.T) {
type Issue3108 struct {
MyName Issue3108String `json:"my_name"`