1
1
package main
2
2
3
-
4
3
import (
5
4
"fmt"
6
5
"strconv"
@@ -34,7 +33,31 @@ func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
34
33
if service .Desc .Options ().(* descriptorpb.ServiceOptions ).GetDeprecated () {
35
34
continue
36
35
}
37
- genService (file , service , g )
36
+ count := 0
37
+ for _ , method := range service .Methods {
38
+ mop := method .Desc .Options ().(* descriptorpb.MethodOptions )
39
+ if mop .GetDeprecated () {
40
+ continue
41
+ }
42
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
43
+ continue
44
+ }
45
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
46
+ need := false
47
+ for _ , em := range emethod {
48
+ if strings .ToUpper (em ) == "GRPC" {
49
+ need = true
50
+ break
51
+ }
52
+ }
53
+ if ! need {
54
+ continue
55
+ }
56
+ count ++
57
+ }
58
+ if count > 0 {
59
+ genService (file , service , g )
60
+ }
38
61
}
39
62
return g
40
63
}
@@ -63,7 +86,22 @@ func genService(file *protogen.File, s *protogen.Service, g *protogen.GeneratedF
63
86
64
87
func genPath (file * protogen.File , service * protogen.Service , g * protogen.GeneratedFile ) {
65
88
for _ , method := range service .Methods {
66
- if method .Desc .Options ().(* descriptorpb.MethodOptions ).GetDeprecated () {
89
+ mop := method .Desc .Options ().(* descriptorpb.MethodOptions )
90
+ if mop .GetDeprecated () {
91
+ continue
92
+ }
93
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
94
+ continue
95
+ }
96
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
97
+ need := false
98
+ for _ , em := range emethod {
99
+ if strings .ToUpper (em ) == "GRPC" {
100
+ need = true
101
+ break
102
+ }
103
+ }
104
+ if ! need {
67
105
continue
68
106
}
69
107
pathname := "_CGrpcPath" + service .GoName + method .GoName
@@ -78,7 +116,22 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
78
116
79
117
g .P ("type " , serverName , " interface {" )
80
118
for _ , method := range service .Methods {
81
- if method .Desc .Options ().(* descriptorpb.MethodOptions ).GetDeprecated () {
119
+ mop := method .Desc .Options ().(* descriptorpb.MethodOptions )
120
+ if mop .GetDeprecated () {
121
+ continue
122
+ }
123
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
124
+ continue
125
+ }
126
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
127
+ need := false
128
+ for _ , em := range emethod {
129
+ if strings .ToUpper (em ) == "GRPC" {
130
+ need = true
131
+ break
132
+ }
133
+ }
134
+ if ! need {
82
135
continue
83
136
}
84
137
g .P (method .Comments .Leading ,
@@ -89,10 +142,25 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
89
142
g .P ()
90
143
// Server handler
91
144
for _ , method := range service .Methods {
92
- pathurl := "/" + * file . Proto . Package + "." + string ( service . Desc .Name ()) + "/" + string ( method . Desc . Name () )
93
- if method . Desc . Options ().( * descriptorpb. MethodOptions ) .GetDeprecated () {
145
+ mop := method . Desc .Options ().( * descriptorpb. MethodOptions )
146
+ if mop .GetDeprecated () {
94
147
continue
95
148
}
149
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
150
+ continue
151
+ }
152
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
153
+ need := false
154
+ for _ , em := range emethod {
155
+ if strings .ToUpper (em ) == "GRPC" {
156
+ need = true
157
+ break
158
+ }
159
+ }
160
+ if ! need {
161
+ continue
162
+ }
163
+ pathurl := "/" + * file .Proto .Package + "." + string (service .Desc .Name ()) + "/" + string (method .Desc .Name ())
96
164
fname := "func _" + service .GoName + "_" + method .GoName + "_" + "CGrpcHandler"
97
165
p1 := "handler func (" + g .QualifiedGoIdent (contextPackage .Ident ("Context" )) + ",*" + g .QualifiedGoIdent (method .Input .GoIdent ) + ")(*" + g .QualifiedGoIdent (method .Output .GoIdent ) + ",error)"
98
166
freturn := g .QualifiedGoIdent (cgrpcPackage .Ident ("OutsideHandler" ))
@@ -135,6 +203,20 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
135
203
if mop .GetDeprecated () {
136
204
continue
137
205
}
206
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
207
+ continue
208
+ }
209
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
210
+ need := false
211
+ for _ , em := range emethod {
212
+ if strings .ToUpper (em ) == "GRPC" {
213
+ need = true
214
+ break
215
+ }
216
+ }
217
+ if ! need {
218
+ continue
219
+ }
138
220
var mids []string
139
221
if proto .HasExtension (mop , pbex .E_CgrpcMidwares ) {
140
222
mids = proto .GetExtension (mop , pbex .E_CgrpcMidwares ).([]string )
@@ -173,7 +255,22 @@ func genClient(file *protogen.File, service *protogen.Service, g *protogen.Gener
173
255
174
256
g .P ("type " , clientName , " interface {" )
175
257
for _ , method := range service .Methods {
176
- if method .Desc .Options ().(* descriptorpb.MethodOptions ).GetDeprecated () {
258
+ mop := method .Desc .Options ().(* descriptorpb.MethodOptions )
259
+ if mop .GetDeprecated () {
260
+ continue
261
+ }
262
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
263
+ continue
264
+ }
265
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
266
+ need := false
267
+ for _ , em := range emethod {
268
+ if strings .ToUpper (em ) == "GRPC" {
269
+ need = true
270
+ break
271
+ }
272
+ }
273
+ if ! need {
177
274
continue
178
275
}
179
276
g .P (method .Comments .Leading ,
@@ -195,6 +292,20 @@ func genClient(file *protogen.File, service *protogen.Service, g *protogen.Gener
195
292
if mop .GetDeprecated () {
196
293
continue
197
294
}
295
+ if ! proto .HasExtension (mop , pbex .E_Method ) {
296
+ continue
297
+ }
298
+ emethod := proto .GetExtension (mop , pbex .E_Method ).([]string )
299
+ need := false
300
+ for _ , em := range emethod {
301
+ if strings .ToUpper (em ) == "GRPC" {
302
+ need = true
303
+ break
304
+ }
305
+ }
306
+ if ! need {
307
+ continue
308
+ }
198
309
pathname := "_CGrpcPath" + service .GoName + method .GoName
199
310
p1 := "ctx " + g .QualifiedGoIdent (contextPackage .Ident ("Context" ))
200
311
p2 := "req *" + g .QualifiedGoIdent (method .Input .GoIdent )
0 commit comments