From 8c0c134aa56cf64dbe24a0f6653f320a594f793b Mon Sep 17 00:00:00 2001 From: kimchen Date: Tue, 23 Jul 2024 19:57:41 +0800 Subject: [PATCH 1/4] fix(service): add gin/binding import to template.go Add the missing gin/binding import to template.go to resolve issues with service generation that were caused by an import path error. --- cmd/protoc-gen-go-gin/internal/generate/service/template.go | 1 + 1 file changed, 1 insertion(+) 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..67ba6b47 100644 --- a/cmd/protoc-gen-go-gin/internal/generate/service/template.go +++ b/cmd/protoc-gen-go-gin/internal/generate/service/template.go @@ -103,6 +103,7 @@ import ( "github.com/gin-gonic/gin" "google.golang.org/grpc/metadata" + "github.com/gin-gonic/gin/binding" "github.com/zhufuyi/sponge/pkg/gin/middleware" "github.com/zhufuyi/sponge/pkg/logger" From 1fd8bf59ad78e63dbe773fa4346f21e6d238fdfb Mon Sep 17 00:00:00 2001 From: kimchen Date: Sat, 27 Jul 2024 12:19:27 +0800 Subject: [PATCH 2/4] feat(router-gen): add gin/binding import --- cmd/protoc-gen-go-gin/internal/generate/router/gen.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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..347a1a80 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." ) @@ -36,7 +37,7 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated g.P() g.P("// import packages: ", stringsPkg.Ident(" "), contextPkg.Ident(" "), errcodePkg.Ident(" "), - middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" ")) + middlewarePkg.Ident(" "), zapPkg.Ident(" "), ginPkg.Ident(" "), ginBindingPkg.Ident(" ")) g.P() for _, s := range file.Services { From 3fdb68d708a52a17213255f81a308fa5af74629a Mon Sep 17 00:00:00 2001 From: kimchen Date: Sat, 27 Jul 2024 12:45:24 +0800 Subject: [PATCH 3/4] refactor(service): remove unused gin/binding import Removed the unused `github.com/gin-gonic/gin/binding` import from the `template.go` file to prevent compile-time issues and clean up the code. --- cmd/protoc-gen-go-gin/internal/generate/service/template.go | 2 -- 1 file changed, 2 deletions(-) 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 67ba6b47..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,8 +103,6 @@ import ( "github.com/gin-gonic/gin" "google.golang.org/grpc/metadata" - "github.com/gin-gonic/gin/binding" - "github.com/zhufuyi/sponge/pkg/gin/middleware" "github.com/zhufuyi/sponge/pkg/logger" From a282d2d6cc306c23977997f737d1de286b6f4033 Mon Sep 17 00:00:00 2001 From: kimchen Date: Sat, 27 Jul 2024 13:41:44 +0800 Subject: [PATCH 4/4] feat(router-gen): add conditional import for gin-binding based on method 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. --- .../internal/generate/router/gen.go | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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 347a1a80..f70ae9dc 100644 --- a/cmd/protoc-gen-go-gin/internal/generate/router/gen.go +++ b/cmd/protoc-gen-go-gin/internal/generate/router/gen.go @@ -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 {