From d27e5a39ad40181cbba6ad15b6b84dc8b6b3ac2e Mon Sep 17 00:00:00 2001 From: davyxu Date: Mon, 24 May 2021 11:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81pb=E6=8C=89=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/protoplus/main.go | 5 ++- gen/pbscheme/gen_dir.go | 69 +++++++++++++++++++++++++++++++++++++++++ go.sum | 8 ----- model/descriptorset.go | 59 +++++++++++++++++++++++++++++++++++ model/field.go | 2 +- 5 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 gen/pbscheme/gen_dir.go diff --git a/cmd/protoplus/main.go b/cmd/protoplus/main.go index c90e373..8cf3b22 100644 --- a/cmd/protoplus/main.go +++ b/cmd/protoplus/main.go @@ -42,7 +42,10 @@ var ( {name: "ppgoreg_out", usage: "output protoplus message register entry in golang", outfile: ppgo.GenGoReg}, {name: "ppcs_out", usage: "output protoplus message serialize csharp source file", outfile: ppcs.GenCS}, {name: "ppcsreg_out", usage: "output protoplus message register entry in csharp", outfile: ppcs.GenCSReg}, - {name: "pbscheme_out", usage: "output google protobuf schema file", outfile: pbscheme.GenProto}, + {name: "pbscheme_out", usage: "output google protobuf schema file as single file", outfile: pbscheme.GenProto}, + + // 使用例子: protoc $(cat filelist.txt) + {name: "pbscheme_dir", usage: "output google protobuf schema files into dir", outfile: pbscheme.GenProtoDir}, {name: "ppscheme_out", usage: "output protoplus scheme json file", outfile: ppscheme.GenJson}, {name: "route_out", usage: "output route table json file", outfile: route.GenJson}, diff --git a/gen/pbscheme/gen_dir.go b/gen/pbscheme/gen_dir.go new file mode 100644 index 0000000..8aa486a --- /dev/null +++ b/gen/pbscheme/gen_dir.go @@ -0,0 +1,69 @@ +package pbscheme + +import ( + "fmt" + "github.com/davyxu/protoplus/codegen" + "github.com/davyxu/protoplus/gen" + "io/ioutil" + "path/filepath" + "strings" +) + +const protoDirCodeTemplate = `// Generated by github.com/davyxu/protoplus +// DO NOT EDIT! +syntax = "proto3"; + +option go_package= "./;proto"; + +package {{.PackageName}}; + +{{range $a, $enumobj := .DependentSource}} +import "{{.}}"; {{end}} + +{{range $a, $enumobj := .Enums}} +enum {{.Name}} { {{range .Fields}} + {{.Name}} = {{PbTagNumber $enumobj .}}; {{end}} +}{{end}} + +{{range $a, $obj := .Structs}} +{{ObjectLeadingComment .}} +message {{.Name}} { {{range .Fields}} + {{PbTypeName .}} {{GoFieldName .}} = {{PbTagNumber $obj .}};{{FieldTrailingComment .}} {{end}} +} +{{end}} +` + +func GenProtoDir(ctx *gen.Context) error { + + rootDS := ctx.DescriptorSet + + var sb strings.Builder + + for srcName, ds := range rootDS.DescriptorSetBySource() { + + ctx.DescriptorSet = ds + + gen := codegen.NewCodeGen("dirproto"). + RegisterTemplateFunc(codegen.UsefulFunc). + RegisterTemplateFunc(UsefulFunc). + ParseTemplate(protoDirCodeTemplate, ctx) + + if gen.Error() != nil { + fmt.Println(string(gen.Data())) + return gen.Error() + } + + fullPathName := filepath.Join(ctx.OutputFileName, srcName) + + fmt.Fprintf(&sb, "%s ", srcName) + + err := gen.WriteOutputFile(fullPathName).Error() + if err != nil { + return err + } + } + + err := ioutil.WriteFile(filepath.Join(ctx.OutputFileName, "filelist.txt"), []byte(sb.String()), 0666) + + return err +} diff --git a/go.sum b/go.sum index a82d02d..1c27dfb 100644 --- a/go.sum +++ b/go.sum @@ -7,17 +7,12 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI github.com/aws/aws-sdk-go v1.35.20/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davyxu/golexer v0.0.0-20180314091252-f048a86ae200 h1:qLWlQBFg5c7HUOK+UdoGouLzRaYBQr2WwoAqixU+eik= -github.com/davyxu/golexer v0.0.0-20180314091252-f048a86ae200/go.mod h1:K/1PVWqaQEwMs+N+VvaoS66o1BAAwso+jEgXKBIa8QY= github.com/davyxu/golexer v0.1.1-0.20200202091144-a15ddde83f6a h1:VYps2zwgBJPNuLGLiPrR4w6ud6cJpwlBA1OOytrCpx4= github.com/davyxu/golexer v0.1.1-0.20200202091144-a15ddde83f6a/go.mod h1:dCUDBJcBQ9/AOzz6++KvSetGgVf5zXphevt6LQvKy0A= -github.com/davyxu/goobjfmt v0.1.0/go.mod h1:KKrytCtCXny2sEg3ojQfJ4NThhBP8hKw/qM9vhDwgog= github.com/davyxu/protoplus v0.1.0/go.mod h1:WzmNYPvYsyks3G81jCJ/vGY2ljs49qFMfCmXGwvxFLA= -github.com/davyxu/ulog v1.0.0 h1:CfKRT2yK1JStZH5Yeal3BtF3kj/OETt1gTchuAbekBU= github.com/davyxu/ulog v1.0.0/go.mod h1:kg3w8bI/8AOFX/NyNVHBAUBcvX7dDHrTfxdnk1sef18= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= @@ -32,7 +27,6 @@ github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUz github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/consul/api v1.8.1/go.mod h1:sDjTOq0yUyv5G4h+BqSea7Fn6BU+XbolEz1952UB+mk= github.com/hashicorp/consul/sdk v0.7.0/go.mod h1:fY08Y9z5SvJqevyZNy6WWPXiG3KwBPAvlcdx16zZ0fM= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -70,7 +64,6 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mediocregopher/radix.v2 v0.0.0-20181115013041-b67df6e626f9/go.mod h1:fLRUbhbSd5Px2yKUaGYYPltlyxi1guJz1vCmo1RQL50= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= @@ -155,7 +148,6 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/model/descriptorset.go b/model/descriptorset.go index 96dcf37..349d134 100644 --- a/model/descriptorset.go +++ b/model/descriptorset.go @@ -4,6 +4,10 @@ type DescriptorSet struct { Objects []*Descriptor `json:",omitempty"` PackageName string Codec string + + DependentSource []string // 按文件管理的描述符取出时, 这个字段有效. 本文件依赖的其他source的symbiol + SourceName string // 按文件管理的描述符取出时, 这个字段有效. 表示本DescriptorSet的文件名 + dsBySource map[string]*DescriptorSet // 按文件名管理的描述符集合 } func (self *DescriptorSet) Services() (ret []*Descriptor) { @@ -64,3 +68,58 @@ func (self *DescriptorSet) ObjectByName(name string) *Descriptor { func (self *DescriptorSet) AddObject(d *Descriptor) { self.Objects = append(self.Objects, d) } + +func (self *DescriptorSet) addDependentSource(name string) { + + if self.SourceName == name { + return + } + + for _, n := range self.DependentSource { + if n == name { + return + } + } + + self.DependentSource = append(self.DependentSource, name) +} + +func (self *DescriptorSet) DescriptorSetBySource() map[string]*DescriptorSet { + if self.dsBySource != nil { + return self.dsBySource + } + + self.dsBySource = map[string]*DescriptorSet{} + + for _, obj := range self.Objects { + ds := self.dsBySource[obj.SrcName] + if ds == nil { + ds = &DescriptorSet{ + PackageName: self.PackageName, + Codec: self.Codec, + SourceName: obj.SrcName, + } + self.dsBySource[obj.SrcName] = ds + } + + ds.AddObject(obj) + } + + for _, file := range self.dsBySource { + for _, st := range file.Structs() { + + for _, fd := range st.Fields { + + switch fd.Kind { + case Kind_Struct, Kind_Enum: + refTarget := self.ObjectByName(fd.Type) + if refTarget != nil { + file.addDependentSource(refTarget.SrcName) + } + } + } + } + } + + return self.dsBySource +} diff --git a/model/field.go b/model/field.go index b0d28a1..ef12743 100644 --- a/model/field.go +++ b/model/field.go @@ -12,7 +12,7 @@ type FieldDescriptor struct { Tag int `json:",omitempty"` Repeatd bool `json:",omitempty"` - Descriptor *Descriptor `json:"-"` + Descriptor *Descriptor `json:"-"` // 字段归属的父级描述符 } func (self *FieldDescriptor) ParseType(str string) {