@@ -211,21 +211,12 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
211
211
}
212
212
}
213
213
}
214
- if structType != nil && requestType == nil && ! lo .Contains (names , "r" ) {
214
+ if structType != nil && ! lo .Contains (names , "r" ) {
215
215
newField := & ast.Field {
216
216
Names : []* ast.Ident {ast .NewIdent ("r" )},
217
217
Type : & ast.StarExpr {X : ast .NewIdent ("http.Request" )},
218
218
}
219
219
structType .Fields .List = append (structType .Fields .List , newField )
220
- } else if structType != nil && requestType != nil && lo .Contains (names , "r" ) {
221
- for i , v := range structType .Fields .List {
222
- if len (v .Names ) > 0 {
223
- if v .Names [0 ].Name == "r" {
224
- // 删除这个元素
225
- structType .Fields .List = append (structType .Fields .List [:i ], structType .Fields .List [i + 1 :]... )
226
- }
227
- }
228
- }
229
220
}
230
221
231
222
if structType != nil && responseType == nil && ! lo .Contains (names , "w" ) {
@@ -259,12 +250,12 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
259
250
paramNames = append (paramNames , name .Name )
260
251
}
261
252
}
262
- if requestType == nil && ! lo .Contains (paramNames , "r" ) {
253
+ if ! lo .Contains (paramNames , "r" ) {
263
254
fn .Type .Params .List = append (fn .Type .Params .List , & ast.Field {
264
255
Names : []* ast.Ident {ast .NewIdent ("r" )},
265
256
Type : & ast.StarExpr {X : ast .NewIdent ("http.Request" )},
266
257
})
267
- } else if requestType != nil && lo .Contains (paramNames , "r" ) {
258
+ } else if lo .Contains (paramNames , "r" ) {
268
259
for i , v := range fn .Type .Params .List {
269
260
if len (v .Names ) > 0 {
270
261
if v .Names [0 ].Name == "r" {
@@ -311,24 +302,13 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
311
302
}
312
303
}
313
304
314
- if requestType == nil && ! hasR {
305
+ if ! hasR {
315
306
// Add new field
316
307
newField := & ast.KeyValueExpr {
317
308
Key : ast .NewIdent ("r" ),
318
309
Value : ast .NewIdent ("r" ), // or any default value you want
319
310
}
320
311
compositeLit .Elts = append (compositeLit .Elts , newField )
321
- } else if requestType != nil && hasR {
322
- for i , v := range compositeLit .Elts {
323
- if kv , ok := v .(* ast.KeyValueExpr ); ok {
324
- if key , ok := kv .Key .(* ast.Ident ); ok {
325
- if key .Name == "r" {
326
- // 删除这个元素
327
- compositeLit .Elts = append (compositeLit .Elts [:i ], compositeLit .Elts [i + 1 :]... )
328
- }
329
- }
330
- }
331
- }
332
312
}
333
313
334
314
if responseType == nil && ! hasW {
@@ -361,11 +341,7 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
361
341
})
362
342
363
343
// check `net/http` import
364
- if requestType == nil || responseType == nil {
365
- astutil .AddImport (fset , f , "net/http" )
366
- } else if requestType != nil && responseType != nil {
367
- astutil .DeleteImport (fset , f , "net/http" )
368
- }
344
+ astutil .AddImport (fset , f , "net/http" )
369
345
}
370
346
return nil
371
347
}
0 commit comments