Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions tools/protoc-gen-go-triple/gen/generator/genTriple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k_s 别取这种名字,你把这段代码粘贴到 chatgpt,可以咨询下,让它给你改个更合适的名字

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

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() {
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions tools/protoc-gen-go-triple/gen/generator/tripleTpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,8 @@ const ImportTpl = `

import (
"context"
{{if .IsStream}}"net/http"{{end}}
)

import (
"dubbo.apache.org/dubbo-go/v3"
Comment thread
hs80 marked this conversation as resolved.
Outdated
"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}}
)

`
Expand Down
5 changes: 1 addition & 4 deletions tools/protoc-gen-go-triple/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tools/protoc-gen-go-triple/go.sum
Original file line number Diff line number Diff line change
@@ -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=
49 changes: 44 additions & 5 deletions tools/protoc-gen-go-triple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"flag"
"fmt"
"os"
"strings"
)

import (
Expand Down Expand Up @@ -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文件

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use English

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)
Comment thread
hs80 marked this conversation as resolved.
Outdated
// 导入dubbo基础库

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use English

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"))
Comment thread
hs80 marked this conversation as resolved.
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
}
Expand Down
Loading