From 404146f97a6cf54d61d5d2793f101d442a5f54ea Mon Sep 17 00:00:00 2001 From: xjones Date: Wed, 12 Jan 2022 14:38:59 +0800 Subject: [PATCH 1/2] fix: code struct make it like golang --- core/client.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/core/client.go b/core/client.go index 3cb80de..fef5d4a 100644 --- a/core/client.go +++ b/core/client.go @@ -413,22 +413,25 @@ func CreateFormFile(w *multipart.Writer, filename, contentType string, file []by func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) { bodyBuf = &bytes.Buffer{} - if reader, ok := body.(io.Reader); ok { - _, err = bodyBuf.ReadFrom(reader) - } else if fp, ok := body.(**os.File); ok { - _, err = bodyBuf.ReadFrom(*fp) - } else if b, ok := body.([]byte); ok { + switch b := body.(type) { + case string: + _, err = bodyBuf.WriteString(b) + case *string: + _, err = bodyBuf.WriteString(*b) + case []byte: _, err = bodyBuf.Write(b) - } else if s, ok := body.(string); ok { - _, err = bodyBuf.WriteString(s) - } else if s, ok := body.(*string); ok { - _, err = bodyBuf.WriteString(*s) - } else if regJSONTypeCheck.MatchString(contentType) { - err = json.NewEncoder(bodyBuf).Encode(body) - } else if regXMLTypeCheck.MatchString(contentType) { - err = xml.NewEncoder(bodyBuf).Encode(body) + case **os.File: + _, err = bodyBuf.ReadFrom(*b) + case io.Reader: + _, err = bodyBuf.ReadFrom(b) + default: + if regJSONTypeCheck.MatchString(contentType) { + err = json.NewEncoder(bodyBuf).Encode(body) + } + if regXMLTypeCheck.MatchString(contentType) { + err = xml.NewEncoder(bodyBuf).Encode(body) + } } - if err != nil { return nil, err } From 65d11996ec8182ba41d3a96e394f99ba65b35d7d Mon Sep 17 00:00:00 2001 From: xjones Date: Wed, 12 Jan 2022 20:24:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=8A=A0=E4=B8=8Aelse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/client.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/client.go b/core/client.go index fef5d4a..db290e0 100644 --- a/core/client.go +++ b/core/client.go @@ -427,8 +427,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e default: if regJSONTypeCheck.MatchString(contentType) { err = json.NewEncoder(bodyBuf).Encode(body) - } - if regXMLTypeCheck.MatchString(contentType) { + } else if regXMLTypeCheck.MatchString(contentType) { err = xml.NewEncoder(bodyBuf).Encode(body) } }