mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix: fix StructToUrlValues failed when tag contain omitempty
This commit is contained in:
@@ -590,21 +590,29 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
fmt.Println(todoValues.Get("id"))
|
||||||
|
fmt.Println(todoValues.Get("userId"))
|
||||||
fmt.Println(todoValues.Get("name"))
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -592,21 +592,29 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
fmt.Println(todoValues.Get("id"))
|
||||||
|
fmt.Println(todoValues.Get("userId"))
|
||||||
fmt.Println(todoValues.Get("name"))
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -297,7 +297,11 @@ func StructToUrlValues(targetStruct any) url.Values {
|
|||||||
for i := 0; i < fieldNum; i++ {
|
for i := 0; i < fieldNum; i++ {
|
||||||
name := rt.Field(i).Name
|
name := rt.Field(i).Name
|
||||||
tag := rt.Field(i).Tag.Get("json")
|
tag := rt.Field(i).Tag.Get("json")
|
||||||
|
// if regex.MatchString(name) && tag != "" && !strings.Contains(tag, "omitempty"){
|
||||||
if regex.MatchString(name) && tag != "" {
|
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()))
|
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -217,30 +217,31 @@ func TestStructToUrlValues(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
||||||
|
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item1 := TodoQuery{
|
||||||
Id: 1,
|
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", queryValues1.Get("id"))
|
||||||
assert.Equal("1", todoValues.Get("userId"))
|
assert.Equal("123", queryValues1.Get("userId"))
|
||||||
|
assert.Equal("test", queryValues1.Get("name"))
|
||||||
|
assert.Equal("", queryValues1.Get("status"))
|
||||||
|
|
||||||
request := &HttpRequest{
|
item2 := TodoQuery{
|
||||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
Id: 2,
|
||||||
Method: "GET",
|
UserId: 456,
|
||||||
QueryParams: todoValues,
|
|
||||||
}
|
}
|
||||||
|
queryValues2 := StructToUrlValues(item2)
|
||||||
|
|
||||||
httpClient := NewHttpClient()
|
assert.Equal("2", queryValues2.Get("id"))
|
||||||
resp, err := httpClient.SendRequest(request)
|
assert.Equal("456", queryValues2.Get("userId"))
|
||||||
if err != nil || resp.StatusCode != 200 {
|
assert.Equal("", queryValues2.Get("name"))
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := io.ReadAll(resp.Body)
|
|
||||||
t.Log("response: ", string(body))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,21 +123,42 @@ func ExampleHttpClient_DecodeResponse() {
|
|||||||
|
|
||||||
func ExampleStructToUrlValues() {
|
func ExampleStructToUrlValues() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item1 := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Name: "Test",
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := StructToUrlValues(todoQuery)
|
queryValues1 := StructToUrlValues(item1)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id"))
|
item2 := TodoQuery{
|
||||||
fmt.Println(todoValues.Get("name"))
|
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:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// Test
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
|
// 2
|
||||||
|
// 456
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleConvertMapToQueryString() {
|
func ExampleConvertMapToQueryString() {
|
||||||
|
|||||||
Reference in New Issue
Block a user