From 13b53c315d7ab273389fd700bd6e49deb8736824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B1=E5=86=9B=E8=B6=85?= Date: Thu, 16 Oct 2025 09:18:59 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90triple?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=A4=9A=E5=8C=85=E5=BC=95=E5=85=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20=E4=BF=AE=E5=A4=8Dproto=E5=8C=85=E4=B8=AD=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89common=E5=8C=85=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=91=BD=E5=90=8D=E5=86=B2=E7=AA=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gen/generator/genTriple.go | 28 +++-------- .../gen/generator/tripleTpl.go | 10 +--- tools/protoc-gen-go-triple/go.mod | 5 +- tools/protoc-gen-go-triple/go.sum | 4 +- tools/protoc-gen-go-triple/main.go | 49 +++++++++++++++++-- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/tools/protoc-gen-go-triple/gen/generator/genTriple.go b/tools/protoc-gen-go-triple/gen/generator/genTriple.go index fb16e2bf79..7916b40067 100644 --- a/tools/protoc-gen-go-triple/gen/generator/genTriple.go +++ b/tools/protoc-gen-go-triple/gen/generator/genTriple.go @@ -18,15 +18,12 @@ package generator import ( - "errors" "os" "path/filepath" "strings" ) import ( - "github.com/golang/protobuf/protoc-gen-go/descriptor" - "google.golang.org/protobuf/compiler/protogen" ) @@ -59,21 +56,22 @@ func (g *Generator) generateToFile(filePath string, data []byte) error { return util.GoFmtFile(filePath) } -func ProcessProtoFile(file *descriptor.FileDescriptorProto) (TripleGo, error) { +func ProcessProtoFile(g *protogen.GeneratedFile, f *protogen.File) (TripleGo, error) { + file := f.Proto tripleGo := TripleGo{ Source: file.GetName(), ProtoPackage: file.GetPackage(), Services: make([]Service, 0), } - for _, service := range file.GetService() { + for k_s, service := range file.GetService() { serviceMethods := make([]Method, 0) - for _, method := range service.GetMethod() { + for k_m, method := range service.GetMethod() { serviceMethods = append(serviceMethods, Method{ MethodName: method.GetName(), - RequestType: util.ToUpper(strings.Split(method.GetInputType(), ".")[len(strings.Split(method.GetInputType(), "."))-1]), + RequestType: g.QualifiedGoIdent(f.Services[k_s].Methods[k_m].Input.GoIdent), StreamsRequest: method.GetClientStreaming(), - ReturnType: util.ToUpper(strings.Split(method.GetOutputType(), ".")[len(strings.Split(method.GetOutputType(), "."))-1]), + ReturnType: g.QualifiedGoIdent(f.Services[k_s].Methods[k_m].Output.GoIdent), StreamsReturn: method.GetServerStreaming(), }) if method.GetClientStreaming() || method.GetServerStreaming() { @@ -86,20 +84,6 @@ func ProcessProtoFile(file *descriptor.FileDescriptorProto) (TripleGo, error) { Methods: serviceMethods, }) } - var goPkg string - pkgs := strings.Split(file.Options.GetGoPackage(), ";") - switch len(pkgs) { - case 2: - tripleGo.Package = pkgs[1] - goPkg = pkgs[0] - case 1: - tripleGo.Package = file.GetPackage() - goPkg = file.GetPackage() - default: - return tripleGo, errors.New("need to set the package name in go_package") - } - - goPkg = strings.ReplaceAll(goPkg, "/", "_") _, fileName := filepath.Split(file.GetName()) tripleGo.FileName = strings.Split(fileName, ".")[0] return tripleGo, nil diff --git a/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go b/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go index 643f2d54da..6a88be9242 100644 --- a/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go +++ b/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go @@ -140,16 +140,8 @@ const ImportTpl = ` import ( "context" - {{if .IsStream}}"net/http"{{end}} -) - -import ( "dubbo.apache.org/dubbo-go/v3" - "dubbo.apache.org/dubbo-go/v3/client" - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" - "dubbo.apache.org/dubbo-go/v3/server" + {{if .IsStream}}"net/http"{{end}} ) ` diff --git a/tools/protoc-gen-go-triple/go.mod b/tools/protoc-gen-go-triple/go.mod index c5e831eeb5..4750061ec1 100644 --- a/tools/protoc-gen-go-triple/go.mod +++ b/tools/protoc-gen-go-triple/go.mod @@ -2,7 +2,4 @@ module dubbo.apache.org/dubbo-go/v3/tools/protoc-gen-go-triple go 1.23 -require ( - github.com/golang/protobuf v1.5.4 - google.golang.org/protobuf v1.34.2 -) +require google.golang.org/protobuf v1.36.10 diff --git a/tools/protoc-gen-go-triple/go.sum b/tools/protoc-gen-go-triple/go.sum index 9e46e8c155..837ddd1ee8 100644 --- a/tools/protoc-gen-go-triple/go.sum +++ b/tools/protoc-gen-go-triple/go.sum @@ -1,8 +1,8 @@ -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= diff --git a/tools/protoc-gen-go-triple/main.go b/tools/protoc-gen-go-triple/main.go index 2ac907a3cc..3018a957d8 100644 --- a/tools/protoc-gen-go-triple/main.go +++ b/tools/protoc-gen-go-triple/main.go @@ -25,6 +25,7 @@ import ( "flag" "fmt" "os" + "strings" ) import ( @@ -75,17 +76,55 @@ func main() { } func genTriple(plugin *protogen.Plugin) error { + var errors []error + for _, file := range plugin.Files { if !file.Generate { continue } - tripleGo, err := generator.ProcessProtoFile(file.Proto) - if err != nil { - return err + + // 跳过无服务的proto文件 + if len(file.Proto.GetService()) == 0 { + continue } + filename := file.GeneratedFilenamePrefix + ".triple.go" - g := plugin.NewGeneratedFile(filename, file.GoImportPath) - return generator.GenTripleFile(g, tripleGo) + // Use the same import path as the pb.go file to ensure they're in the same package + // Extract the package name from the go_package option + goPackage := file.Proto.Options.GetGoPackage() + var importPath protogen.GoImportPath + if goPackage != "" { + parts := strings.Split(goPackage, ";") + importPath = protogen.GoImportPath(parts[0]) + } else { + importPath = file.GoImportPath + } + g := plugin.NewGeneratedFile(filename, importPath) + // 导入dubbo基础库 + g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/client").Ident("client")) + g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common").Ident("common")) + g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common/constant").Ident("constant")) + g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol").Ident("triple_protocol")) + g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/server").Ident("server")) + tripleGo, err := generator.ProcessProtoFile(g, file) + if err != nil { + errors = append(errors, fmt.Errorf("processing %s: %w", file.Desc.Path(), err)) + continue + } + // Ensure the generated file uses the exact Go package name computed by protoc-gen-go. + tripleGo.Package = string(file.GoPackageName) + + err = generator.GenTripleFile(g, tripleGo) + if err != nil { + errors = append(errors, fmt.Errorf("generating %s: %w", filename, err)) + } + } + if len(errors) > 0 { + var errorMessages []string + for _, err := range errors { + errorMessages = append(errorMessages, err.Error()) + } + return fmt.Errorf("multiple errors occurred:\n%s", strings.Join(errorMessages, "\n")) } return nil } From 35cc9b909cb4a3c6bcbb4b55783a2859cb79ffb9 Mon Sep 17 00:00:00 2001 From: hs80 Date: Fri, 17 Oct 2025 10:48:25 +0800 Subject: [PATCH 2/4] Update tools/protoc-gen-go-triple/main.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除go_package手动解析 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/protoc-gen-go-triple/main.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tools/protoc-gen-go-triple/main.go b/tools/protoc-gen-go-triple/main.go index 3018a957d8..8749ae543b 100644 --- a/tools/protoc-gen-go-triple/main.go +++ b/tools/protoc-gen-go-triple/main.go @@ -91,15 +91,8 @@ func genTriple(plugin *protogen.Plugin) error { filename := file.GeneratedFilenamePrefix + ".triple.go" // Use the same import path as the pb.go file to ensure they're in the same package // Extract the package name from the go_package option - goPackage := file.Proto.Options.GetGoPackage() - var importPath protogen.GoImportPath - if goPackage != "" { - parts := strings.Split(goPackage, ";") - importPath = protogen.GoImportPath(parts[0]) - } else { - importPath = file.GoImportPath - } - g := plugin.NewGeneratedFile(filename, importPath) + // Use the import path as parsed by protogen to avoid edge cases. + g := plugin.NewGeneratedFile(filename, file.GoImportPath) // 导入dubbo基础库 g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/client").Ident("client")) g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common").Ident("common")) From 388ab9a0b1a502d15b2955cb3b2ff37a32f85d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B1=E5=86=9B=E8=B6=85?= Date: Wed, 22 Oct 2025 15:35:15 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=B8=AD=E5=AF=BC=E5=85=A5=E5=BA=93=EF=BC=8C=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E6=A0=BC=E5=BC=8F=20=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=94=B9=E4=B8=BA=E8=8B=B1=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/protoc-gen-go-triple/gen/generator/tripleTpl.go | 5 ++++- tools/protoc-gen-go-triple/main.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go b/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go index 6a88be9242..18ca04b2aa 100644 --- a/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go +++ b/tools/protoc-gen-go-triple/gen/generator/tripleTpl.go @@ -140,10 +140,13 @@ const ImportTpl = ` import ( "context" - "dubbo.apache.org/dubbo-go/v3" {{if .IsStream}}"net/http"{{end}} ) +import ( + "dubbo.apache.org/dubbo-go/v3" +) + ` const TotalTpl = `// This is a compile-time assertion to ensure that this generated file and the Triple package diff --git a/tools/protoc-gen-go-triple/main.go b/tools/protoc-gen-go-triple/main.go index 8749ae543b..1dee399c03 100644 --- a/tools/protoc-gen-go-triple/main.go +++ b/tools/protoc-gen-go-triple/main.go @@ -83,7 +83,7 @@ func genTriple(plugin *protogen.Plugin) error { continue } - // 跳过无服务的proto文件 + // Skip proto files without services. if len(file.Proto.GetService()) == 0 { continue } @@ -93,7 +93,7 @@ func genTriple(plugin *protogen.Plugin) error { // Extract the package name from the go_package option // Use the import path as parsed by protogen to avoid edge cases. g := plugin.NewGeneratedFile(filename, file.GoImportPath) - // 导入dubbo基础库 + // import Dubbo's libraries g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/client").Ident("client")) g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common").Ident("common")) g.QualifiedGoIdent(protogen.GoImportPath("dubbo.apache.org/dubbo-go/v3/common/constant").Ident("constant")) From d55c5bf9b0791dc830eb3ba8a899d3e6b11cc107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B1=E5=86=9B=E8=B6=85?= Date: Thu, 23 Oct 2025 09:12:31 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E7=9A=84=E9=94=AE=E5=90=8D=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/protoc-gen-go-triple/gen/generator/genTriple.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/protoc-gen-go-triple/gen/generator/genTriple.go b/tools/protoc-gen-go-triple/gen/generator/genTriple.go index 7916b40067..89051d4c25 100644 --- a/tools/protoc-gen-go-triple/gen/generator/genTriple.go +++ b/tools/protoc-gen-go-triple/gen/generator/genTriple.go @@ -63,15 +63,15 @@ func ProcessProtoFile(g *protogen.GeneratedFile, f *protogen.File) (TripleGo, er ProtoPackage: file.GetPackage(), Services: make([]Service, 0), } - for k_s, service := range file.GetService() { + for serviceIndex, service := range file.GetService() { serviceMethods := make([]Method, 0) - for k_m, method := range service.GetMethod() { + for methodIndex, method := range service.GetMethod() { serviceMethods = append(serviceMethods, Method{ MethodName: method.GetName(), - RequestType: g.QualifiedGoIdent(f.Services[k_s].Methods[k_m].Input.GoIdent), + RequestType: g.QualifiedGoIdent(f.Services[serviceIndex].Methods[methodIndex].Input.GoIdent), StreamsRequest: method.GetClientStreaming(), - ReturnType: g.QualifiedGoIdent(f.Services[k_s].Methods[k_m].Output.GoIdent), + ReturnType: g.QualifiedGoIdent(f.Services[serviceIndex].Methods[methodIndex].Output.GoIdent), StreamsReturn: method.GetServerStreaming(), }) if method.GetClientStreaming() || method.GetServerStreaming() {