优化rpc json生成程序

This commit is contained in:
GoEdgeLab
2022-06-22 14:36:33 +08:00
parent f83ccab9de
commit be1a53e567
3 changed files with 4447 additions and 3841 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -15,14 +15,14 @@ import (
"regexp" "regexp"
) )
type Service struct { type ServiceInfo struct {
Name string `json:"name"` Name string `json:"name"`
Methods []*Method `json:"methods"` Methods []*MethodInfo `json:"methods"`
Filename string `json:"filename"` Filename string `json:"filename"`
Doc string `json:"doc"` Doc string `json:"doc"`
} }
type Method struct { type MethodInfo struct {
Name string `json:"name"` Name string `json:"name"`
RequestMessageName string `json:"requestMessageName"` RequestMessageName string `json:"requestMessageName"`
ResponseMessageName string `json:"responseMessageName"` ResponseMessageName string `json:"responseMessageName"`
@@ -30,15 +30,15 @@ type Method struct {
Doc string `json:"doc"` Doc string `json:"doc"`
} }
type Message struct { type MessageInfo struct {
Name string `json:"name"` Name string `json:"name"`
Code string `json:"code"` Code string `json:"code"`
Doc string `json:"doc"` Doc string `json:"doc"`
} }
type RPCList struct { type RPCList struct {
Services []*Service `json:"services"` Services []*ServiceInfo `json:"services"`
Messages []*Message `json:"messages"` Messages []*MessageInfo `json:"messages"`
} }
func readComments(data []byte) string { func readComments(data []byte) string {
@@ -67,10 +67,10 @@ func main() {
flag.BoolVar(&quiet, "quiet", false, "") flag.BoolVar(&quiet, "quiet", false, "")
flag.Parse() flag.Parse()
var dirs = []string{Tea.Root + "/../pkg/rpc/protos/"} var dirs = []string{Tea.Root + "/../pkg/rpc/protos/", Tea.Root + "/../pkg/rpc/protos/models"}
var services = []*Service{} var services = []*ServiceInfo{}
var messages = []*Message{} var messages = []*MessageInfo{}
for _, dir := range dirs { for _, dir := range dirs {
func(dir string) { func(dir string) {
@@ -112,7 +112,7 @@ func main() {
var comment = readComments(data[:serviceNamePosition]) var comment = readComments(data[:serviceNamePosition])
// 方法列表 // 方法列表
var methods = []*Method{} var methods = []*MethodInfo{}
var serviceData = serviceMatch[2] var serviceData = serviceMatch[2]
var methodCodeReg = regexp.MustCompile(`\b(METHOD\d+)\b`) var methodCodeReg = regexp.MustCompile(`\b(METHOD\d+)\b`)
var methodCodeMatches = methodCodeReg.FindAllSubmatch(serviceData, -1) var methodCodeMatches = methodCodeReg.FindAllSubmatch(serviceData, -1)
@@ -123,7 +123,7 @@ func main() {
var methodPieces = methodReg.FindSubmatch(methodData) var methodPieces = methodReg.FindSubmatch(methodData)
var methodCodePosition = methodCodePositions[methodMatchIndex] var methodCodePosition = methodCodePositions[methodMatchIndex]
methods = append(methods, &Method{ methods = append(methods, &MethodInfo{
Name: string(methodPieces[1]), Name: string(methodPieces[1]),
RequestMessageName: string(methodPieces[2]), RequestMessageName: string(methodPieces[2]),
ResponseMessageName: string(methodPieces[3]), ResponseMessageName: string(methodPieces[3]),
@@ -132,7 +132,7 @@ func main() {
}) })
} }
services = append(services, &Service{ services = append(services, &ServiceInfo{
Name: serviceName, Name: serviceName,
Methods: methods, Methods: methods,
Filename: filepath.Base(path), Filename: filepath.Base(path),
@@ -186,9 +186,9 @@ func main() {
// 注释 // 注释
var index = bytes.Index(data, []byte(messageCode)) var index = bytes.Index(data, []byte(messageCode))
var messageName = string(firstMessagesReg.FindSubmatch(messageData)[1]) var messageName = string(firstMessagesReg.FindSubmatch(messageData)[1])
messages = append(messages, &Message{ messages = append(messages, &MessageInfo{
Name: messageName, Name: messageName,
Code: string(messageData), Code: string(bytes.TrimSpace(messageData)),
Doc: readComments(data[:index]), Doc: readComments(data[:index]),
}) })
} }

View File

@@ -9,6 +9,7 @@ import "models/model_server_name_auditing_result.proto";
import "models/rpc_messages.proto"; import "models/rpc_messages.proto";
import "models/model_user_plan.proto"; import "models/model_user_plan.proto";
// 网站服务相关服务
service ServerService { service ServerService {
// 创建服务 // 创建服务
rpc createServer (CreateServerRequest) returns (CreateServerResponse); rpc createServer (CreateServerRequest) returns (CreateServerResponse);