@@ -2,8 +2,8 @@ package genapi
22
33import (
44 "bytes"
5- "html/template"
65 "log"
6+ "text/template"
77)
88
99var frame = `// Code generated by protoc-gen-go_api(github.com/dev-openapi/protoc-gen-go_api version={{ .Version }}). DO NOT EDIT.
@@ -13,16 +13,29 @@ package {{ .GoPackage }}
1313
1414import (
1515 context "context"
16+ fmt "fmt"
17+ io "io"
18+ json "encoding/json"
19+ bytes "bytes"
20+ http "net/http"
21+ strings "strings"
1622)
1723// Reference imports to suppress errors if they are not otherwise used.
1824var _ = context.Background
25+ var _ = http.NewRequest
26+ var _ = io.Copy
27+ var _ = bytes.Compare
28+ var _ = json.Marshal
29+ var _ = strings.Compare
1930
2031{{ range .Services }}
2132// Client API for {{ .ServName }} service
2233
23- type {{ .ServName }}Service interace {
34+ type {{ .ServName }}Service interface {
35+ {{- range .Methods }}
2436 // {{ .MethName }} {{ .Comment }}
25- {{ .MethName }}(ctx context.Context, in *{{ .ReqTyp }}, opts ...CallOption) (*{{ .ResTyp }}, error)
37+ {{ .MethName }}(ctx context.Context, in *{{ .ReqTyp }}, opts ...Option) (*{{ .ResTyp }}, error)
38+ {{- end }}
2639}
2740
2841type {{ unexport .ServName }}Service struct {
@@ -42,7 +55,7 @@ func New{{ .ServName }}Service(opts ...Option) {{ .ServName }}Service {
4255
4356{{ range .Methods }}
4457func (c *{{ unexport .ServName }}Service) {{ .MethName }}(ctx context.Context, in *{{ .ReqTyp }}, opts ...Option) (*{{ .ResTyp }}, error) {
45- var res * {{ .ResTyp }}
58+ var res {{ .ResTyp }}
4659 {{ .ReqCode | html }}
4760}
4861{{ end -}}
@@ -58,13 +71,15 @@ var requestCode = `// options
5871 // body
5972 var body io.Reader
6073 {{ .BodyCode | html }}
61- req, err := http.NewRequest({{ .Verb }}, rawURL, body)
74+ req, err := http.NewRequest(" {{ .Verb }}" , rawURL, body)
6275 if err != nil {
6376 return nil, err
6477 }
78+ {{ if ne .QueryCode "" }}
6579 params := req.URL.Query()
6680 {{ .QueryCode | html }}
6781 req.URL.RawQuery = params.Encode()
82+ {{ end }}
6883 // header
6984 for k, v := range headers {
7085 req.Header.Set(k, v)
@@ -74,35 +89,35 @@ var requestCode = `// options
7489 return nil, err
7590 }
7691 err = opt.DoResponse(resp, &res)
77- return res, err
92+ return & res, err
7893`
7994
8095var bodyFormCode = `bodyForms := make(map[string]string)
81- {{ .Body | html }}
82- var bs []string
83- for k, v := range bodyForms {
84- bs = append(bs, fmt.Sprintf("%s=%s", k, v))
85- }
86- body = strings.NewReader(strings.Join(bs, "&"))
87- headers["Content-Type"] = "application/x-www-form-urlencoded"
96+ {{ .Body | html }}
97+ var bs []string
98+ for k, v := range bodyForms {
99+ bs = append(bs, fmt.Sprintf("%s=%s", k, v))
100+ }
101+ body = strings.NewReader(strings.Join(bs, "&"))
102+ headers["Content-Type"] = "application/x-www-form-urlencoded"
88103`
89104
90105var bodyMultiCode = `bodyForms := make(map[string]string])
91- {{ .Body | html }}
92- var bs []string
93- for k, v := range bodyForms {
94- bs = append(bs, fmt.Sprintf("%s=%s", k, v))
95- }
96- body = strings.NewReader(strings.Join(bs, "&"))
97- headers["Content-Type"] = "multipart/form-data"
106+ {{ .Body | html }}
107+ var bs []string
108+ for k, v := range bodyForms {
109+ bs = append(bs, fmt.Sprintf("%s=%s", k, v))
110+ }
111+ body = strings.NewReader(strings.Join(bs, "&"))
112+ headers["Content-Type"] = "multipart/form-data"
98113`
99114
100115var bodyJsonCode = `bs, err := json.Marshal({{ .Body | html }})
101- if err != nil {
102- return nil, err
103- }
104- body = bytes.NewReader(bs)
105- headers["Content-Type"] = "application/json"
116+ if err != nil {
117+ return nil, err
118+ }
119+ body = bytes.NewReader(bs)
120+ headers["Content-Type"] = "application/json"
106121`
107122
108123func buildFrame (data * FileData ) (string , error ) {
0 commit comments