hs80 commented on code in PR #3052:
URL: https://github.com/apache/dubbo-go/pull/3052#discussion_r2438033203


##########
tools/protoc-gen-go-triple/main.go:
##########
@@ -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"))

Review Comment:
   g.QualifiedGoIdent方法内部会将包名和包路径做映射,然后放到内部变量保存,并标记此包是否已使用
   在生成代码时,protogen.GeneratedFile会将这些引用写入到生成文件的package下方



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to