diff --git a/cmd/protoc-gen-go-gin/internal/generate/router/gen.go b/cmd/protoc-gen-go-gin/internal/generate/router/gen.go index 15c0cb49..f70ae9dc 100644 --- a/cmd/protoc-gen-go-gin/internal/generate/router/gen.go +++ b/cmd/protoc-gen-go-gin/internal/generate/router/gen.go @@ -18,6 +18,7 @@ const ( middlewarePkg = protogen.GoImportPath("github.com/zhufuyi/sponge/pkg/gin/middleware") zapPkg = protogen.GoImportPath("go.uber.org/zap") ginPkg = protogen.GoImportPath("github.com/gin-gonic/gin") + ginBindingPkg = protogen.GoImportPath("github.com/gin-gonic/gin/binding") deprecationComment = "// Deprecated: Do not use." ) @@ -35,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(" ")) + // 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 { diff --git a/cmd/protoc-gen-go-gin/internal/generate/service/template.go b/cmd/protoc-gen-go-gin/internal/generate/service/template.go index 8f760d1e..446e5fe2 100644 --- a/cmd/protoc-gen-go-gin/internal/generate/service/template.go +++ b/cmd/protoc-gen-go-gin/internal/generate/service/template.go @@ -103,7 +103,6 @@ import ( "github.com/gin-gonic/gin" "google.golang.org/grpc/metadata" - "github.com/zhufuyi/sponge/pkg/gin/middleware" "github.com/zhufuyi/sponge/pkg/logger"