From 24d0c0bb281b1404463f1275e13f28c8afc4e236 Mon Sep 17 00:00:00 2001 From: xubo Date: Fri, 12 Feb 2021 23:21:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88=E9=80=82=E9=85=8DMsgDir?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E7=94=B1=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/protoplus/main.go | 40 +++++++++++++---- gen/route/msgdir.go | 100 ++++++++++++++++++++++++++++++++++++++++++ gen/route/route.go | 62 ++++++++++++++++++++++++++ model/route.go | 14 ++++++ 4 files changed, 208 insertions(+), 8 deletions(-) create mode 100644 gen/route/msgdir.go create mode 100644 gen/route/route.go create mode 100644 model/route.go diff --git a/cmd/protoplus/main.go b/cmd/protoplus/main.go index b663d97..bf3a6db 100644 --- a/cmd/protoplus/main.go +++ b/cmd/protoplus/main.go @@ -10,6 +10,7 @@ import ( "github.com/davyxu/protoplus/gen/golang" "github.com/davyxu/protoplus/gen/json" _ "github.com/davyxu/protoplus/gen/json" + "github.com/davyxu/protoplus/gen/route" "github.com/davyxu/protoplus/model" "github.com/davyxu/protoplus/util" "os" @@ -17,14 +18,18 @@ import ( // 显示版本号 var ( - flagVersion = flag.Bool("version", false, "Show version") - flagPackage = flag.String("package", "", "package name in source files") - flagPbOut = flag.String("pb_out", "", "output google protobuf schema file") - flagGoOut = flag.String("go_out", "", "output golang source file") - flagCSOut = flag.String("cs_out", "", "output csharp source file") - flagJsonOut = flag.String("json_out", "", "output json file") - flagGoRegOut = flag.String("goreg_out", "", "output golang message register source file") - flagJson = flag.Bool("json", false, "output json to std out") + flagVersion = flag.Bool("version", false, "Show version") + flagPackage = flag.String("package", "", "package name in source files") + flagPbOut = flag.String("pb_out", "", "output google protobuf schema file") + flagGoOut = flag.String("go_out", "", "output golang source file") + flagCSOut = flag.String("cs_out", "", "output csharp source file") + flagJsonOut = flag.String("json_out", "", "output descriptor json file") + flagGoRegOut = flag.String("goreg_out", "", "output golang message register source file") + flagRouteOut = flag.String("route_out", "", "output route table json file") + + flagRoute = flag.Bool("route", false, "output route table json to std out") + + flagJson = flag.Bool("json", false, "output descriptor json to std out") flagGenReg = flag.Bool("genreg", false, "gen message register entry") flagStructBase = flag.String("structbase", "IProtoStruct", "struct inherite class type name in c#") flagCodec = flag.String("codec", "protoplus", "default codec in register entry") @@ -106,6 +111,16 @@ func main() { } } + if *flagRouteOut != "" { + ctx.OutputFileName = *flagRouteOut + + err = route.GenJson(&ctx) + + if err != nil { + goto OnError + } + } + if *flagJson { err = json.OutputJson(&ctx) @@ -115,6 +130,15 @@ func main() { } } + if *flagRoute { + + err = route.OutputJson(&ctx) + + if err != nil { + goto OnError + } + } + return OnError: diff --git a/gen/route/msgdir.go b/gen/route/msgdir.go new file mode 100644 index 0000000..3972a8d --- /dev/null +++ b/gen/route/msgdir.go @@ -0,0 +1,100 @@ +package route + +import ( + "fmt" + "github.com/davyxu/protoplus/model" + "strings" +) + +type MsgDir struct { + From, Mid, To string + Name string +} + +func (self *MsgDir) Valid() bool { + return self.From != "" && self.To != "" +} + +func (self *MsgDir) HasStar() bool { + if self.From == "*" { + return true + } + + if self.Mid == "*" { + return true + } + + if self.To == "*" { + return true + } + + return false +} + +func (self *MsgDir) Less(Other MsgDir) bool { + + if self.From != Other.From { + return self.From < Other.From + } + + if self.Mid != Other.Mid { + return self.Mid < Other.Mid + } + + if self.To != Other.To { + return self.To < Other.To + } + + return self.Name < Other.Name +} + +func parseMessage(d *model.Descriptor) (rm MsgDir) { + + msgdir := d.TagValueString("MsgDir") + if msgdir == "" { + return + } + + // 上行 + if strings.Contains(msgdir, "->") { + endPoints := strings.Split(msgdir, "->") + rm.Name = d.Name + + switch len(endPoints) { + case 3: + rm.From = strings.TrimSpace(endPoints[0]) + + rm.Mid = strings.TrimSpace(endPoints[1]) + + rm.To = strings.TrimSpace(endPoints[2]) + return + case 2: + rm.From = strings.TrimSpace(endPoints[0]) + + rm.To = strings.TrimSpace(endPoints[1]) + return + } + } else if strings.Contains(msgdir, "<-") { // 下行 + endPoints := strings.Split(msgdir, "<-") + rm.Name = d.Name + + switch len(endPoints) { + case 3: + rm.From = strings.TrimSpace(endPoints[2]) + + rm.Mid = strings.TrimSpace(endPoints[1]) + + rm.To = strings.TrimSpace(endPoints[0]) + return + case 2: + rm.From = strings.TrimSpace(endPoints[1]) + + rm.To = strings.TrimSpace(endPoints[0]) + return + } + } else { + fmt.Println("unknown msg dir", d.Name, msgdir) + } + + return +} diff --git a/gen/route/route.go b/gen/route/route.go new file mode 100644 index 0000000..6d25754 --- /dev/null +++ b/gen/route/route.go @@ -0,0 +1,62 @@ +package route + +import ( + "encoding/json" + "fmt" + "github.com/davyxu/protoplus/gen" + "github.com/davyxu/protoplus/model" + "github.com/davyxu/protoplus/msgidutil" + "io/ioutil" +) + +// 输出到文件 +func GenJson(ctx *gen.Context) error { + + data, err := genJsonData(ctx) + + if err != nil { + return err + } + + return ioutil.WriteFile(ctx.OutputFileName, data, 0666) +} + +// 将json输出到标准输出 +func OutputJson(ctx *gen.Context) error { + + data, err := genJsonData(ctx) + + if err != nil { + return err + } + + fmt.Println(string(data)) + return nil +} + +func genJsonData(ctx *gen.Context) ([]byte, error) { + + var rt model.RouteTable + + for _, d := range ctx.Structs() { + + msgDir := parseMessage(d) + msgID := msgidutil.StructMsgID(d) + + if msgDir.Valid() { + rt.Rule = append(rt.Rule, &model.RouteRule{ + MsgName: ctx.PackageName + "." + d.Name, + SvcName: msgDir.To, + MsgID: msgID, + }) + } + } + + data, err := json.MarshalIndent(&rt, "", "\t") + + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/model/route.go b/model/route.go new file mode 100644 index 0000000..ab1973c --- /dev/null +++ b/model/route.go @@ -0,0 +1,14 @@ +package model + +// 路由规则 +type RouteRule struct { + MsgName string + MsgID int + + SvcName string +} + +// 路由表,包含多条路由规则 +type RouteTable struct { + Rule []*RouteRule +}