This commit is contained in:
david 2024-02-27 22:26:03 +08:00
parent f48415d46a
commit 1bd68b7efd
1 changed files with 78 additions and 2 deletions

View File

@ -62,7 +62,15 @@ func parseProtos(root, dir string) {
panic(err)
}
mess = append(mess, descs[0].GetMessageTypes()...)
if len(descs) > 0 {
mess = append(mess, descs[0].GetMessageTypes()...)
}
if len(descs[0].GetServices()) > 0 {
for _, srvItem := range descs[0].GetServices() {
writeSrvs(dir, tsPath, srvItem.GetName(), *srvItem.GetSourceInfo().TrailingComments, srvItem.GetMethods())
}
}
}
}
@ -70,8 +78,76 @@ func parseProtos(root, dir string) {
writeMessageTypes(tsPath, mess)
}
func writeSrvs(fileName string) {
func writeSrvs(PkgName, tsPath, srvName, commit string, methods []*desc.MethodDescriptor) {
var bodys []string
var types []string
var funcs []string
headerTpl := `
import request from "../../request";
import { AxiosPromise } from "axios";
import { {{Types}} } from "./types";
`
methodsTpl := `
{{Commit}}
export function {{MethodName}}(data: {{In}}): AxiosPromise<{{Reply}}> {
return request({
url: "/{{PkgName}}.{{SrvName}}.{{MethodName}}",
method: "post",
data: data,
});
}
`
for _, v := range methods {
tmp := methodsTpl
tmp = strings.ReplaceAll(tmp, "{{PkgName}}", PkgName)
tmp = strings.ReplaceAll(tmp, "{{SrvName}}", srvName)
tmp = strings.ReplaceAll(tmp, "{{MethodName}}", v.GetName())
var commit string = ""
if v.GetSourceInfo().GetLeadingComments() != "" {
commit = commit + v.GetSourceInfo().GetLeadingComments() + " "
}
if v.GetSourceInfo().GetTrailingComments() != "" {
commit = commit + v.GetSourceInfo().GetTrailingComments() + " "
}
commit = strings.ReplaceAll(commit, "\r\n", "")
if commit == "" {
tmp = strings.ReplaceAll(tmp, "{{Commit}}", "")
} else {
tmp = strings.ReplaceAll(tmp, "{{Commit}}", " // "+commit)
}
in := v.GetInputType().GetName()
reply := v.GetOutputType().GetName()
tmp = strings.ReplaceAll(tmp, "{{In}}", in)
tmp = strings.ReplaceAll(tmp, "{{Reply}}", reply)
if !utils.In(in, types) {
types = append(types, in)
}
if !utils.In(reply, types) {
types = append(types, reply)
}
funcs = append(funcs, tmp)
}
header := strings.ReplaceAll(headerTpl, "{{Types}}", strings.Join(types, ","))
bodys = append(bodys, header)
bodys = append(bodys, funcs...)
tsFileName := strings.ToLower(srvName) + ".ts"
destPath := filepath.Join(tsPath, tsFileName)
utils.StringToFile(destPath, strings.Join(bodys, "\r\n"))
}
func writeMessageTypes(tsPath string, msg []*desc.MessageDescriptor) {