Skip to content

Commit

Permalink
chore: rename gtpl to go.tpl and fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Nov 2, 2023
1 parent 81045b6 commit a4c3a9a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
7 changes: 4 additions & 3 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"golang.org/x/tools/imports"
)

//go:embed templates/*.gotpl
//go:embed templates/*.go.tpl
var codegenTemplates embed.FS

type tagOption string
Expand Down Expand Up @@ -88,9 +88,10 @@ var path2regex = strings.NewReplacer(
`/`, `[\\/]`,
)

func Generate(cfg *config.Config) error {
func Generate(c *config.Config) error {
var (
srcDir string
cfg = c.Clone()
sources = make([]string, len(cfg.Source))
)

Expand Down Expand Up @@ -416,7 +417,7 @@ func parseGoPackage(cfg *config.Config, rootDir string, dirs []string, matcher M
}

blr := bytes.NewBufferString("")
tmplName := "model.gotpl"
tmplName := "model.go.tpl"
tmpl, err := template.New(tmplName).Funcs(template.FuncMap{
"quote": strconv.Quote,
"createTable": createTableStmt,
Expand Down
50 changes: 47 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ const (
PascalCase naming = "PascalCase"
)

const ConfigFile = "sqlgen.yml"
const (
DefaultConfigFile = "sqlgen.yml"
DefaultGeneratedFile = "generated.go"
)

var cfgFilenames = []string{ConfigFile, ".sqlgen.yml", ".sqlgen.yaml", "sqlgen.yaml"}
var cfgFilenames = []string{DefaultConfigFile, ".sqlgen.yml", ".sqlgen.yaml", "sqlgen.yaml"}

type Config struct {
Source []string `yaml:"src"`
Expand All @@ -54,12 +57,53 @@ func (c *Config) init() {
c.Tag = "sql"
c.Driver = MySQL
c.Strict = true
c.Exec.Filename = "generated.go"
c.Exec.Filename = DefaultGeneratedFile
c.Database.Package = "db"
c.Database.Dir = "db"
c.Database.Filename = "db.go"
}

func (c Config) Clone() *Config {
newConfig := &Config{}
newConfig.init()
newConfig.Source = make([]string, len(c.Source))
copy(newConfig.Source, c.Source)
if c.Driver != "" {
newConfig.Driver = c.Driver
}
if c.NamingConvention != "" {
newConfig.NamingConvention = c.NamingConvention
}
if c.Tag != "" {
newConfig.Tag = c.Tag
}
if c.Exec.Filename != "" {
newConfig.Exec.Filename = c.Exec.Filename
}
if c.Database.Dir != "" {
newConfig.Database.Dir = c.Database.Dir
}
if c.Database.Package != "" {
newConfig.Database.Package = c.Database.Package
}
if c.Database.Filename != "" {
newConfig.Database.Filename = c.Database.Filename
}
if c.Strict != newConfig.Strict {
newConfig.Strict = c.Strict
}
if c.SkipHeader != newConfig.SkipHeader {
newConfig.SkipHeader = c.SkipHeader
}
if c.SourceMap != newConfig.SourceMap {
newConfig.SourceMap = c.SourceMap
}
if c.SkipModTidy != newConfig.SkipModTidy {
newConfig.SkipModTidy = c.SkipModTidy
}
return newConfig
}

func (c Config) RenameFunc() func(string) string {
switch c.NamingConvention {
case SnakeCase:
Expand Down
4 changes: 2 additions & 2 deletions codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

func Init(cfg *config.Config) error {
tmpl, err := template.ParseFS(codegenTemplates, "templates/init.yml.gotpl")
tmpl, err := template.ParseFS(codegenTemplates, "templates/init.yml.go.tpl")
if err != nil {
return err
}

w, err := os.OpenFile(config.ConfigFile, os.O_RDWR|os.O_CREATE, 0o644)
w, err := os.OpenFile(config.DefaultConfigFile, os.O_RDWR|os.O_CREATE, 0o644)
if err != nil {
return err
}
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit a4c3a9a

Please sign in to comment.