2024-02-27 18:39:15 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/engine/utils"
|
2024-02-27 21:02:40 +08:00
|
|
|
"github.com/jhump/protoreflect/desc"
|
|
|
|
"github.com/jhump/protoreflect/desc/protoparse"
|
2024-02-27 18:39:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
protoPath string = "./proto"
|
|
|
|
jsPath string = "./axios/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Conv poroto file to js file.")
|
|
|
|
|
|
|
|
srvPaths := getSubDirs(protoPath)
|
|
|
|
fmt.Println("Root:", protoPath, srvPaths, "JS Out:", jsPath)
|
|
|
|
|
|
|
|
parseProtos(protoPath, srvPaths[1])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSubDirs(root string) []string {
|
|
|
|
var paths []string
|
|
|
|
|
|
|
|
files, _ := os.ReadDir(root)
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
paths = append(paths, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseProtos(root, dir string) {
|
2024-02-27 21:02:40 +08:00
|
|
|
//var msg []pbparser.MessageElement
|
|
|
|
var mess []*desc.MessageDescriptor
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
fPath := path.Join(root, dir)
|
|
|
|
tsPath := path.Join(jsPath, dir)
|
|
|
|
|
|
|
|
files, _ := os.ReadDir(fPath)
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
pfPath := filepath.Join(fPath, file.Name())
|
2024-02-27 21:02:40 +08:00
|
|
|
|
|
|
|
Parser := protoparse.Parser{
|
|
|
|
IncludeSourceCodeInfo: true,
|
|
|
|
}
|
|
|
|
descs, err := Parser.ParseFiles(pfPath)
|
2024-02-27 18:39:15 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
mess = append(mess, descs[0].GetMessageTypes()...)
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
writeMessageTypes(tsPath, mess)
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeSrvs(fileName string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
func writeMessageTypes(tsPath string, msg []*desc.MessageDescriptor) {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl := `
|
|
|
|
{{Commit}}
|
|
|
|
export interface {{Name}} {
|
|
|
|
{{Data}}
|
|
|
|
}`
|
|
|
|
|
|
|
|
keyMap := make(map[string]int)
|
|
|
|
|
|
|
|
var bodys []string
|
|
|
|
for _, msgItem := range msg {
|
|
|
|
var data []string
|
|
|
|
var msgBody = tpl
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
for _, fd := range msgItem.GetFields() {
|
2024-02-27 18:39:15 +08:00
|
|
|
data = append(data, dispFields(fd))
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
keyFname := strings.ToLower(msgItem.GetName())
|
2024-02-27 18:39:15 +08:00
|
|
|
if _, ok := keyMap[keyFname]; !ok {
|
2024-02-27 21:02:40 +08:00
|
|
|
var commit string = ""
|
|
|
|
if msgItem.GetSourceInfo().GetLeadingComments() != "" {
|
|
|
|
commit = msgItem.GetSourceInfo().GetLeadingComments()
|
|
|
|
}
|
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Commit}}", commit)
|
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Name}}", msgItem.GetName())
|
2024-02-27 18:39:15 +08:00
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Data}}", strings.Join(data, "\r\n"))
|
|
|
|
|
|
|
|
bodys = append(bodys, msgBody)
|
|
|
|
keyMap[keyFname] = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typesPath := filepath.Join(tsPath, "types.ts")
|
|
|
|
utils.StringToFile(typesPath, strings.Join(bodys, "\r\n"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
func dispFields(fd *desc.FieldDescriptor) string {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl := " {{Name}}?: {{Type}}; {{Commit}}"
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
name := strings.Trim(fd.GetName(), "")
|
2024-02-27 18:39:15 +08:00
|
|
|
name = strings.ReplaceAll(name, "\t", "")
|
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Name}}", name)
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
var commit string = ""
|
|
|
|
if fd.GetSourceInfo().GetLeadingComments() != "" {
|
|
|
|
commit = commit + fd.GetSourceInfo().GetLeadingComments() + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd.GetSourceInfo().GetTrailingComments() != "" {
|
|
|
|
commit = commit + fd.GetSourceInfo().GetTrailingComments() + " "
|
|
|
|
}
|
2024-02-27 18:39:15 +08:00
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
if commit == "" {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", "")
|
|
|
|
} else {
|
2024-02-27 21:02:40 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", " // "+commit)
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var fdType string = ""
|
2024-02-27 21:02:40 +08:00
|
|
|
switch fd.GetType().String() {
|
|
|
|
case "TYPE_STRING":
|
2024-02-27 18:39:15 +08:00
|
|
|
fdType = "string"
|
2024-02-27 21:02:40 +08:00
|
|
|
case "TYPE_INT64", "TYPE_INT32", "TYPE_FLOAT32", "TYPE_FLOAT64":
|
2024-02-27 18:39:15 +08:00
|
|
|
fdType = "number"
|
|
|
|
case "bool":
|
2024-02-27 21:02:40 +08:00
|
|
|
fdType = "TYPE_BOOL"
|
2024-02-27 18:39:15 +08:00
|
|
|
default:
|
2024-02-27 21:02:40 +08:00
|
|
|
if fd.GetLabel().String() == "LABEL_REPEATED" {
|
|
|
|
fdType = fd.GetMessageType().GetName() + "[]"
|
2024-02-27 18:39:15 +08:00
|
|
|
} else {
|
2024-02-27 21:02:40 +08:00
|
|
|
fdType = fd.GetType().String()
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Type}}", fdType)
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "\r\n", "")
|
|
|
|
|
2024-02-27 18:39:15 +08:00
|
|
|
return tpl
|
|
|
|
}
|