From f26648abfd71fc9fceeb4f2ef2d5bdc924594f87 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Tue, 27 Feb 2024 18:39:15 +0800 Subject: [PATCH] dev --- axios/request.ts | 5 +- axios/sdk.ts | 10 --- axios/service/initialize/types.ts | 86 ++++++++++++++++-- cli/main.go | 142 ++++++++++++++++++++++++++++++ go.mod | 13 +++ go.sum | 8 ++ types.ts | 109 +++++++++++++++++++++++ 7 files changed, 355 insertions(+), 18 deletions(-) delete mode 100644 axios/sdk.ts create mode 100644 cli/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 types.ts diff --git a/axios/request.ts b/axios/request.ts index 4d91a33..c43bf5b 100644 --- a/axios/request.ts +++ b/axios/request.ts @@ -1,14 +1,13 @@ import axios, { InternalAxiosRequestConfig, AxiosResponse } from "axios"; import { v4 as uuidv4 } from 'uuid'; -import { Config } from "./sdk"; // 创建 axios 实例 const service = axios.create({ - baseURL: Config.BaseUrl, + baseURL: import.meta.env.VITE_APP_BASE_API, timeout: 50000, headers: { "Request-ID":uuidv4(), - "Workspace":Config.Workspace, + "Workspace":import.meta.env.VITE_Workspace, "Content-Type": "application/json;charset=utf-8" }, }); diff --git a/axios/sdk.ts b/axios/sdk.ts deleted file mode 100644 index 722fe18..0000000 --- a/axios/sdk.ts +++ /dev/null @@ -1,10 +0,0 @@ - -export class Config { - static BaseUrl: string; // 服务器地址 - static Workspace: string; // 工作空间 - - constructor(url:string,ws:string){ - Config.BaseUrl = url; - Config.Workspace = ws; - } -} \ No newline at end of file diff --git a/axios/service/initialize/types.ts b/axios/service/initialize/types.ts index ad020fa..4e3834a 100644 --- a/axios/service/initialize/types.ts +++ b/axios/service/initialize/types.ts @@ -1,10 +1,86 @@ + + export interface Crc { - code?: string; // ping码 + code?: string; } + export interface StatusReply { - status?: number;// 状态码 - identity?: string; // 标识码 - message?: string; //状态说明 - timeseq?: number; // 响应时间序列 + status?: number; + identity?: string; + message?: string; + timeseq?: number; +} + + +export interface CheckForUpdatesRequest { + app?: string; + os?: string; + arch?: string; + version?: string; +} + + +export interface CheckForUpdatesReply { + identity?: string; + version?: string; + summary?: string; + files?: string; + pubdate?: string; +} + + +export interface Empty { + +} + + +export interface ConfigureRequest { + app?: string; + os?: string; +} + + +export interface ConfigureReply { + data?: ConfigureItem[]; +} + + +export interface ConfigureItem { + Identity?: string; + key?: string; + value?: string; + version?: number; +} + + +export interface AreasReply { + areas?: AreasItem[]; +} + + +export interface AreasItem { + id?: string; + pid?: string; + deep?: string; + name?: string; + pinyin_prefix?: string; + pinyin?: string; + ext_id?: string; + ext_name?: string; +} + + +export interface TagsReply { + tags?: TagsItem[]; +} + + +export interface TagsItem { + id?: number; + type?: number; + key?: string; + title?: string; + remark?: string; + icon?: string; } \ No newline at end of file diff --git a/cli/main.go b/cli/main.go new file mode 100644 index 0000000..bc324b5 --- /dev/null +++ b/cli/main.go @@ -0,0 +1,142 @@ +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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e9c826c --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module proto-tools + +go 1.22.0 + +require ( + git.apinb.com/bsm-sdk/engine v1.0.5 + github.com/tallstoat/pbparser v0.2.0 +) + +require ( + github.com/google/uuid v1.6.0 // indirect + github.com/jaevor/go-nanoid v1.3.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b59476a --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +git.apinb.com/bsm-sdk/engine v1.0.5 h1:nsU+5suyRFaVqHhnrU9ontpgW+54cuY/+7OnI7VEa60= +git.apinb.com/bsm-sdk/engine v1.0.5/go.mod h1:tNYgBhykUTBtpH+4EdAamcKqMwpJ3XUJ0roKqxGM3lM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg= +github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY= +github.com/tallstoat/pbparser v0.2.0 h1:lsFH4mdiOv1MIQVmge/idThSTd2uByNodWVfbtysjzg= +github.com/tallstoat/pbparser v0.2.0/go.mod h1:aUC6W9uQLeAXZkknve8ZDO6InhRYpYHlJ9kvsQh1i2k= diff --git a/types.ts b/types.ts new file mode 100644 index 0000000..813c212 --- /dev/null +++ b/types.ts @@ -0,0 +1,109 @@ + + +export interface Crc { + code?: string; +} + + + + +export interface StatusReply { + status?: number; + identity?: string; + message?: string; + timeseq?: number; +} + + + + +export interface CheckForUpdatesRequest { + app?: string; + os?: string; + arch?: string; + version?: string; +} + + + + +export interface CheckForUpdatesReply { + identity?: string; + version?: string; + summary?: string; + files?: string; + pubdate?: string; +} + + + + +export interface Empty { + +} + + + + +export interface ConfigureRequest { + app?: string; + os?: string; +} + + + + +export interface ConfigureReply { + data?: ConfigureItem[]; +} + + + + +export interface ConfigureItem { + Identity?: string; + key?: string; + value?: string; + version?: number; +} + + + + +export interface AreasReply { + areas?: AreasItem[]; +} + + + + +export interface AreasItem { + id?: string; + pid?: string; + deep?: string; + name?: string; + pinyin_prefix?: string; + pinyin?: string; + ext_id?: string; + ext_name?: string; +} + + + + +export interface TagsReply { + tags?: TagsItem[]; +} + + + + +export interface TagsItem { + id?: number; + type?: number; + key?: string; + title?: string; + remark?: string; + icon?: string; +} +