@@ -126,8 +126,8 @@ func ReadFile(fpath string) (*Proto, error) {
126
126
protoService = ProtoService {}
127
127
)
128
128
129
- skippingEnum := false
130
- skippingGatewayCnt := 0
129
+ skipping := 0
130
+ braceDepth := 0
131
131
for _ , line := range lines {
132
132
if strings .HasPrefix (line , "//" ) {
133
133
ls := strings .Replace (line , "//" , "" , 1 )
@@ -137,76 +137,57 @@ func ReadFile(fpath string) (*Proto, error) {
137
137
emitComments := comments
138
138
comments = []string {}
139
139
140
- // skippin enum
141
- if strings .HasPrefix (line , "enum " ) {
142
- skippingEnum = true
143
- continue
144
- }
145
- if skippingEnum && strings .HasSuffix (line , "}" ) { // end of enum
146
- skippingEnum = false
147
- continue
148
- }
149
- if skippingEnum {
150
- continue
140
+ if strings .Contains (line , "}" ) {
141
+ braceDepth --
151
142
}
152
-
153
- // skip gengo/grpc-gateway
154
- if strings .HasPrefix (line , "option (google.api.http) = " ) {
155
- skippingGatewayCnt ++
156
- continue
143
+ if strings .Contains (line , "{" ) {
144
+ braceDepth ++
157
145
}
158
- if strings .HasPrefix (line , "post: " ) {
159
- skippingGatewayCnt ++
146
+ line = strings .TrimSpace (line )
147
+ switch {
148
+ case skipping > 0 && braceDepth > skipping :
160
149
continue
161
- }
162
- if strings .HasPrefix (line , "body: " ) {
163
- skippingGatewayCnt ++
164
- continue
165
- }
166
- if skippingGatewayCnt == 3 && line == "}" {
167
- skippingGatewayCnt ++
150
+ case strings .HasPrefix (line , "enum " ):
151
+ fallthrough
152
+ case strings .HasPrefix (line , "option (google.api.http) = " ):
153
+ skipping = braceDepth - 1
168
154
continue
169
- }
170
- if skippingGatewayCnt == 4 {
171
- skippingGatewayCnt = 0
172
- continue // end of grpc-gateway
155
+ default :
156
+ skipping = 0
173
157
}
174
158
175
159
switch mode {
176
160
case reading :
177
- for j , elem := range strings .Fields (line ) {
178
- switch j {
179
- case 0 :
180
- switch elem {
181
- case "message" :
182
- mode = parsingMessage
183
-
184
- case "service" :
185
- mode = parsingService
186
- }
187
-
188
- case 1 : // proto message/service name
189
- switch mode {
190
- case parsingMessage : // message Name
191
- protoMessage .Name = strings .Replace (elem , "{" , "" , - 1 )
192
- protoMessage .Description = strings .Join (emitComments , " " )
193
- protoMessage .Fields = []ProtoField {} // reset
194
-
195
- case parsingService : // service Name
196
- protoService .Name = strings .Replace (elem , "{" , "" , - 1 )
197
- protoService .Description = strings .Join (emitComments , " " )
198
- protoService .Methods = []ProtoMethod {} // reset
199
- }
200
- }
161
+ fs := strings .Fields (line )
162
+ if len (fs ) < 2 {
163
+ break
164
+ }
165
+ switch fs [0 ] {
166
+ case "message" :
167
+ mode = parsingMessage
168
+ case "service" :
169
+ mode = parsingService
170
+ }
171
+ switch fs [0 ] {
172
+ // proto message/service name
173
+ case "message" :
174
+ mode = parsingMessage
175
+ protoMessage .Name = strings .Replace (fs [1 ], "{" , "" , - 1 )
176
+ protoMessage .Description = strings .Join (emitComments , " " )
177
+ protoMessage .Fields = []ProtoField {} // reset
178
+ case "service" :
179
+ mode = parsingService
180
+ protoService .Name = strings .Replace (fs [1 ], "{" , "" , - 1 )
181
+ protoService .Description = strings .Join (emitComments , " " )
182
+ protoService .Methods = []ProtoMethod {} // reset
201
183
}
202
-
203
184
case parsingMessage :
204
- if strings . HasSuffix ( line , "}" ) { // closing of message
185
+ if braceDepth == 0 { // closing of message
205
186
protoMessage .FilePath = fs
206
187
rp .Messages = append (rp .Messages , protoMessage )
207
188
protoMessage = ProtoMessage {}
208
189
mode = reading
209
- continue
190
+ break
210
191
}
211
192
212
193
protoField := ProtoField {}
@@ -216,6 +197,9 @@ func ReadFile(fpath string) (*Proto, error) {
216
197
tl = strings .Replace (tl , "repeated " , "" , - 1 )
217
198
}
218
199
fds := strings .Fields (tl )
200
+ if len (fds ) < 2 {
201
+ break
202
+ }
219
203
tp , err := ToProtoType (fds [0 ])
220
204
if err != nil {
221
205
protoField .ProtoType = 0
@@ -253,7 +237,7 @@ func ReadFile(fpath string) (*Proto, error) {
253
237
protoMethod .ResponseType = f3
254
238
protoMethod .Description = strings .Join (emitComments , " " )
255
239
protoService .Methods = append (protoService .Methods , protoMethod )
256
- } else if ! strings .HasSuffix (line , "{}" ) && strings . HasSuffix ( line , "}" ) {
240
+ } else if ! strings .HasSuffix (line , "{}" ) && braceDepth == 0 {
257
241
// end of service
258
242
protoService .FilePath = fs
259
243
rp .Services = append (rp .Services , protoService )
0 commit comments