143 lines
2.7 KiB
Go
143 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.apinb.com/bsm-sdk/engine/utils"
|
|
"github.com/tallstoat/pbparser"
|
|
)
|
|
|
|
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) {
|
|
var msg []pbparser.MessageElement
|
|
|
|
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())
|
|
pb, err := pbparser.ParseFile(pfPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
msg = append(msg, pb.Messages...)
|
|
|
|
}
|
|
}
|
|
|
|
writeTypes(tsPath, msg)
|
|
}
|
|
|
|
func writeSrvs(fileName string) {
|
|
|
|
}
|
|
|
|
func writeTypes(tsPath string, msg []pbparser.MessageElement) {
|
|
tpl := `
|
|
{{Commit}}
|
|
export interface {{Name}} {
|
|
{{Data}}
|
|
}`
|
|
|
|
keyMap := make(map[string]int)
|
|
|
|
var bodys []string
|
|
for _, msgItem := range msg {
|
|
var data []string
|
|
var msgBody = tpl
|
|
|
|
for _, fd := range msgItem.Fields {
|
|
data = append(data, dispFields(fd))
|
|
}
|
|
|
|
keyFname := strings.ToLower(msgItem.Name)
|
|
if _, ok := keyMap[keyFname]; !ok {
|
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Commit}}", msgItem.Documentation)
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Name}}", msgItem.Name)
|
|
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"))
|
|
|
|
}
|
|
|
|
func dispFields(fd pbparser.FieldElement) string {
|
|
tpl := " {{Name}}?: {{Type}}; {{Commit}}"
|
|
|
|
name := strings.Trim(fd.Name, "")
|
|
name = strings.ReplaceAll(name, "\t", "")
|
|
tpl = strings.ReplaceAll(tpl, "{{Name}}", name)
|
|
|
|
jsonByte, _ := json.Marshal(fd)
|
|
fmt.Println(string(jsonByte))
|
|
|
|
if fd.Documentation == "" {
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", "")
|
|
} else {
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", " // "+fd.Documentation)
|
|
}
|
|
|
|
var fdType string = ""
|
|
switch fd.Type.Name() {
|
|
case "string":
|
|
fdType = "string"
|
|
case "int32", "int64", "float":
|
|
fdType = "number"
|
|
case "bool":
|
|
fdType = "bool"
|
|
default:
|
|
if fd.Label == "repeated" {
|
|
fdType = fd.Type.Name() + "[]"
|
|
} else {
|
|
fdType = fd.Type.Name()
|
|
}
|
|
}
|
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Type}}", fdType)
|
|
|
|
return tpl
|
|
}
|