提交 98f12a4a 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

代码生成器模板

上级 5661281e
package autoCodeModel
import (
"fmt"
log "gin-vue-admin/init/initlog"
"html/template"
"os"
)
//开发中功能,若您发现这块代码可以研究,可以无视
type AutoCodeStruct struct {
StructName string `json:"structName"`
Components []Component `json:"components"`
StructName string `json:"structName"`
PackageName string `json:"packageName"`
Abbreviation string `json:"abbreviation"`
Components []Component `json:"components"`
}
type Component struct {
ComponentName string `json:"componentName"`
ComponentType string `json:"componentType"`
ComponentJson string `json:"componentJson"`
Ismultiple bool `json:"isMultiple"`
ComponentShowType string `json:"componentShowType"`
NideDictionary bool `json:"nideDictionary"`
......@@ -20,3 +30,66 @@ type Dictionary struct {
Label string `json:"label"`
Value string `json:"value"`
}
func Temp() {
tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl")
fmt.Println(tmpl, err)
a1 := Component{
ComponentName: "TestComponent",
ComponentType: "string",
ComponentJson: "testComponent",
Ismultiple: false,
ComponentShowType: "",
NideDictionary: false,
DictionaryName: "",
ComponentDictionary: nil,
}
a2 := Component{
ComponentName: "TestBigComponent",
ComponentType: "int",
ComponentJson: "testBigComponent",
Ismultiple: false,
ComponentShowType: "",
NideDictionary: false,
DictionaryName: "",
ComponentDictionary: nil,
}
a := AutoCodeStruct{
StructName: "Test",
PackageName: "autocode",
Components: []Component{a1, a2},
}
_dir := "../" + a.PackageName
exist, err := pathExists(_dir)
if err != nil {
log.L.Info("get dir error![%v]\n", err)
return
}
if exist {
log.L.Info("has dir![%v]\n", _dir)
} else {
log.L.Info("no dir![%v]\n", _dir)
// 创建文件夹
err := os.Mkdir(_dir, os.ModePerm)
if err != nil {
log.L.Error("mkdir failed![%v]\n", err)
} else {
log.L.Info("mkdir success!\n")
}
}
file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755)
err = tmpl.Execute(file, a)
}
// 判断文件夹是否存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
package autoCodeModel
import "testing"
func TestTemp(t *testing.T) {
Temp()
}
// 自动生成模板{{.StructName}}
package {{.PackageName}}
import (
"gin-vue-admin/controller/servers"
"gin-vue-admin/init/qmsql"
"gin-vue-admin/model/modelInterface"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
type {{.StructName}} struct {
gorm.Model {{range .Components}}
{{.ComponentName}} {{.ComponentType}} `json:"{{.ComponentJson}}"` {{ end }}
}
// 创建{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Create{{.StructName}}()(err error){
err = qmsql.DEFAULTDB.Create({{.Abbreviation}}).Error
return err
}
// 删除{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Delete{{.StructName}}()(err error){
err = qmsql.DEFAULTDB.Delete({{.Abbreviation}}).Error
return err
}
// 更新{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){
}
func ({{.Abbreviation}} *{{StructName}})Create(){
}
\ No newline at end of file
......@@ -4,6 +4,9 @@
<el-form-item label="组件名称" label-width="80" :span="8">
<el-input v-model="dialogMiddle.componentName" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="组件JSON" label-width="80" :span="8">
<el-input v-model="dialogMiddle.componentJson" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="数据类型" label-width="80" :span="8">
<el-select v-model="dialogMiddle.componentType" placeholder="请选择活动区域">
<el-option
......
......@@ -7,6 +7,9 @@
<el-form-item label="Struct名称" :span="8">
<el-input v-model="form.structName"></el-input>
</el-form-item>
<el-form-item label="Package名称" :span="8">
<el-input v-model="form.packageName"></el-input>
</el-form-item>
<!-- <el-form-item label="结构类型" :span="8">
<el-select v-model="form.structType" multiple placeholder="请选择结构类型(多选)">
<el-option
......@@ -101,7 +104,8 @@ export default {
addFlag:"",
form:{
structName:"",
structType:[],
packageName:"",
components:[]
},
options:[
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册