Skip to content

Commit

Permalink
update multipart format generate
Browse files Browse the repository at this point in the history
  • Loading branch information
xnzone committed Jul 13, 2023
1 parent 3dbd82b commit a71d973
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions internal/genapi/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func parseRestMethod(fd *descriptor.FileDescriptorProto, serv *descriptor.Servic
ReqTyp: typeName(meth.GetInputType()),
ResTyp: typeName(meth.GetOutputType()),
}
data.Comment = strings.ReplaceAll(data.Comment, "\n", "\n\t//")
switch {
case meth.GetClientStreaming():
data.ReqCode = fmt.Sprintf(noClientStream, meth.GetName())
Expand Down
11 changes: 7 additions & 4 deletions internal/genapi/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func buildBody(m *descriptor.MethodDescriptorProto, rest *restInfo) string {
var bc string
switch typ {
case BODY_FORM:
forms := buildBodyForm(m, rest)
forms := buildBodyForm(m, rest, false)
if len(forms) <= 0 {
break
}
bc, _ = buildBodyFormCode(strings.Join(forms, "\n\t"))
case BODY_MULTI:
forms := buildBodyForm(m, rest)
forms := buildBodyForm(m, rest, true)
if len(forms) <= 0 {
break
}
Expand All @@ -83,7 +83,7 @@ func buildBody(m *descriptor.MethodDescriptorProto, rest *restInfo) string {
return code.String()
}

func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo) []string {
func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo, multi bool) []string {
queryParams := map[string]*descriptor.FieldDescriptorProto{}
request := descInfo.Type[m.GetInputType()].(*descriptor.DescriptorProto)
if rest.body != "*" {
Expand All @@ -101,7 +101,10 @@ func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo) []string
queryParams[path] = leaf
}
}
fmtKey := "bodyForms[%q] = %s"
fmtKey := "bodyForms.Add(%q, %s)"
if multi {
fmtKey = `bodyForms.WriteField(%q, %s)`
}
var parents []string

if rest.body != "*" && rest.body != "" {
Expand Down
36 changes: 18 additions & 18 deletions internal/genapi/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
bytes "bytes"
http "net/http"
strings "strings"
url "net/url"
multipart "mime/multipart"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = context.Background
Expand All @@ -28,6 +30,8 @@ var _ = bytes.Compare
var _ = json.Marshal
var _ = strings.Compare
var _ = fmt.Errorf
var _ = url.Parse
var _ = multipart.ErrMessageTooLarge
{{ range .Services }}
// Client API for {{ .ServName }} service
Expand Down Expand Up @@ -68,11 +72,14 @@ var requestCode = `// options
opt := buildOptions(c.opts, opts...)
headers := make(map[string]string)
// route
{{ .RouteCode | html }}
{{ .RouteCode }}
// body
var body io.Reader
{{ .BodyCode | html }}
{{ .BodyCode }}
{{- if eq .BodyCode "" -}}
req, err := http.NewRequest("{{ .Verb }}", rawURL, nil)
{{- else }}
req, err := http.NewRequest("{{ .Verb }}", rawURL, body)
{{- end }}
if err != nil {
return nil, err
}
Expand All @@ -93,31 +100,24 @@ var requestCode = `// options
return &res, err
`

var bodyFormCode = `bodyForms := make(map[string]string)
{{ .Body | html }}
var bs []string
for k, v := range bodyForms {
bs = append(bs, fmt.Sprintf("%s=%s", k, v))
}
body = strings.NewReader(strings.Join(bs, "&"))
var bodyFormCode = `bodyForms := url.Values{}
{{ .Body }}
body := strings.NewReader(bodyForms.Encode())
headers["Content-Type"] = "application/x-www-form-urlencoded"
`

var bodyMultiCode = `bodyForms := make(map[string]string)
{{ .Body | html }}
var bs []string
for k, v := range bodyForms {
bs = append(bs, fmt.Sprintf("%s=%s", k, v))
}
body = strings.NewReader(strings.Join(bs, "&"))
var bodyMultiCode = `body := new(bytes.Buffer)
bodyForms := multipart.NewWriter(body)
{{ .Body }}
defer func() { _ = bodyForms.Close() } ()
headers["Content-Type"] = "multipart/form-data"
`

var bodyJsonCode = `bs, err := json.Marshal({{ .Body | html }})
if err != nil {
return nil, err
}
body = bytes.NewReader(bs)
body := bytes.NewReader(bs)
headers["Content-Type"] = "application/json"
`

Expand Down
2 changes: 1 addition & 1 deletion internal/genapi/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package genapi

const Version = "v1.0.2"
const Version = "v1.0.3"

0 comments on commit a71d973

Please sign in to comment.