Skip to content

Commit

Permalink
整合适配MsgDir的路由工具
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Feb 12, 2021
1 parent 0198c16 commit 24d0c0b
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 8 deletions.
40 changes: 32 additions & 8 deletions cmd/protoplus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ 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"
)

// 显示版本号
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")
Expand Down Expand Up @@ -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)
Expand All @@ -115,6 +130,15 @@ func main() {
}
}

if *flagRoute {

err = route.OutputJson(&ctx)

if err != nil {
goto OnError
}
}

return

OnError:
Expand Down
100 changes: 100 additions & 0 deletions gen/route/msgdir.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions gen/route/route.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions model/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

// 路由规则
type RouteRule struct {
MsgName string
MsgID int

SvcName string
}

// 路由表,包含多条路由规则
type RouteTable struct {
Rule []*RouteRule
}

0 comments on commit 24d0c0b

Please sign in to comment.