fix issue in gconv.Struct for json string parameter

This commit is contained in:
John
2020-11-05 22:19:34 +08:00
parent 12ed05f846
commit 176dcdc7cc
3 changed files with 91 additions and 4 deletions

View File

@ -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")
})
}

View File

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

View File

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