DMwangnima commented on code in PR #2380: URL: https://github.com/apache/dubbo-go/pull/2380#discussion_r1294140344
########## protocol/triple/protoc-gen-triple/gen/generator/genTriple.go: ########## @@ -0,0 +1,147 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ + +package generator + +import ( + "os" + "path/filepath" + "strings" +) + +import ( + "github.com/emicklei/proto" +) + +func (g *Generator) GenTriple() error { + p, err := g.parseFileToProto(g.ctx.Src) + if err != nil { + return err + } + triple, err := g.parseProtoToTriple(p) + if err != nil { + return err + } + bastPath, err := os.Getwd() + if err != nil { + return err + } + triple.Source, err = filepath.Rel(bastPath, g.ctx.Src) + if err != nil { + return err + } + data, err := g.parseTripleToString(triple) + if err != nil { + return err + } + g.parseGOOut(triple) + return g.generateToFile(g.ctx.GoOut, []byte(data)) +} + +func (g *Generator) parseFileToProto(filePath string) (*proto.Proto, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + + parser := proto.NewParser(file) + p, err := parser.Parse() + if err != nil { + return nil, err + } + return p, nil +} + +func (g *Generator) parseProtoToTriple(p *proto.Proto) (TripleGo, error) { + var tripleGo TripleGo + proto.Walk( + p, + proto.WithPackage(func(p *proto.Package) { + tripleGo.ProtoPackage = p.Name + tripleGo.Package = p.Name + "triple" + }), + proto.WithService(func(p *proto.Service) { + s := Service{ServiceName: p.Name} + for _, visitee := range p.Elements { + if vi, ok := visitee.(*proto.RPC); ok { + md := Method{ + MethodName: vi.Name, + RequestType: vi.RequestType, + StreamsRequest: vi.StreamsRequest, + ReturnType: vi.ReturnsType, + StreamsReturn: vi.StreamsReturns, + } + s.Methods = append(s.Methods, md) + } + } + tripleGo.Services = append(tripleGo.Services, s) + }), + proto.WithOption(func(p *proto.Option) { + if p.Name == "go_package" { + tripleGo.Import = g.ctx.GoModuleName + strings.Split(p.Constant.Source, ";")[0] Review Comment: There is a problem If go_package is a complete go package name. For example: ` option go_package = "dubbo.apache.org/dubbo-go/v3/protoc-gen-triple/internal/proto;greet" ` ########## protocol/triple/protoc-gen-triple/gen/generator/genPb.go: ########## @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package generator + +import ( + "fmt" + "os" + "path/filepath" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/protoc-gen-triple/util" +) + +func (g *Generator) GenPb() error { + pwd, err := os.Getwd() + if err != nil { + return err + } + g.genPbCmd(pwd) + output, err := util.Exec(g.ctx.ProtocCmd, pwd) + if len(output) > 0 { + fmt.Println(output) + } + return err +} + +func (g *Generator) genPbCmd(goout string) { + src := g.ctx.Src + g.ctx.ProtocCmd = fmt.Sprintf("protoc %s -I=%s --go_out=%s", filepath.Base(src), filepath.Dir(src), goout) Review Comment: For complete option go_package, maybe we should refer to protoc --go_out=paths=source_relative:. ? -- 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]
