Skip to content

Commit

Permalink
fix the error caused by omitempty
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao committed Jun 22, 2020
1 parent 4c13582 commit 852924e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/alibabacloud-go/tea

go 1.14

require (
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
Expand Down
10 changes: 9 additions & 1 deletion tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
return out
}
if dataValue.Kind().String() == "ptr" {
if dataValue.IsNil() {
return out
}
dataValue = dataValue.Elem()
}
if !dataValue.IsValid() {
Expand All @@ -664,9 +667,12 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
name, containsNameTag := field.Tag.Lookup("json")
if !containsNameTag {
name = field.Name
} else {
strs := strings.Split(name, ",")
name = strs[0]
}
fieldValue := dataValue.FieldByName(field.Name)
if !fieldValue.IsValid() {
if !fieldValue.IsValid() || fieldValue.IsNil() {
continue
}
if field.Type.Kind().String() == "struct" {
Expand Down Expand Up @@ -870,6 +876,8 @@ func validatePtr(elementValue reflect.Value, containsregexpTag bool, tag, tagNam

func checkRequire(field reflect.StructField, valueField reflect.Value) error {
name, _ := field.Tag.Lookup("json")
strs := strings.Split(name, ",")
name = strs[0]
if !valueField.IsNil() && valueField.IsValid() {
return nil
}
Expand Down
40 changes: 20 additions & 20 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

type test struct {
Key string `json:"key"`
Body []byte `json:"body"`
Key string `json:"key,omitempty"`
Body []byte `json:"body,omitempty"`
}

type PrettifyTest struct {
Expand All @@ -46,16 +46,16 @@ var runtimeObj = map[string]interface{}{
}

type validateTest struct {
Num1 *int `json:"num1" require:"true" minimum:"2"`
Num2 *int `json:"num2" maximum:"6"`
Name1 *string `json:"name1" maxLength:"4"`
Name2 *string `json:"name2" minLength:"2"`
Str *string `json:"str" pattern:"^[a-d]*$" maxLength:"4"`
MaxLength *errMaxLength `json:"MaxLength"`
MinLength *errMinLength `json:"MinLength"`
Maximum *errMaximum `json:"Maximum"`
Minimum *errMinimum `json:"Minimum"`
List []*string `json:"list" pattern:"^[a-d]*$" maxLength:"4"`
Num1 *int `json:"num1,omitempty" require:"true" minimum:"2"`
Num2 *int `json:"num2,omitempty" maximum:"6"`
Name1 *string `json:"name1,omitempty" maxLength:"4"`
Name2 *string `json:"name2,omitempty" minLength:"2"`
Str *string `json:"str,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
MinLength *errMinLength `json:"MinLength,omitempty"`
Maximum *errMaximum `json:"Maximum,omitempty"`
Minimum *errMinimum `json:"Minimum,omitempty"`
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
}

type errMaxLength struct {
Expand Down Expand Up @@ -221,12 +221,12 @@ func TestMerge(t *testing.T) {
}

type Test struct {
Msg *string `json:"Msg"`
Cast *CastError `json:"Cast"`
ListPtr []*string `json:"ListPtr"`
List []string `json:"List"`
CastList []CastError `json:"CastList"`
CastListPtr []*CastError `json:"CastListPtr"`
Msg *string `json:"Msg,omitempty"`
Cast *CastError `json:"Cast,omitempty"`
ListPtr []*string `json:"ListPtr,omitempty"`
List []string `json:"List,omitempty"`
CastList []CastError `json:"CastList,omitempty"`
CastListPtr []*CastError `json:"CastListPtr,omitempty"`
}

func TestToMap(t *testing.T) {
Expand Down Expand Up @@ -366,7 +366,7 @@ func Test_DoRequest(t *testing.T) {
runtimeObj["httpsProxy"] = "# #%gfdf"
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, resp)
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
utils.AssertContains(t, err.Error(), `invalid URL escape "%gf"`)

request.Pathname = String("?log")
request.Headers["tea"] = String("")
Expand All @@ -383,7 +383,7 @@ func Test_DoRequest(t *testing.T) {
runtimeObj["socks5Proxy"] = "# #%gfdf"
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, resp)
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
utils.AssertContains(t, err.Error(), ` invalid URL escape "%gf"`)

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
Expand Down

0 comments on commit 852924e

Please sign in to comment.