-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_param.go
157 lines (129 loc) · 3.86 KB
/
api_param.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/jhump/protoreflect/desc"
"github.com/sonatard/proto-to-postman/pbdesc"
"github.com/sonatard/proto-to-postman/postman"
"golang.org/x/xerrors"
"google.golang.org/genproto/googleapis/api/annotations"
)
var marshalOpt = &jsonpb.Marshaler{
EnumsAsInts: true,
EmitDefaults: true,
Indent: "\t",
}
type apiParamsBuilder struct {
baseURL string
headers []*postman.HeaderParam
pbdesc *pbdesc.ProtoDescriptor
}
func NewAPIParamsBuilder(baseURL string, headers []*postman.HeaderParam) *apiParamsBuilder {
return &apiParamsBuilder{
baseURL: baseURL,
headers: headers,
pbdesc: &pbdesc.ProtoDescriptor{},
}
}
func (a *apiParamsBuilder) Build(fds []*desc.FileDescriptor) ([]*postman.APIParam, error) {
var apiParams []*postman.APIParam
for _, fd := range fds {
for _, service := range fd.GetServices() {
for _, method := range service.GetMethods() {
params, err := a.build(method, service)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
apiParams = append(apiParams, params...)
}
}
}
return apiParams, nil
}
func (a *apiParamsBuilder) build(method *desc.MethodDescriptor, service *desc.ServiceDescriptor) ([]*postman.APIParam, error) {
opts := method.GetOptions()
if !proto.HasExtension(opts, annotations.E_Http) {
return []*postman.APIParam{}, nil
}
ext, err := proto.GetExtension(opts, annotations.E_Http)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
rule, ok := ext.(*annotations.HttpRule)
if !ok {
return nil, xerrors.New("annotation extension assertion error")
}
apiParams, err := a.apiParamsByHTTPRule(rule, method.GetInputType())
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
return apiParams, nil
}
func (a *apiParamsBuilder) apiParamsByHTTPRule(rule *annotations.HttpRule, inputType *desc.MessageDescriptor) ([]*postman.APIParam, error) {
var apiParams []*postman.APIParam
apiParam, err := a.apiParamByHTTPRule(rule, inputType)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
apiParams = append(apiParams, apiParam)
for _, r := range rule.GetAdditionalBindings() {
apiParam, err := a.apiParamByHTTPRule(r, inputType)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
apiParams = append(apiParams, apiParam)
}
return apiParams, nil
}
func (a *apiParamsBuilder) apiParamByHTTPRule(rule *annotations.HttpRule, inputType *desc.MessageDescriptor) (*postman.APIParam, error) {
endpoint, err := newEndpoint(rule)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
bodyMsgType, err := a.pbdesc.BodyMsgTypeNameByHTTPRule(inputType, rule)
bodyNotFound := xerrors.Is(err, pbdesc.ErrBodyNotFound)
if err != nil && !bodyNotFound {
return nil, xerrors.Errorf(": %w", err)
}
var jsonBody string
if !bodyNotFound {
msg, err := a.pbdesc.NewMessage(bodyMsgType)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
b, err := msg.MarshalJSONPB(marshalOpt)
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
jsonBody = string(b)
}
return &postman.APIParam{
BaseURL: a.baseURL,
HTTPMethod: endpoint.method,
Path: endpoint.path,
Body: jsonBody,
Headers: a.headers,
}, nil
}
type endpoint struct {
method string
path string
}
func newEndpoint(rule *annotations.HttpRule) (*endpoint, error) {
var e *endpoint
switch opt := rule.GetPattern().(type) {
case *annotations.HttpRule_Get:
e = &endpoint{"GET", opt.Get}
case *annotations.HttpRule_Put:
e = &endpoint{"PUT", opt.Put}
case *annotations.HttpRule_Post:
e = &endpoint{"POST", opt.Post}
case *annotations.HttpRule_Delete:
e = &endpoint{"DELETE", opt.Delete}
case *annotations.HttpRule_Patch:
e = &endpoint{"PATCH", opt.Patch}
default:
return nil, xerrors.New("annotation http rule method dose not support type")
}
return e, nil
}