From 9f88a43bec779f597c15afdb9442f694b0a67a4a Mon Sep 17 00:00:00 2001 From: songzhibin97 <49082129+songzhibin97@users.noreply.github.com> Date: Wed, 18 May 2022 19:23:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83=E8=87=AA=E5=8A=A8=E5=8C=96?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=8C=85=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 规范自动化插件包功能 Co-authored-by: longzhang83 <38556219+longzhang83@users.noreply.github.com> --- server/model/system/sys_auto_code.go | 60 ++++++++++++++++---------- server/service/system/sys_auto_code.go | 16 ++++--- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/server/model/system/sys_auto_code.go b/server/model/system/sys_auto_code.go index e26bee9c..48e53ab6 100644 --- a/server/model/system/sys_auto_code.go +++ b/server/model/system/sys_auto_code.go @@ -15,7 +15,7 @@ type AutoCodeStruct struct { Description string `json:"description"` // Struct中文名称 AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api AutoMoveFile bool `json:"autoMoveFile"` // 是否自动移动文件 - Fields []*Field `json:"fields"` + Fields []*Field `json:"fields,omitempty"` DictTypes []string `json:"-"` Package string `json:"package"` PackageT string `json:"-"` @@ -43,26 +43,40 @@ type SysAutoCode struct { } type AutoPlugReq struct { - PlugName string `json:"plugName"` // 必然大写开头 - Snake string `json:"snake"` // 后端自动转为 snake - RouterGroup string `json:"routerGroup"` - HasGlobal bool `json:"hasGlobal"` - HasRequest bool `json:"hasRequest"` - HasResponse bool `json:"hasResponse"` - NeedModel bool `json:"needModel"` - Global []struct { - Key string `json:"key"` - Type string `json:"type"` - Desc string `json:"desc"` - } `json:"global"` - Request []struct { - Key string `json:"key"` - Type string `json:"type"` - Desc string `json:"desc"` - } `json:"request"` - Response []struct { - Key string `json:"key"` - Type string `json:"type"` - Desc string `json:"desc"` - } `json:"response"` + PlugName string `json:"plugName"` // 必然大写开头 + Snake string `json:"snake"` // 后端自动转为 snake + RouterGroup string `json:"routerGroup"` + HasGlobal bool `json:"hasGlobal"` + HasRequest bool `json:"hasRequest"` + HasResponse bool `json:"hasResponse"` + NeedModel bool `json:"needModel"` + Global []AutoPlugInfo `json:"global,omitempty"` + Request []AutoPlugInfo `json:"request,omitempty"` + Response []AutoPlugInfo `json:"response,omitempty"` +} + +func (a *AutoPlugReq) CheckList() { + a.Global = bind(a.Global) + a.Request = bind(a.Request) + a.Response = bind(a.Response) + +} +func bind(req []AutoPlugInfo) []AutoPlugInfo { + var r []AutoPlugInfo + for _, info := range req { + if info.Effective() { + r = append(r, info) + } + } + return r +} + +type AutoPlugInfo struct { + Key string `json:"key"` + Type string `json:"type"` + Desc string `json:"desc"` +} + +func (a AutoPlugInfo) Effective() bool { + return a.Key != "" && a.Type != "" && a.Desc != "" } diff --git a/server/service/system/sys_auto_code.go b/server/service/system/sys_auto_code.go index 9ae1aef7..96aee190 100644 --- a/server/service/system/sys_auto_code.go +++ b/server/service/system/sys_auto_code.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "go.uber.org/zap" "go/ast" "go/format" "go/parser" @@ -793,10 +794,15 @@ func ImportReference(filepath, importCode, structName, packageName, groupName st // 自动创建插件模板 func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) error { + // 检查列表参数是否有效 + plug.CheckList() tplFileList, _ := autoCodeService.GetAllTplFile(plugPath, nil) for _, tpl := range tplFileList { temp, err := template.ParseFiles(tpl) - fmt.Println(err) + if err != nil { + zap.L().Error("parse err", zap.String("tpl", tpl), zap.Error(err)) + return err + } pathArr := strings.SplitAfter(tpl, "/") if strings.Index(pathArr[2], "tpl") < 0 { dirPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+pathArr[2])) @@ -804,10 +810,10 @@ func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) erro } file := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+tpl[len(plugPath):len(tpl)-4])) f, _ := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666) - e := temp.Execute(f, plug) - if e != nil { - fmt.Println(e) - return e + err = temp.Execute(f, plug) + if err != nil { + zap.L().Error("exec err", zap.String("tpl", tpl), zap.Error(err), zap.Any("plug", plug)) + return err } defer f.Close() } -- GitLab