Browse Source

对于query参数的序列化用不同的tag

gitlink
Sydonian 11 months ago
parent
commit
b4a7963c22
3 changed files with 24 additions and 7 deletions
  1. +17
    -0
      sdks/storage/cdsapi/storage_test.go
  2. +6
    -6
      utils/http2/http.go
  3. +1
    -1
      utils/http2/http_test.go

+ 17
- 0
sdks/storage/cdsapi/storage_test.go View File

@@ -125,6 +125,23 @@ func Test_Object(t *testing.T) {
}) })
} }


func Test_ObjectList(t *testing.T) {
Convey("路径查询", t, func() {
cli := NewClient(&Config{
URL: "http://localhost:7890",
})

resp, err := cli.Object().List(ObjectList{
UserID: 1,
PackageID: 10,
Path: "100x100K/zexema",
})
So(err, ShouldBeNil)
fmt.Printf("\n")
fmt.Printf("%+v\n", resp.Objects[0])
})
}

func Test_Storage(t *testing.T) { func Test_Storage(t *testing.T) {
Convey("上传后调度文件", t, func() { Convey("上传后调度文件", t, func() {
cli := NewClient(&Config{ cli := NewClient(&Config{


+ 6
- 6
utils/http2/http.go View File

@@ -385,7 +385,7 @@ func PostMultiPart(url string, param MultiPartRequestParam) (*http.Response, err
defer muWriter.Close() defer muWriter.Close()


if param.Form != nil { if param.Form != nil {
mp, err := objectToStringMap(param.Form)
mp, err := objectToStringMap(param.Form, "json")
if err != nil { if err != nil {
return fmt.Errorf("formValues object to map failed, err: %w", err) return fmt.Errorf("formValues object to map failed, err: %w", err)
} }
@@ -477,7 +477,7 @@ func prepareQuery(req *http.Request, query any) error {
mp, ok := query.(map[string]string) mp, ok := query.(map[string]string)
if !ok { if !ok {
var err error var err error
if mp, err = objectToStringMap(query); err != nil {
if mp, err = objectToStringMap(query, "form"); err != nil {
return fmt.Errorf("query object to map: %w", err) return fmt.Errorf("query object to map: %w", err)
} }
} }
@@ -499,7 +499,7 @@ func prepareHeader(req *http.Request, header any) error {
mp, ok := header.(map[string]string) mp, ok := header.(map[string]string)
if !ok { if !ok {
var err error var err error
if mp, err = objectToStringMap(header); err != nil {
if mp, err = objectToStringMap(header, "json"); err != nil {
return fmt.Errorf("header object to map: %w", err) return fmt.Errorf("header object to map: %w", err)
} }
} }
@@ -543,7 +543,7 @@ func prepareFormBody(req *http.Request, body any) error {
mp, ok := body.(map[string]string) mp, ok := body.(map[string]string)
if !ok { if !ok {
var err error var err error
if mp, err = objectToStringMap(body); err != nil {
if mp, err = objectToStringMap(body, "json"); err != nil {
return fmt.Errorf("body object to map: %w", err) return fmt.Errorf("body object to map: %w", err)
} }
} }
@@ -577,10 +577,10 @@ func setValue(values ul.Values, key, value string) ul.Values {
return values return values
} }


func objectToStringMap(obj any) (map[string]string, error) {
func objectToStringMap(obj any, tag string) (map[string]string, error) {
anyMap := make(map[string]any) anyMap := make(map[string]any)
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
TagName: tag,
Result: &anyMap, Result: &anyMap,
WeaklyTypedInput: true, WeaklyTypedInput: true,
}) })


+ 1
- 1
utils/http2/http_test.go View File

@@ -21,7 +21,7 @@ func Test_objectToStringMap(t *testing.T) {
Omit: nil, Omit: nil,
} }


mp, err := objectToStringMap(a)
mp, err := objectToStringMap(a, "json")
So(err, ShouldBeNil) So(err, ShouldBeNil)


So(mp, ShouldResemble, map[string]string{ So(mp, ShouldResemble, map[string]string{


Loading…
Cancel
Save