1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generator.go 3.97 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package pkg
import (
"bytes"
"errors"
"go/format"
"log"
"os"
"strings"
"text/template"
"github.com/jinzhu/gorm"
)
var (
// please remove this if qb and model in the same folder
model_file_folder string = "gitee.com/carlmax_my/console-core-go/internal/repository/"
)
// fieldConfig
type fieldConfig struct {
FieldName string
ColumnName string
FieldType string
HumpName string
CanPatch bool
}
// structConfig
type structConfig struct {
config
StructName string
OnlyFields []fieldConfig
OptionFields []fieldConfig
PrimaryKeyType string
PrimaryKeyDef string
RefStructPkgPath string
RefStructName string
}
type ImportPkg struct {
Pkg string
}
type structHelpers struct {
Titlize func(string) string
}
type config struct {
PkgName string
Helpers structHelpers
QueryBuilderName string
}
// The Generator is the one responsible for generating the code, adding the imports, formatting, and writing it to the file.
type Generator struct {
buf map[string]*bytes.Buffer
inputFolder string
config config
structConfigs []structConfig
outputFolder string
pkgName string
template *template.Template
}
// NewGenerator function creates an instance of the generator given the name of the output file as an argument.
func NewGenerator(inputFolder, outputFolder, pkgName string, template *template.Template) *Generator {
return &Generator{
buf: map[string]*bytes.Buffer{},
inputFolder: inputFolder,
outputFolder: outputFolder,
pkgName: pkgName,
template: template,
}
}
// ParserAST parse by go file
func (g *Generator) ParserAST(p *Parser, structs []string) (ret *Generator) {
for _, v := range structs {
g.buf[gorm.ToDBName(v)] = new(bytes.Buffer)
}
g.structConfigs = p.Parse()
if g.pkgName != "" {
g.config.PkgName = g.pkgName
} else {
g.config.PkgName = p.pkg.Name
}
g.config.Helpers = structHelpers{
Titlize: strings.Title,
}
g.config.QueryBuilderName = SQLColumnToHumpStyle(p.pkg.Name) + "QueryBuilder"
return g
}
func (g *Generator) checkConfig() (err error) {
if len(g.config.PkgName) == 0 {
err = errors.New("package name dose'n set")
return
}
for i := 0; i < len(g.structConfigs); i++ {
g.structConfigs[i].config = g.config
}
return
}
// Generate executes the template and store it in an internal buffer.
func (g *Generator) Generate() *Generator {
if err := g.checkConfig(); err != nil {
panic(err)
}
for _, v := range g.structConfigs {
if _, ok := g.buf[gorm.ToDBName(v.StructName)]; !ok {
continue
}
v.PkgName = g.config.PkgName
if model_file_folder != "" {
modelPkgName := "model" // v.PkgName
v.RefStructName = modelPkgName + "." + v.StructName
v.RefStructPkgPath = "\"" + model_file_folder + modelPkgName + "\""
} else {
v.RefStructName = v.StructName
v.RefStructPkgPath = "\"\""
}
// v.OnlyFields
if err := g.template.Execute(g.buf[gorm.ToDBName(v.StructName)], v); err != nil {
panic(err)
}
}
return g
}
// Format function formats the output of the generation.
func (g *Generator) Format() *Generator {
for k := range g.buf {
formattedOutput, err := format.Source(g.buf[k].Bytes())
if err != nil {
panic(err)
}
g.buf[k] = bytes.NewBuffer(formattedOutput)
}
return g
}
// IsExists whether file exist
func IsExists(path string) (os.FileInfo, bool) {
f, err := os.Stat(path)
return f, err == nil || os.IsExist(err)
}
// Flush function writes the output to the output file.
func (g *Generator) Flush() error {
for k := range g.buf {
if g.outputFolder != "." {
_, b := IsExists(g.outputFolder)
if !b {
err := os.Mkdir(g.outputFolder, 0766)
if err != nil {
panic(err)
}
log.Println("create dir : ", g.outputFolder)
}
}
filename := g.outputFolder + "/gen_" + strings.ToLower(k) + ".go"
bytes := g.buf[k].Bytes()
if err := os.WriteFile(filename, bytes, 0777); err != nil {
log.Fatalln(err)
}
log.Println(" └── file : ", filename, len(bytes))
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385