mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
merge main
This commit is contained in:
@@ -21,12 +21,11 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
@@ -219,7 +218,7 @@ func (client *HttpClient) setTLS(rawUrl string) {
|
||||
}
|
||||
}
|
||||
|
||||
// setHeader set http rquest header
|
||||
// setHeader set http request header
|
||||
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
|
||||
if headers == nil {
|
||||
headers = make(http.Header)
|
||||
@@ -278,33 +277,15 @@ func validateRequest(req *HttpRequest) error {
|
||||
// StructToUrlValues convert struct to url valuse,
|
||||
// only convert the field which is exported and has `json` tag.
|
||||
// Play: https://go.dev/play/p/pFqMkM40w9z
|
||||
func StructToUrlValues(targetStruct any) url.Values {
|
||||
rv := reflect.ValueOf(targetStruct)
|
||||
rt := reflect.TypeOf(targetStruct)
|
||||
|
||||
if rt.Kind() == reflect.Ptr {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
if rt.Kind() != reflect.Struct {
|
||||
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
|
||||
}
|
||||
|
||||
func StructToUrlValues(targetStruct any) (url.Values, error) {
|
||||
result := url.Values{}
|
||||
|
||||
fieldNum := rt.NumField()
|
||||
pattern := `^[A-Z]`
|
||||
regex := regexp.MustCompile(pattern)
|
||||
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()))
|
||||
}
|
||||
s, err := convertor.StructToMap(targetStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range s {
|
||||
result.Add(k, fmt.Sprintf("%v", v))
|
||||
}
|
||||
|
||||
return result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -220,26 +220,26 @@ func TestStructToUrlValues(t *testing.T) {
|
||||
Id int `json:"id"`
|
||||
UserId int `json:"userId"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Status string
|
||||
}
|
||||
item1 := TodoQuery{
|
||||
Id: 1,
|
||||
UserId: 123,
|
||||
Name: "test",
|
||||
Status: "completed",
|
||||
Name: "",
|
||||
}
|
||||
todoValues, err := StructToUrlValues(item1)
|
||||
if err != nil {
|
||||
t.Errorf("params is invalid: %v", err)
|
||||
}
|
||||
queryValues1 := StructToUrlValues(item1)
|
||||
|
||||
assert.Equal("1", queryValues1.Get("id"))
|
||||
assert.Equal("123", queryValues1.Get("userId"))
|
||||
assert.Equal("test", queryValues1.Get("name"))
|
||||
assert.Equal("", queryValues1.Get("status"))
|
||||
assert.Equal("1", todoValues.Get("id"))
|
||||
assert.Equal("123", todoValues.Get("userId"))
|
||||
assert.Equal("", todoValues.Get("name"))
|
||||
|
||||
item2 := TodoQuery{
|
||||
Id: 2,
|
||||
UserId: 456,
|
||||
}
|
||||
queryValues2 := StructToUrlValues(item2)
|
||||
queryValues2, _ := StructToUrlValues(item2)
|
||||
|
||||
assert.Equal("2", queryValues2.Get("id"))
|
||||
assert.Equal("456", queryValues2.Get("userId"))
|
||||
|
||||
@@ -134,13 +134,16 @@ func ExampleStructToUrlValues() {
|
||||
Name: "test",
|
||||
Status: "completed",
|
||||
}
|
||||
queryValues1 := StructToUrlValues(item1)
|
||||
queryValues1, err := StructToUrlValues(item1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
item2 := TodoQuery{
|
||||
Id: 2,
|
||||
UserId: 456,
|
||||
}
|
||||
queryValues2 := StructToUrlValues(item2)
|
||||
queryValues2, _ := StructToUrlValues(item2)
|
||||
|
||||
fmt.Println(queryValues1.Get("id"))
|
||||
fmt.Println(queryValues1.Get("userId"))
|
||||
|
||||
Reference in New Issue
Block a user