Skip to content

Commit

Permalink
Merge pull request #96 from Jonescy/jonescy-issues95
Browse files Browse the repository at this point in the history
fix: code struct make it like golang
  • Loading branch information
EmmetZC authored Jan 13, 2022
2 parents c8ebd15 + 65d1199 commit 65e92ae
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,24 @@ 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)
} else if regXMLTypeCheck.MatchString(contentType) {
err = xml.NewEncoder(bodyBuf).Encode(body)
}
}

if err != nil {
return nil, err
}
Expand Down

0 comments on commit 65e92ae

Please sign in to comment.