Skip to content

Commit a71d973

Browse files
committed
update multipart format generate
1 parent 3dbd82b commit a71d973

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

internal/genapi/gen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func parseRestMethod(fd *descriptor.FileDescriptorProto, serv *descriptor.Servic
112112
ReqTyp: typeName(meth.GetInputType()),
113113
ResTyp: typeName(meth.GetOutputType()),
114114
}
115+
data.Comment = strings.ReplaceAll(data.Comment, "\n", "\n\t//")
115116
switch {
116117
case meth.GetClientStreaming():
117118
data.ReqCode = fmt.Sprintf(noClientStream, meth.GetName())

internal/genapi/rest.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ func buildBody(m *descriptor.MethodDescriptorProto, rest *restInfo) string {
6565
var bc string
6666
switch typ {
6767
case BODY_FORM:
68-
forms := buildBodyForm(m, rest)
68+
forms := buildBodyForm(m, rest, false)
6969
if len(forms) <= 0 {
7070
break
7171
}
7272
bc, _ = buildBodyFormCode(strings.Join(forms, "\n\t"))
7373
case BODY_MULTI:
74-
forms := buildBodyForm(m, rest)
74+
forms := buildBodyForm(m, rest, true)
7575
if len(forms) <= 0 {
7676
break
7777
}
@@ -83,7 +83,7 @@ func buildBody(m *descriptor.MethodDescriptorProto, rest *restInfo) string {
8383
return code.String()
8484
}
8585

86-
func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo) []string {
86+
func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo, multi bool) []string {
8787
queryParams := map[string]*descriptor.FieldDescriptorProto{}
8888
request := descInfo.Type[m.GetInputType()].(*descriptor.DescriptorProto)
8989
if rest.body != "*" {
@@ -101,7 +101,10 @@ func buildBodyForm(m *descriptor.MethodDescriptorProto, rest *restInfo) []string
101101
queryParams[path] = leaf
102102
}
103103
}
104-
fmtKey := "bodyForms[%q] = %s"
104+
fmtKey := "bodyForms.Add(%q, %s)"
105+
if multi {
106+
fmtKey = `bodyForms.WriteField(%q, %s)`
107+
}
105108
var parents []string
106109

107110
if rest.body != "*" && rest.body != "" {

internal/genapi/tmpl.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
bytes "bytes"
2020
http "net/http"
2121
strings "strings"
22+
url "net/url"
23+
multipart "mime/multipart"
2224
)
2325
// Reference imports to suppress errors if they are not otherwise used.
2426
var _ = context.Background
@@ -28,6 +30,8 @@ var _ = bytes.Compare
2830
var _ = json.Marshal
2931
var _ = strings.Compare
3032
var _ = fmt.Errorf
33+
var _ = url.Parse
34+
var _ = multipart.ErrMessageTooLarge
3135
3236
{{ range .Services }}
3337
// Client API for {{ .ServName }} service
@@ -68,11 +72,14 @@ var requestCode = `// options
6872
opt := buildOptions(c.opts, opts...)
6973
headers := make(map[string]string)
7074
// route
71-
{{ .RouteCode | html }}
75+
{{ .RouteCode }}
7276
// body
73-
var body io.Reader
74-
{{ .BodyCode | html }}
77+
{{ .BodyCode }}
78+
{{- if eq .BodyCode "" -}}
79+
req, err := http.NewRequest("{{ .Verb }}", rawURL, nil)
80+
{{- else }}
7581
req, err := http.NewRequest("{{ .Verb }}", rawURL, body)
82+
{{- end }}
7683
if err != nil {
7784
return nil, err
7885
}
@@ -93,31 +100,24 @@ var requestCode = `// options
93100
return &res, err
94101
`
95102

96-
var bodyFormCode = `bodyForms := make(map[string]string)
97-
{{ .Body | html }}
98-
var bs []string
99-
for k, v := range bodyForms {
100-
bs = append(bs, fmt.Sprintf("%s=%s", k, v))
101-
}
102-
body = strings.NewReader(strings.Join(bs, "&"))
103+
var bodyFormCode = `bodyForms := url.Values{}
104+
{{ .Body }}
105+
body := strings.NewReader(bodyForms.Encode())
103106
headers["Content-Type"] = "application/x-www-form-urlencoded"
104107
`
105108

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

116116
var bodyJsonCode = `bs, err := json.Marshal({{ .Body | html }})
117117
if err != nil {
118118
return nil, err
119119
}
120-
body = bytes.NewReader(bs)
120+
body := bytes.NewReader(bs)
121121
headers["Content-Type"] = "application/json"
122122
`
123123

internal/genapi/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package genapi
22

3-
const Version = "v1.0.2"
3+
const Version = "v1.0.3"

0 commit comments

Comments
 (0)