未验证 提交 9f88a43b 编写于 作者: S songzhibin97 提交者: GitHub

规范自动化插件包功能

* 规范自动化插件包功能
Co-authored-by: Nlongzhang83 <38556219+longzhang83@users.noreply.github.com>
上级 b1c024a9
......@@ -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:"-"`
......@@ -50,19 +50,33 @@ type AutoPlugReq struct {
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 {
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"`
} `json:"response"`
}
func (a AutoPlugInfo) Effective() bool {
return a.Key != "" && a.Type != "" && a.Desc != ""
}
......@@ -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()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册