/
get_method_gateway.go
130 lines (114 loc) · 3.48 KB
/
get_method_gateway.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
package gateway
import (
"path/filepath"
"strings"
"github.com/golang/glog"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/jaronnie/protoc-gen-grpc-gateway-gosdk/internal/env"
"github.com/jaronnie/protoc-gen-grpc-gateway-gosdk/internal/parse"
"github.com/jaronnie/protoc-gen-grpc-gateway-gosdk/internal/vars"
)
func GetMethodGateway(m *protogen.Method, env *env.PluginEnv) (*vars.Gateway, error) {
if m.Desc.IsStreamingClient() && m.Desc.IsStreamingServer() {
return nil, nil
}
// get grpc gateway url
options, ok := m.Desc.Options().(*descriptorpb.MethodOptions)
if !ok {
return nil, nil
}
httpRule, ok := proto.GetExtension(options, annotations.E_Http).(*annotations.HttpRule)
if !ok {
return nil, nil
}
var (
httpMethod string
url string
)
switch httpRule.GetPattern().(type) {
case *annotations.HttpRule_Get:
httpMethod = "GET"
url = httpRule.GetGet()
case *annotations.HttpRule_Put:
httpMethod = "PUT"
url = httpRule.GetPut()
case *annotations.HttpRule_Post:
httpMethod = "POST"
url = httpRule.GetPost()
case *annotations.HttpRule_Delete:
httpMethod = "DELETE"
url = httpRule.GetDelete()
case *annotations.HttpRule_Patch:
httpMethod = "PATCH"
url = httpRule.GetPatch()
default:
return nil, nil
}
pathParams, err := parse.PathParam(url)
if err != nil {
return nil, nil
}
queryParams := parse.CreateQueryParams(m)
if env.GatewayPrefix != "" {
url = env.GatewayPrefix + url
}
// protoRequestBody's fields
httpRuleBodyName := httpRule.Body
var httpRuleBodyGoName string
if httpRuleBodyName != "*" {
for _, v := range m.Input.Fields {
if httpRuleBodyName == string(v.Desc.Name()) {
httpRuleBodyGoName = v.GoName
}
}
}
glog.V(1).Infof("rpc method name: [%v], "+
"http request method: [%v], "+
"url: [%v], "+
"http request body [%s], "+
"http response body [%s], "+
"path params: [%v], "+
"query params: [%v], "+
"body: [%v]",
m.GoName, httpMethod, url, m.Input.GoIdent.GoName,
m.Output.GoIdent.GoName, pathParams, queryParams, httpRule.Body)
var isSpecified bool
for _, v := range env.SpecifiedMethods {
if v == m.GoName {
isSpecified = true
}
}
comments := strings.TrimRight(m.Comments.Leading.String(), "\n")
if comments != "" {
comments = "\n" + comments
}
return &vars.Gateway{
ProtoRequestBody: vars.ProtoRequestBody{
Name: m.Input.GoIdent.GoName,
GoImportPath: env.GoModule + "/pb" + strings.TrimLeft(string(m.Input.GoIdent.GoImportPath), "."),
RootPath: filepath.Base(env.GoModule + strings.TrimLeft(string(m.Input.GoIdent.GoImportPath), ".")),
},
HTTPRequestBody: vars.HTTPRequestBody{
BodyName: httpRuleBodyName,
GoBodyName: httpRuleBodyGoName,
},
HTTPResponseBody: vars.HTTPResponseBody{
Name: m.Output.GoIdent.GoName,
GoImportPath: env.GoModule + "/pb" + strings.TrimLeft(string(m.Output.GoIdent.GoImportPath), "."),
RootPath: filepath.Base(env.GoModule + strings.TrimLeft(string(m.Output.GoIdent.GoImportPath), ".")),
},
IsStreamClient: m.Desc.IsStreamingClient(),
IsStreamServer: m.Desc.IsStreamingServer(),
ProtoServiceName: string(m.Parent.Desc.Name()),
FuncName: m.GoName,
Comments: comments,
HTTPMethod: httpMethod,
URL: url,
PathParams: pathParams,
QueryParams: queryParams,
IsSpecified: isSpecified,
}, nil
}