mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-16 10:42:27 +08:00
fix: support nest struct in StructToUrlValues
This commit is contained in:
@@ -28,7 +28,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
|
||||||
"github.com/duke-git/lancet/v2/slice"
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -363,11 +362,20 @@ func validateRequest(req *HttpRequest) error {
|
|||||||
// Play: https://go.dev/play/p/pFqMkM40w9z
|
// Play: https://go.dev/play/p/pFqMkM40w9z
|
||||||
func StructToUrlValues(targetStruct any) (url.Values, error) {
|
func StructToUrlValues(targetStruct any) (url.Values, error) {
|
||||||
result := url.Values{}
|
result := url.Values{}
|
||||||
s, err := convertor.StructToMap(targetStruct)
|
|
||||||
|
var m map[string]interface{}
|
||||||
|
|
||||||
|
jsonBytes, err := json.Marshal(targetStruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for k, v := range s {
|
|
||||||
|
err = json.Unmarshal(jsonBytes, &m)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
result.Add(k, fmt.Sprintf("%v", v))
|
result.Add(k, fmt.Sprintf("%v", v))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -227,16 +227,25 @@ func TestStructToUrlValues(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
||||||
|
|
||||||
|
type CommReq struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
}
|
||||||
|
|
||||||
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"`
|
Name string `json:"name,omitempty"`
|
||||||
|
CommReq `json:",inline"`
|
||||||
}
|
}
|
||||||
item1 := TodoQuery{
|
item1 := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 123,
|
UserId: 123,
|
||||||
Name: "",
|
Name: "",
|
||||||
|
CommReq: CommReq{
|
||||||
|
Version: "1.0",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
todoValues, err := StructToUrlValues(item1)
|
todoValues, err := StructToUrlValues(item1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("params is invalid: %v", err)
|
t.Errorf("params is invalid: %v", err)
|
||||||
@@ -245,19 +254,10 @@ func TestStructToUrlValues(t *testing.T) {
|
|||||||
assert.Equal("1", todoValues.Get("id"))
|
assert.Equal("1", todoValues.Get("id"))
|
||||||
assert.Equal("123", todoValues.Get("userId"))
|
assert.Equal("123", todoValues.Get("userId"))
|
||||||
assert.Equal("", todoValues.Get("name"))
|
assert.Equal("", todoValues.Get("name"))
|
||||||
|
assert.Equal("1.0", todoValues.Get("version"))
|
||||||
item2 := TodoQuery{
|
|
||||||
Id: 2,
|
|
||||||
UserId: 456,
|
|
||||||
}
|
|
||||||
queryValues2, _ := StructToUrlValues(item2)
|
|
||||||
|
|
||||||
assert.Equal("2", queryValues2.Get("id"))
|
|
||||||
assert.Equal("456", queryValues2.Get("userId"))
|
|
||||||
assert.Equal("", queryValues2.Get("name"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFileRequest(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
func handleFileRequest(t *testing.T, _ http.ResponseWriter, r *http.Request) {
|
||||||
err := r.ParseMultipartForm(1024)
|
err := r.ParseMultipartForm(1024)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user