From a4c3a9a2787f921ce25e7089ab712df7d0f3101c Mon Sep 17 00:00:00 2001 From: si3nloong Date: Thu, 2 Nov 2023 11:02:52 +0800 Subject: [PATCH] chore: rename `gtpl` to `go.tpl` and fix config --- codegen/codegen.go | 7 +-- codegen/config/config.go | 50 +++++++++++++++++-- codegen/generate.go | 4 +- .../{init.yml.gotpl => init.yml.go.tpl} | 0 .../templates/{model.gotpl => model.go.tpl} | 0 5 files changed, 53 insertions(+), 8 deletions(-) rename codegen/templates/{init.yml.gotpl => init.yml.go.tpl} (100%) rename codegen/templates/{model.gotpl => model.go.tpl} (100%) diff --git a/codegen/codegen.go b/codegen/codegen.go index e88f116..763c78d 100644 --- a/codegen/codegen.go +++ b/codegen/codegen.go @@ -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 @@ -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)) ) @@ -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, diff --git a/codegen/config/config.go b/codegen/config/config.go index 94c7401..9dc0b8d 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -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"` @@ -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: diff --git a/codegen/generate.go b/codegen/generate.go index d1a6e73..17a7e0f 100644 --- a/codegen/generate.go +++ b/codegen/generate.go @@ -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 } diff --git a/codegen/templates/init.yml.gotpl b/codegen/templates/init.yml.go.tpl similarity index 100% rename from codegen/templates/init.yml.gotpl rename to codegen/templates/init.yml.go.tpl diff --git a/codegen/templates/model.gotpl b/codegen/templates/model.go.tpl similarity index 100% rename from codegen/templates/model.gotpl rename to codegen/templates/model.go.tpl