-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |