修复协议代理生成的一些问题

This commit is contained in:
UnKownOwO
2023-05-31 20:53:18 +08:00
parent 98976d7a7d
commit 63b933b9f5
2 changed files with 41 additions and 26 deletions

View File

@@ -106,9 +106,9 @@ gen_proto:
gen_client_proto: gen_client_proto:
cd gate/client_proto && \ cd gate/client_proto && \
rm -rf client_proto_gen.go && \ rm -rf client_proto_gen.go && \
go test -count=1 -v -run TestClientProtoGen . && \
rm -rf proto/*.pb.go && \ rm -rf proto/*.pb.go && \
find proto -name '*.proto' | xargs -n 1 protoc --proto_path=proto --go_out=proto find proto -name '*.proto' | xargs -n 1000 protoc --proto_path=proto --go_out=proto && \
go test -count=1 -v -run TestClientProtoGen .
.PHONY: test .PHONY: test
test: test:

View File

@@ -1,6 +1,8 @@
package client_proto package client_proto
import ( import (
"bytes"
"fmt"
"os" "os"
"strings" "strings"
"testing" "testing"
@@ -31,13 +33,15 @@ func TestClientProtoGen(t *testing.T) {
clientCmdData := string(clientCmdFile) clientCmdData := string(clientCmdFile)
clientCmdLineList := strings.Split(clientCmdData, "\n") clientCmdLineList := strings.Split(clientCmdData, "\n")
// 生成代码文件 // 生成代码文件
fileData := "package client_proto\n" var fileDataBuilder strings.Builder
fileData += "\n" fileDataBuilder.WriteString(`package client_proto
fileData += "import (\n"
fileData += "\t\"hk4e/gate/client_proto/proto\"\n" import (
fileData += ")\n" "hk4e/gate/client_proto/proto"
fileData += "\n" )
fileData += "func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {\n"
func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {
`)
for _, clientCmdLine := range clientCmdLineList { for _, clientCmdLine := range clientCmdLineList {
// 清理空格以及换行符之类的 // 清理空格以及换行符之类的
clientCmdLine = strings.TrimSpace(clientCmdLine) clientCmdLine = strings.TrimSpace(clientCmdLine)
@@ -50,21 +54,32 @@ func TestClientProtoGen(t *testing.T) {
} }
cmdName := item[0] cmdName := item[0]
cmdId := item[1] cmdId := item[1]
fileData += "\tc.clientCmdIdCmdNameMap[uint16(" + cmdId + ")] = \"" + cmdName + "\"\n" _, err = fmt.Fprintf(&fileDataBuilder, ` c.clientCmdIdCmdNameMap[uint16(%s)] = "%s"
fileData += "\tc.clientCmdNameCmdIdMap[\"" + cmdName + "\"] = uint16(" + cmdId + ")\n" c.clientCmdNameCmdIdMap["%s"] = uint16(%s)
`, cmdId, cmdName, cmdName, cmdId)
if err != nil {
panic(err)
}
} }
fileData += "}\n" fileDataBuilder.WriteString(`}
fileData += "\n"
fileData += "func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {\n" func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {
fileData += "\tswitch protoObjName {\n" switch protoObjName {
`)
for _, protoObjName := range protoObjNameList { for _, protoObjName := range protoObjNameList {
fileData += "\tcase \"" + protoObjName + "\":\n\t\treturn new(proto." + protoObjName + ")\n" _, err = fmt.Fprintf(&fileDataBuilder, ` case "%s":
return new(proto.%s)
`, protoObjName, protoObjName)
if err != nil {
panic(err)
}
} }
fileData += "\tdefault:\n" fileDataBuilder.WriteString(` default:
fileData += "\t\treturn nil\n" return nil
fileData += "\t}\n" }
fileData += "}\n" }
err = os.WriteFile("./client_proto_gen.go", []byte(fileData), 0644) `)
err = os.WriteFile("./client_proto_gen.go", []byte(fileDataBuilder.String()), 0644)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -76,10 +91,10 @@ func TestClientProtoGen(t *testing.T) {
} }
rawFileStr := string(rawFileData) rawFileStr := string(rawFileData)
rawFileLine := strings.Split(rawFileStr, "\n") rawFileLine := strings.Split(rawFileStr, "\n")
newFileStr := "" var newFileBuilder bytes.Buffer
for i := 0; i < len(rawFileLine); i++ { for i := 0; i < len(rawFileLine); i++ {
line := rawFileLine[i] line := rawFileLine[i]
newFileStr += line + "\n" newFileBuilder.WriteString(line + "\n")
if !strings.Contains(line, "enum") { if !strings.Contains(line, "enum") {
continue continue
} }
@@ -94,7 +109,7 @@ func TestClientProtoGen(t *testing.T) {
continue continue
} }
for _, ref := range refEnum { for _, ref := range refEnum {
newFileStr += ref + "\n" newFileBuilder.WriteString(ref + "\n")
} }
i++ i++
for { for {
@@ -102,12 +117,12 @@ func TestClientProtoGen(t *testing.T) {
if !strings.Contains(nextLine, "}") { if !strings.Contains(nextLine, "}") {
i++ i++
} else { } else {
newFileStr += nextLine + "\n" newFileBuilder.WriteString(nextLine + "\n")
break break
} }
} }
} }
err = os.WriteFile("./proto/"+entry.Name(), []byte(newFileStr), 0644) err = os.WriteFile("./proto/"+entry.Name(), newFileBuilder.Bytes(), 0644)
if err != nil { if err != nil {
panic(err) panic(err)
} }