代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。