From 6a9eb645bb2051b479e5caa5ab6aacf549adf32e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 6 Mar 2023 17:07:57 +0800 Subject: [PATCH] fix: fix StructToUrlValues failed when tag contain omitempty --- docs/netutil.md | 22 ++++++++++++++------- docs/netutil_zh-CN.md | 22 ++++++++++++++------- netutil/http.go | 4 ++++ netutil/http_test.go | 39 +++++++++++++++++++------------------ netutil/net_example_test.go | 39 ++++++++++++++++++++++++++++--------- 5 files changed, 84 insertions(+), 42 deletions(-) diff --git a/docs/netutil.md b/docs/netutil.md index 18c1a2f..eb82cc9 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -590,21 +590,29 @@ import ( func main() { type TodoQuery struct { - Id int `json:"id"` - Name string `json:"name"` + Id int `json:"id"` + UserId int `json:"userId"` + Name string `json:"name,omitempty"` + Status string } - todoQuery := TodoQuery{ - Id: 1, - Name: "Test", + item := TodoQuery{ + Id: 1, + UserId: 123, + Name: "test", + Status: "completed", } - todoValues := netutil.StructToUrlValues(todoQuery) + queryValues := netutil.StructToUrlValues(item) fmt.Println(todoValues.Get("id")) + fmt.Println(todoValues.Get("userId")) fmt.Println(todoValues.Get("name")) + fmt.Println(todoValues.Get("status")) // Output: // 1 - // Test + // 123 + // test + // } ``` diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index 88e0b13..c9a007f 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -592,21 +592,29 @@ import ( func main() { type TodoQuery struct { - Id int `json:"id"` - Name string `json:"name"` + Id int `json:"id"` + UserId int `json:"userId"` + Name string `json:"name,omitempty"` + Status string } - todoQuery := TodoQuery{ - Id: 1, - Name: "Test", + item := TodoQuery{ + Id: 1, + UserId: 123, + Name: "test", + Status: "completed", } - todoValues := netutil.StructToUrlValues(todoQuery) + queryValues := netutil.StructToUrlValues(item) fmt.Println(todoValues.Get("id")) + fmt.Println(todoValues.Get("userId")) fmt.Println(todoValues.Get("name")) + fmt.Println(todoValues.Get("status")) // Output: // 1 - // Test + // 123 + // test + // } ``` diff --git a/netutil/http.go b/netutil/http.go index 6276380..8cf4bb7 100644 --- a/netutil/http.go +++ b/netutil/http.go @@ -297,7 +297,11 @@ func StructToUrlValues(targetStruct any) url.Values { for i := 0; i < fieldNum; i++ { name := rt.Field(i).Name tag := rt.Field(i).Tag.Get("json") + // if regex.MatchString(name) && tag != "" && !strings.Contains(tag, "omitempty"){ if regex.MatchString(name) && tag != "" { + if strings.Contains(tag, "omitempty") { + tag = strings.Split(tag, ",")[0] + } result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface())) } } diff --git a/netutil/http_test.go b/netutil/http_test.go index d32915c..791936d 100644 --- a/netutil/http_test.go +++ b/netutil/http_test.go @@ -217,30 +217,31 @@ func TestStructToUrlValues(t *testing.T) { assert := internal.NewAssert(t, "TestStructToUrlValues") type TodoQuery struct { - Id int `json:"id"` - UserId int `json:"userId"` + Id int `json:"id"` + UserId int `json:"userId"` + Name string `json:"name,omitempty"` + Status string } - todoQuery := TodoQuery{ + item1 := TodoQuery{ Id: 1, - UserId: 1, + UserId: 123, + Name: "test", + Status: "completed", } - todoValues := StructToUrlValues(todoQuery) + queryValues1 := StructToUrlValues(item1) - assert.Equal("1", todoValues.Get("id")) - assert.Equal("1", todoValues.Get("userId")) + assert.Equal("1", queryValues1.Get("id")) + assert.Equal("123", queryValues1.Get("userId")) + assert.Equal("test", queryValues1.Get("name")) + assert.Equal("", queryValues1.Get("status")) - request := &HttpRequest{ - RawURL: "https://jsonplaceholder.typicode.com/todos", - Method: "GET", - QueryParams: todoValues, + item2 := TodoQuery{ + Id: 2, + UserId: 456, } + queryValues2 := StructToUrlValues(item2) - httpClient := NewHttpClient() - resp, err := httpClient.SendRequest(request) - if err != nil || resp.StatusCode != 200 { - log.Fatal(err) - } - - body, _ := io.ReadAll(resp.Body) - t.Log("response: ", string(body)) + assert.Equal("2", queryValues2.Get("id")) + assert.Equal("456", queryValues2.Get("userId")) + assert.Equal("", queryValues2.Get("name")) } diff --git a/netutil/net_example_test.go b/netutil/net_example_test.go index c9d7ee6..a0f6e99 100644 --- a/netutil/net_example_test.go +++ b/netutil/net_example_test.go @@ -123,21 +123,42 @@ func ExampleHttpClient_DecodeResponse() { func ExampleStructToUrlValues() { type TodoQuery struct { - Id int `json:"id"` - Name string `json:"name"` + Id int `json:"id"` + UserId int `json:"userId"` + Name string `json:"name,omitempty"` + Status string } - todoQuery := TodoQuery{ - Id: 1, - Name: "Test", + item1 := TodoQuery{ + Id: 1, + UserId: 123, + Name: "test", + Status: "completed", } - todoValues := StructToUrlValues(todoQuery) + queryValues1 := StructToUrlValues(item1) - fmt.Println(todoValues.Get("id")) - fmt.Println(todoValues.Get("name")) + item2 := TodoQuery{ + Id: 2, + UserId: 456, + } + queryValues2 := StructToUrlValues(item2) + + fmt.Println(queryValues1.Get("id")) + fmt.Println(queryValues1.Get("userId")) + fmt.Println(queryValues1.Get("name")) + fmt.Println(queryValues1.Get("status")) + + fmt.Println(queryValues2.Get("id")) + fmt.Println(queryValues2.Get("userId")) + fmt.Println(queryValues2.Get("name")) // Output: // 1 - // Test + // 123 + // test + // + // 2 + // 456 + // } func ExampleConvertMapToQueryString() {