Skip to content

Commit

Permalink
feat(router-gen): add conditional import for gin-binding based on met…
Browse files Browse the repository at this point in the history
…hod type

The router generation logic now checks for the presence of 'FORM-DATA' methodtype in the service methods. If found, it conditionally includes the 'gin-binding'
package in the generated code to support form-data parameters. This optimization
avoids unnecessary imports in cases where 'gin-binding' is not required.
  • Loading branch information
kimchen committed Jul 27, 2024
1 parent 3fdb68d commit a282d2d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/protoc-gen-go-gin/internal/generate/router/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,30 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
g.P("package ", file.GoPackageName)
g.P()

g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errcodePkg.Ident(" "),
middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "), ginBindingPkg.Ident(" "))
// check whether the service method contains the form-data parameter
needGinBindingPkg := false
for _, s := range file.Services {
for _, m := range s.Methods {
for _, rpcm := range parse.GetMethods(m) {
if rpcm.Method == "FORM-DATA" {
needGinBindingPkg = true
break
}
}
if needGinBindingPkg {
break
}
}

}
if needGinBindingPkg {
g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errcodePkg.Ident(" "),
middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "), ginBindingPkg.Ident(" "))
} else {
g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errcodePkg.Ident(" "),
middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "))
}

g.P()

for _, s := range file.Services {
Expand Down

0 comments on commit a282d2d

Please sign in to comment.