github.com/songzhibin97/gkit@v1.2.13/parser/parse_pb/parse_pb.go (about) 1 package parse_pb 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 7 "github.com/emicklei/proto" 8 "github.com/songzhibin97/gkit/options" 9 "github.com/songzhibin97/gkit/parser" 10 ) 11 12 func ParsePb(filepath string, options ...options.Option) (parser.Parser, error) { 13 reader, err := ioutil.ReadFile(filepath) 14 if err != nil { 15 return nil, err 16 } 17 parser := proto.NewParser(bytes.NewReader(reader)) 18 definition, err := parser.Parse() 19 if err != nil { 20 return nil, err 21 } 22 23 ret := CreatePbParseGo() 24 for _, v := range options { 25 v(ret) 26 } 27 ret.FilePath = filepath 28 for _, element := range definition.Elements { 29 switch v := element.(type) { 30 case *proto.Package: 31 ret.PkgName = v.Name 32 case *proto.Comment: 33 // note 34 ret.AddNode(&Note{Comment: v}) 35 case *proto.Message: 36 // message 37 ret.parseMessage(v, "") 38 case *proto.Service: 39 // service 40 ret.parseService(v) 41 case *proto.Enum: 42 ret.parseEnum(v, "") 43 } 44 } 45 return ret, nil 46 }