Skip to content

Commit

Permalink
support parse proto imports
Browse files Browse the repository at this point in the history
support parse proto options
support parse proto message options
  • Loading branch information
yiv committed Dec 10, 2019
1 parent 453ba89 commit 32e4f4f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
3 changes: 3 additions & 0 deletions generator/addGRPCGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (sg *AddGRPCGenerator) UpdateProtobuf(name string, iface *parser.Interface,
var pbModel *parser.Proto
if pbModel, err = pp.Parse([]byte(s)); err != nil {
logrus.Error("err", err.Error())
fmt.Println("=========s==========")
fmt.Printf("%v\n", string(s))
fmt.Println("=========e==========")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
logrus.SetReportCaller(false)
logrus.SetReportCaller(true)
viper.AutomaticEnv()
gosrc := utils.GetGOPATH() + afero.FilePathSeparator + "src" + afero.FilePathSeparator
pwd, err := os.Getwd()
Expand Down
18 changes: 15 additions & 3 deletions parser/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

type Proto struct {
PackageName string
Imports []*proto.Import
Options []*proto.Option
ServiceName string
Methods []Method
Messages []Struct
Expand All @@ -27,21 +29,25 @@ func (p *Proto) handleMessage() func(s *proto.Message) {
return func(s *proto.Message) {
fields := []NamedTypeValue{}
for _, v := range s.Elements {
var field NamedTypeValue
switch v.(type) {
case *proto.NormalField:
n := v.(*proto.NormalField)
seq := fmt.Sprintf("%v", n.Sequence)
if n.Repeated {
fields = append(fields, NewNameTypeValue(n.Name, "repeated "+n.Type, seq))
field = NewNameTypeValue(n.Name, "repeated "+n.Type, seq)
} else {
fields = append(fields, NewNameTypeValue(n.Name, n.Type, seq))
field = NewNameTypeValue(n.Name, n.Type, seq)
}
field.Options = n.Options
case *proto.MapField:
n := v.(*proto.MapField)
seq := fmt.Sprintf("%v", n.Sequence)
typeName := fmt.Sprintf("map<%v, %v>", n.KeyType, n.Type)
fields = append(fields, NewNameTypeValue(n.Name, typeName, seq))
field = NewNameTypeValue(n.Name, typeName, seq)
field.Options = n.Options
}
fields = append(fields, field)
}
message := Struct{Name: s.Name, Vars: fields}
p.Messages = append(p.Messages, message)
Expand Down Expand Up @@ -82,6 +88,12 @@ func (pp *ProtoParser) Parse(src []byte) (*Proto, error) {
case *proto.Package:
pack := v.(*proto.Package)
p.PackageName = pack.Name
case *proto.Import:
imp := v.(*proto.Import)
p.Imports = append(p.Imports, imp)
case *proto.Option:
opt := v.(*proto.Option)
p.Options = append(p.Options, opt)
}
}

Expand Down
16 changes: 12 additions & 4 deletions parser/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
func TestProtoParse(t *testing.T) {
pp := NewProtoParser()
file := `package pb;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_unkeyed_all) = xx;
option (gogoproto.goproto_unrecognized_all) = false;
option (gogoproto.goproto_sizecache_all) = false;
service User {
rpc SaveBank (EmptyReq) returns (EmptyRes) {}
}
message EmptyReq {
repeated int64 Uid = 1;
repeated int64 Uid = 1 [(gogoproto.jsontag) = "uid", (gogoproto.moretags) = 'xorm:"uid"'];
}
message EmptyRes {
string Err = 1;
Expand All @@ -27,8 +33,10 @@ func TestProtoParse(t *testing.T) {
}

fmt.Printf("PackageName %#v \n", p.PackageName)
//fmt.Printf("ServiceName %#v \n", p.ServiceName)
//fmt.Printf("Methods %#v \n", p.Methods)
//fmt.Printf("Message %#v \n", p.Messages)
fmt.Printf("ServiceName %#v \n", p.ServiceName)
fmt.Printf("Methods %#v \n", p.Methods)
fmt.Printf("Message %#v \n", p.Messages)
fmt.Printf("Imports %#v \n", p.Imports)
fmt.Printf("Options %#v \n", p.Options)

}
3 changes: 3 additions & 0 deletions parser/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package parser

import (
"strings"

"github.com/emicklei/proto"
)

type ParsedSrc interface {
Expand All @@ -15,6 +17,7 @@ type NamedTypeValue struct {
HasValue bool
Comment string
Tag string
Options []*proto.Option
}

func NewNameType(name string, tp string) NamedTypeValue {
Expand Down
4 changes: 2 additions & 2 deletions templates/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func tmplPartialsVarsTmpl() (*asset, error) {
return a, nil
}

var _tmplProtoPbTmpl = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\x90\xcf\x4a\x03\x31\x10\x87\xef\x81\xbc\xc3\x50\xf6\x60\xa1\xa4\x07\x6f\x96\x3d\x79\x56\xc4\x96\xde\x87\xdd\x71\x1b\xd6\x9d\xac\x49\xba\x58\x86\xbc\xbb\xc4\x44\xad\x22\xb9\xcc\x9f\x2f\xf9\x7d\x24\x5c\x38\xe2\x3b\xb4\xb0\x9a\xbd\x8b\xee\x76\xb5\xd3\x6a\xc6\x6e\xc4\x81\x40\x24\xba\x3d\xe3\x48\xf7\x18\x08\xcc\x53\x19\x3f\xe2\x44\x29\xed\xb4\xda\x6e\xe1\x70\xca\xd4\xef\x0d\x04\xf2\x8b\xed\x08\x7a\x7a\xb1\x6c\xa3\x75\x6c\xb4\xfa\x1a\x8a\x98\x7d\x29\x2b\x2d\x5a\x89\x78\xe4\x81\xa0\xb1\x9b\x66\x81\xbb\x16\xcc\x03\xc5\x93\xeb\x43\x4a\x00\x00\x7e\xee\x40\xa4\x59\x4c\xbd\x71\x73\xd5\x3c\xd3\xdb\x1a\x3c\xc5\xb3\xe7\xf0\x67\x11\xd6\x20\x29\x3f\x4e\xdc\xa7\x94\xb4\xca\xe7\x3b\x6a\xda\x34\x5c\xa3\x42\xc0\x81\x42\x26\xa6\x52\xe7\x34\x36\xff\xf8\x8d\xd5\xaf\x61\x73\x44\x5f\xf5\x8a\xdb\xe1\x32\x7f\xd2\x57\x9e\x6d\xe9\x8e\xf8\x7a\xce\x1f\x56\x45\xb4\xfa\x91\xca\x46\x1f\x01\x00\x00\xff\xff\xf7\xbd\x52\x55\x81\x01\x00\x00"
var _tmplProtoPbTmpl = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\x91\x41\x6b\xfb\x30\x0c\xc5\xef\x85\x7e\x07\x51\x0c\xfd\x17\x42\x2e\xff\xdb\x4a\x4e\x85\xc1\x0e\xdd\xc6\x3a\x7a\x19\x3b\x98\x54\xeb\x42\x1b\x3b\xb3\xdc\xd0\x20\xf4\xdd\x47\xec\x2c\x75\xb7\x9e\x22\xc5\x4f\xef\xfd\x64\x53\x67\xbc\x3e\x43\x01\xb3\xc6\x59\x6f\xff\xcf\x96\xd3\xc9\x74\xd2\xe8\xf2\xa0\xf7\x08\xcc\xde\x6e\x8c\x3e\xe0\x4a\x13\x42\xfe\x1c\x7f\x3f\xea\x1a\x45\x82\x90\xd9\x69\xb3\x47\x50\x75\xa6\x0c\xdc\x15\x90\x3f\xd4\x8d\x75\x9e\x44\xaa\x50\xc0\x8c\x59\x99\xfc\xbe\x3a\xa2\x09\x63\x7d\x00\x33\x9a\x9d\xc8\x6d\x83\xa7\xc6\x57\xd6\x90\x88\x0d\x05\x84\xf9\x18\x09\x45\xec\x56\xd6\x90\xd7\xc6\xe7\x1b\x7b\x72\x65\x64\x49\x3c\x09\x5d\x5b\x95\x3d\x7d\xbe\x89\xe5\x30\xce\x49\x5e\x95\xa9\x36\xe4\xad\xd1\x7f\xda\x1d\x89\x00\x00\xb8\xa6\xec\x23\xda\x9f\xc0\x7f\x49\xf3\x82\x5f\x0b\x70\xe8\x4f\xce\xd0\xaf\x03\x5a\x00\xcb\xc8\x10\x20\x6e\xad\xb6\x46\x22\xbd\x47\xea\x15\x75\xac\xaf\xd6\x4b\xf9\x0e\x03\x9f\x32\xf9\x56\xbb\x01\x2f\xb2\xbd\x76\x4d\x50\x27\x9c\x45\xec\xb6\xfa\x78\x42\x11\xe6\xea\x03\x54\x7b\xb9\xca\xb7\xd1\xf6\x9c\xa9\x2e\xd8\x26\xa7\x51\x7e\x16\xc9\x60\xd8\x80\x59\x75\x17\xeb\x79\x68\xff\x5c\xfa\x7c\x10\xbf\x0f\xdf\xe5\xf8\x04\x72\xf5\x1c\xdf\x01\x00\x00\xff\xff\x19\x19\x06\x4e\x63\x02\x00\x00"

func tmplProtoPbTmplBytes() ([]byte, error) {
return bindataRead(
Expand All @@ -454,7 +454,7 @@ func tmplProtoPbTmpl() (*asset, error) {
return nil, err
}

info := bindataFileInfo{name: "tmpl/proto.pb.tmpl", size: 385, mode: os.FileMode(438), modTime: time.Unix(1562677066, 0)}
info := bindataFileInfo{name: "tmpl/proto.pb.tmpl", size: 611, mode: os.FileMode(438), modTime: time.Unix(1575956779, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand Down
11 changes: 9 additions & 2 deletions templates/tmpl/proto.pb.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
syntax = "proto3";

package {{toSnakeCase .PackageName}};
// The {{.PackageName}} service definition.

{{range $m,$n := .Imports}}import "{{$n.Filename}}";
{{end}}

{{range $m,$n := .Options}}option {{$n.Name}} = {{$n.Constant.Source}};
{{end}}

service {{.ServiceName}} {
{{range $i,$v := .Methods}} rpc {{$v.Name}} ({{$v.Name}}Req) returns ({{$v.Name}}Res) {}
{{end}}}


{{range $m,$n := .Messages}}
message {{$n.Name}} {
{{range $k,$v := $n.Vars}} {{$v.Type}} {{$v.Name}} = {{$v.Value}};{{end}}
{{range $k,$v := $n.Vars}} {{$v.Type}} {{$v.Name}} = {{$v.Value}}{{if $v.Options}}[{{range $x,$y := $v.Options}}{{if $x}}, {{end}}{{$y.Name}} = '{{$y.Constant.Source}}'{{end}}]{{end}};{{end}}
}
{{end}}

0 comments on commit 32e4f4f

Please sign in to comment.