@@ -27,8 +27,10 @@ func readLines(r io.Reader) ([]string, error) {
27
27
scanner = bufio .NewScanner (r )
28
28
)
29
29
for scanner .Scan () {
30
- // remove indents
30
+ // remove indentation
31
31
line := strings .TrimSpace (scanner .Text ())
32
+
33
+ // skip empty line
32
34
if len (line ) > 0 {
33
35
if strings .HasPrefix (line , "//" ) {
34
36
lines = append (lines , line )
@@ -38,12 +40,15 @@ func readLines(r io.Reader) ([]string, error) {
38
40
// remove semi-colon line-separator
39
41
sl := strings .Split (line , ";" )
40
42
for _ , txt := range sl {
41
- if len (txt ) > 0 {
42
- if strings .HasPrefix (txt , "optional " ) { // proto2
43
- txt = strings .Replace (txt , "optional " , "" , 1 )
44
- }
45
- lines = append (lines , txt )
43
+ txt = strings .TrimSpace (txt )
44
+ if len (txt ) == 0 {
45
+ continue
46
46
}
47
+
48
+ if strings .HasPrefix (txt , "optional " ) { // proto2
49
+ txt = strings .Replace (txt , "optional " , "" , 1 )
50
+ }
51
+ lines = append (lines , txt )
47
52
}
48
53
}
49
54
}
@@ -122,23 +127,49 @@ func ReadFile(fpath string) (*Proto, error) {
122
127
)
123
128
124
129
skippingEnum := false
130
+ skippingGatewayCnt := 0
125
131
for _ , line := range lines {
126
132
if strings .HasPrefix (line , "//" ) {
127
133
ls := strings .Replace (line , "//" , "" , 1 )
128
134
comments = append (comments , strings .TrimSpace (ls ))
129
135
continue
130
136
}
137
+
138
+ // skippin enum
131
139
if strings .HasPrefix (line , "enum " ) {
132
140
skippingEnum = true
133
141
continue
134
142
}
143
+ if skippingEnum && strings .HasSuffix (line , "}" ) { // end of enum
144
+ skippingEnum = false
145
+ continue
146
+ }
135
147
if skippingEnum {
136
- if strings .HasSuffix (line , "}" ) { // end of enum
137
- skippingEnum = false
138
- }
139
148
continue
140
149
}
141
150
151
+ // skip gengo/grpc-gateway
152
+ if strings .HasPrefix (line , "option (google.api.http) = " ) {
153
+ skippingGatewayCnt ++
154
+ continue
155
+ }
156
+ if strings .HasPrefix (line , "post: " ) {
157
+ skippingGatewayCnt ++
158
+ continue
159
+ }
160
+ if strings .HasPrefix (line , "body: " ) {
161
+ skippingGatewayCnt ++
162
+ continue
163
+ }
164
+ if skippingGatewayCnt == 3 && line == "}" {
165
+ skippingGatewayCnt ++
166
+ continue
167
+ }
168
+ if skippingGatewayCnt == 4 {
169
+ skippingGatewayCnt = 0
170
+ continue // end of grpc-gateway
171
+ }
172
+
142
173
switch mode {
143
174
case reading :
144
175
for j , elem := range strings .Fields (line ) {
@@ -205,7 +236,8 @@ func ReadFile(fpath string) (*Proto, error) {
205
236
if strings .HasPrefix (line , "rpc " ) {
206
237
lt := strings .Replace (line , "rpc " , "" , 1 )
207
238
lt = strings .Replace (lt , ")" , "" , - 1 )
208
- lt = strings .Replace (lt , " {}" , "" , 1 )
239
+ lt = strings .Replace (lt , " {}" , "" , 1 ) // without grpc gateway
240
+ lt = strings .Replace (lt , " {" , "" , 1 ) // with grpc gateway
209
241
fsigs := strings .Split (lt , " returns " )
210
242
211
243
ft := strings .Split (fsigs [0 ], "(" ) // split 'Watch(stream WatchRequest'
@@ -224,7 +256,8 @@ func ReadFile(fpath string) (*Proto, error) {
224
256
protoMethod .Description = strings .Join (comments , " " )
225
257
protoService .Methods = append (protoService .Methods , protoMethod )
226
258
comments = []string {}
227
- } else if ! strings .HasSuffix (line , "{}" ) && strings .HasSuffix (line , "}" ) { // closing of service
259
+ } else if ! strings .HasSuffix (line , "{}" ) && strings .HasSuffix (line , "}" ) {
260
+ // end of service
228
261
protoService .FilePath = fs
229
262
rp .Services = append (rp .Services , protoService )
230
263
protoService = ProtoService {}
0 commit comments