Skip to content

Commit 4009c92

Browse files
committed
fix gen api panic and add import default
1 parent 245c1a7 commit 4009c92

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

internal/genapi/gen.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func Gen(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error
1919
}
2020
var resp plugin.CodeGeneratorResponse
2121
optdata := &OptionData{}
22+
var fname string
2223
for _, f := range req.GetProtoFile() {
2324
if !strContains(req.GetFileToGenerate(), f.GetName()) {
2425
continue
@@ -35,6 +36,9 @@ func Gen(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error
3536
return nil, err
3637
}
3738
name := fmt.Sprintf("%s.api.go", strings.ReplaceAll(f.GetName(), ".proto", ""))
39+
if len(fname) <= 0 {
40+
fname = path.Dir(f.GetName())
41+
}
3842
if len(opts.out) > 0 {
3943
name = path.Join(opts.out, name)
4044
}
@@ -47,8 +51,11 @@ func Gen(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error
4751
if err != nil {
4852
return nil, err
4953
}
54+
if len(opts.out) > 0 {
55+
fname = path.Join(opts.out, fname, "option.go")
56+
}
5057
resp.File = append(resp.File, &plugin.CodeGeneratorResponse_File{
51-
Name: proto.String("option.go"),
58+
Name: proto.String(fname),
5259
Content: proto.String(bs),
5360
})
5461
return &resp, nil

internal/genapi/opts_tmpl.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package genapi
22

33
import (
44
"bytes"
5-
"html/template"
65
"log"
6+
"text/template"
77
)
88

99
var optsCode = `package {{ .GoPackage }}
1010
1111
import (
1212
"net/http"
1313
"errors"
14+
"io/ioutil"
15+
"encoding/json"
1416
)
1517
1618
type Option func(*Options)
@@ -48,7 +50,7 @@ func newOptions(opts ...Option) *Options {
4850
4951
func buildOptions(opt *Options, opts ...Option) *Options {
5052
res := newOptions(opts...)
51-
if len(res.addr) <= 0 {
53+
if len(res.addr) == 0 {
5254
res.addr = opt.addr
5355
}
5456
return res
@@ -65,7 +67,7 @@ func doResponse(resp *http.Response, a interface{}) error {
6567
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
6668
return ErrNot200
6769
}
68-
defer func(){_ = resp.Body.Close()}()
70+
defer func(){ _ = resp.Body.Close()}()
6971
bs, err := ioutil.ReadAll(resp.Body)
7072
if err != nil {
7173
return err

internal/genapi/rest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func genRestMethodCode(fd *descriptor.FileDescriptorProto, serv *descriptor.Serv
2727
}
2828

2929
data := &CodeData{}
30+
data.Verb = strings.ToUpper(rest.verb)
3031

3132
tokens := buildRoute(rest)
3233
route := rest.route
@@ -118,7 +119,7 @@ func buildRoute(rest *restInfo) []string {
118119

119120
func buildQuery(m *descriptor.MethodDescriptorProto) []string {
120121
params := buildParams(m)
121-
str := "params.Add(%s, %s)"
122+
str := `params.Add("%s", %s)`
122123
return formParams(str, params)
123124
}
124125

internal/genapi/tmpl.go

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package genapi
22

33
import (
44
"bytes"
5-
"html/template"
65
"log"
6+
"text/template"
77
)
88

99
var 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
1414
import (
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.
1824
var _ = 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
2841
type {{ unexport .ServName }}Service struct {
@@ -42,7 +55,7 @@ func New{{ .ServName }}Service(opts ...Option) {{ .ServName }}Service {
4255
4356
{{ range .Methods }}
4457
func (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

8095
var 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

90105
var 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

100115
var 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

108123
func buildFrame(data *FileData) (string, error) {

0 commit comments

Comments
 (0)