From 176dcdc7ccba1aff08ffa62d3f9dfb098804cc53 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 5 Nov 2020 22:19:34 +0800 Subject: [PATCH] fix issue in gconv.Struct for json string parameter --- encoding/gjson/gjson_z_unit_struct_test.go | 63 ++++++++++++++++++++++ util/gconv/gconv_struct.go | 16 +++++- util/gconv/gconv_structs.go | 16 +++++- 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/encoding/gjson/gjson_z_unit_struct_test.go b/encoding/gjson/gjson_z_unit_struct_test.go index e55bea233..adff8b4db 100644 --- a/encoding/gjson/gjson_z_unit_struct_test.go +++ b/encoding/gjson/gjson_z_unit_struct_test.go @@ -238,3 +238,66 @@ func Test_ToStructDeep(t *testing.T) { t.Assert(m.Items, nil) }) } + +func Test_ToStruct_Complicated(t *testing.T) { + type CertInfo struct { + UserRealName string `json:"userRealname,omitempty"` + IdentType string `json:"identType,omitempty"` + IdentNo string `json:"identNo,omitempty"` + CompanyName string `json:"companyName,omitempty"` + Website string `json:"website,omitempty"` + RegisterNo string `json:"registerNo,omitempty"` + AreaCode string `json:"areaCode,omitempty"` + Address string `json:"address,omitempty"` + CommunityCreditCode string `json:"communityCreditCode,omitempty"` + PhoneNumber string `json:"phoneNumber,omitempty"` + AreaName string `json:"areaName,omitempty"` + PhoneAreaCode string `json:"phoneAreaCode,omitempty"` + OperateRange string `json:"operateRange,omitempty"` + Email string `json:"email,omitempty"` + LegalPersonName string `json:"legalPersonName,omitempty"` + OrgCode string `json:"orgCode,omitempty"` + BusinessLicense string `json:"businessLicense,omitempty"` + FilePath1 string `json:"filePath1,omitempty"` + MobileNo string `json:"mobileNo,omitempty"` + CardName string `json:"cardName,omitempty"` + BankMobileNo string `json:"bankMobileNo,omitempty"` + BankCode string `json:"bankCode,omitempty"` + BankCard string `json:"bankCard,omitempty"` + } + + type CertList struct { + StatusCode uint `json:"statusCode,string"` + SrcType uint `json:"srcType,string"` + CertID string `json:"certId"` + CardType string `json:"cardType,omitempty"` + CertInfo CertInfo `json:"certInfo"` + } + + type Response struct { + UserLevel uint `json:"userLevel,string,omitempty"` + CertList []CertList `json:"certList"` + } + + gtest.C(t, func(t *gtest.T) { + jsonContent := `{ +"certList":[ +{"certId":"2023313","certInfo":"{\"address\":\"xxxxxxx\",\"phoneNumber\":\"15084890\",\"companyName\":\"dddd\",\"communityCreditCode\":\"91110111MBE1G2B\",\"operateRange\":\"fff\",\"registerNo\":\"91110111MA00G2B\",\"legalPersonName\":\"rrr\"}","srcType":"1","statusCode":"2"}, +{"certId":"2023314","certInfo":"{\"identNo\":\"342224196507051\",\"userRealname\":\"xxxx\",\"identType\":\"01\"}","srcType":"8","statusCode":"0"}, +{"certId":"2023322","certInfo":"{\"businessLicense\":\"91110111MA00BE1G\",\"companyName\":\"sssss\",\"communityCreditCode\":\"91110111MA00BE1\"}","srcType":"2","statusCode":"0"} +] +}` + j, err := gjson.LoadContent(jsonContent) + t.Assert(err, nil) + var response = new(Response) + err = j.ToStruct(response) + t.Assert(err, nil) + t.Assert(len(response.CertList), 3) + t.Assert(response.CertList[0].CertID, 2023313) + t.Assert(response.CertList[1].CertID, 2023314) + t.Assert(response.CertList[2].CertID, 2023322) + t.Assert(response.CertList[0].CertInfo.PhoneNumber, "15084890") + t.Assert(response.CertList[1].CertInfo.IdentNo, "342224196507051") + t.Assert(response.CertList[2].CertInfo.BusinessLicense, "91110111MA00BE1G") + }) +} diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index d06e608c8..8fbc65875 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -68,11 +68,23 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping . switch r := params.(type) { case []byte: if json.Valid(r) { - return json.Unmarshal(r, pointer) + if rv, ok := pointer.(reflect.Value); ok { + if rv.Kind() == reflect.Ptr { + return json.Unmarshal(r, rv.Interface()) + } + } else { + return json.Unmarshal(r, pointer) + } } case string: if paramsBytes := []byte(r); json.Valid(paramsBytes) { - return json.Unmarshal(paramsBytes, pointer) + if rv, ok := pointer.(reflect.Value); ok { + if rv.Kind() == reflect.Ptr { + return json.Unmarshal(paramsBytes, rv.Interface()) + } + } else { + return json.Unmarshal(paramsBytes, pointer) + } } } diff --git a/util/gconv/gconv_structs.go b/util/gconv/gconv_structs.go index 9c8c169de..771e5d7ca 100644 --- a/util/gconv/gconv_structs.go +++ b/util/gconv/gconv_structs.go @@ -47,11 +47,23 @@ func doStructs(params interface{}, pointer interface{}, deep bool, mapping ...ma switch r := params.(type) { case []byte: if json.Valid(r) { - return json.Unmarshal(r, pointer) + if rv, ok := pointer.(reflect.Value); ok { + if rv.Kind() == reflect.Ptr { + return json.Unmarshal(r, rv.Interface()) + } + } else { + return json.Unmarshal(r, pointer) + } } case string: if paramsBytes := []byte(r); json.Valid(paramsBytes) { - return json.Unmarshal(paramsBytes, pointer) + if rv, ok := pointer.(reflect.Value); ok { + if rv.Kind() == reflect.Ptr { + return json.Unmarshal(paramsBytes, rv.Interface()) + } + } else { + return json.Unmarshal(paramsBytes, pointer) + } } } // Pointer type check.