Skip to content

Commit 90ff777

Browse files
committed
update upload byte and update gen verson
1 parent b7a0512 commit 90ff777

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

internal/genapi/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
BODY_JSON = "json"
1515
BODY_FORM = "form"
1616
BODY_MULTI = "multi"
17+
BODY_BYTE = "byte"
1718
)
1819

1920
const (

internal/genapi/rest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func buildBody(m *descriptor.MethodDescriptorProto, rest *restInfo) string {
7676
break
7777
}
7878
bc, _ = buildBodyMultiCode(strings.Join(forms, "\n\t"))
79+
case BODY_BYTE:
80+
bc, _ = buildBodyByteCode(body)
7981
default:
8082
bc, _ = buildBodyJsonCode(body)
8183
}

internal/genapi/tmpl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ var bodyJsonCode = `bs, err := json.Marshal({{ .Body | html }})
121121
headers["Content-Type"] = "application/json"
122122
`
123123

124+
var bodyByteCode = `body := bytes.NewReader({{ .Body }})
125+
headers["Content-Type"] = "application/json"
126+
`
127+
124128
func buildFrame(data *FileData) (string, error) {
125129
frm, err := template.New("frame_tmpl").Funcs(fn).Parse(frame)
126130
if err != nil {
@@ -202,3 +206,20 @@ func buildBodyJsonCode(body string) (string, error) {
202206
return bs.String(), nil
203207

204208
}
209+
210+
func buildBodyByteCode(body string) (string, error) {
211+
bbt, err := template.New("body_byte_tmpl").Funcs(fn).Parse(bodyByteCode)
212+
if err != nil {
213+
log.Println("parse byte code template err: ", err)
214+
return "", err
215+
}
216+
bs := new(bytes.Buffer)
217+
err = bbt.Execute(bs, map[string]string{
218+
"Body": body,
219+
})
220+
if err != nil {
221+
log.Println("execute byte code template err: ", err)
222+
return "", err
223+
}
224+
return bs.String(), nil
225+
}

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.3"
3+
const Version = "v1.0.4"

0 commit comments

Comments
 (0)