Skip to content

Commit

Permalink
chore: rename to struct_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Nov 2, 2023
1 parent aa17ee9 commit 0ba8ebf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ var (
func init() {
genCmd.Flags().StringVarP(&genOpts.config, "config", "c", "", "config file")
genCmd.Flags().BoolVarP(&genOpts.watch, "watch", "w", false, "watch the file changes and re-generate.")
genCmd.Flags().BoolVarP(&genOpts.force, "force", "", false, "force to execute")
genCmd.Flags().BoolVarP(&genOpts.force, "force", "f", false, "force to execute")
}
29 changes: 14 additions & 15 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"log"
"os"
"path/filepath"
"strconv"
Expand All @@ -16,18 +15,13 @@ import (
)

var (
initOpts struct {
force bool
}
initCmd = &cobra.Command{
Use: "init",
Short: "Set up a new " + strconv.Quote(config.DefaultConfigFile) + " file",
// PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// cmd.Println(`This utility will walk you through creating a sqlgen.yaml file.
// It only covers the most common items, and tries to guess sensible defaults.

// See ` + "`sqlgen init`" + ` for definitive documentation on these fields
// and exactly what they do.`)
// return nil
// },
RunE: runInitCommand,
RunE: runInitCommand,
}
)

Expand All @@ -45,7 +39,7 @@ func runInitCommand(cmd *cobra.Command, args []string) error {
},
},
{
Name: "namingConvention",
Name: "naming_convention",
Prompt: &survey.Select{
Message: "What is your naming convention:",
Options: []string{string(config.SnakeCase), string(config.CamelCase), string(config.PascalCase)},
Expand All @@ -71,13 +65,14 @@ func runInitCommand(cmd *cobra.Command, args []string) error {

var answer struct {
Driver string `survey:"driver"`
NamingConvention string `survey:"namingConvention"`
NamingConvention string `survey:"naming_convention"`
Tag string `survey:"tag"`
Strict bool `survey:"strict,omitempty"`
}

if fi, _ := os.Stat(fileDest); fi != nil {
log.Println(`Configuration file already exists`)
_, err := os.Stat(fileDest)
if !initOpts.force && !os.IsNotExist(err) {
cmd.Println(`Configuration file already exists`)
return nil
}

Expand Down Expand Up @@ -120,10 +115,14 @@ func runInitCommand(cmd *cobra.Command, args []string) error {
return nil
}

log.Println(`Creating ` + filename)
cmd.Println(`Creating ` + filename)
return codegen.Init(cfg)
}

func init() {
initCmd.Flags().BoolVarP(&initOpts.force, "force", "f", false, "force to execute")
}

// noInterruptError returns error when it's not `terminal.InterruptErr`
func noInterruptError(err error) error {
if err == terminal.InterruptErr {
Expand Down
2 changes: 1 addition & 1 deletion codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func parseGoPackage(cfg *config.Config, rootDir string, dirs []string, matcher M
// model.HasRow = !IsImplemented(t, sqlRower)

for _, f := range structs[i].fields {
tv := f.tag.Get("sql")
tv := f.tag.Get(cfg.Tag)

switch pkg.TypesInfo.TypeOf(f.t).String() {
// If the type is table name
Expand Down
5 changes: 3 additions & 2 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
const (
DefaultConfigFile = "sqlgen.yml"
DefaultGeneratedFile = "generated.go"
DefaultStructTag = "sql"
)

var cfgFilenames = []string{DefaultConfigFile, ".sqlgen.yml", ".sqlgen.yaml", "sqlgen.yaml"}
Expand All @@ -36,7 +37,7 @@ type Config struct {
Source []string `yaml:"src"`
Driver sqlDriver `yaml:"driver"`
NamingConvention naming `yaml:"naming_convention,omitempty"`
Tag string `yaml:"tag,omitempty"`
Tag string `yaml:"struct_tag,omitempty"`
Strict bool `yaml:"strict"`
Exec struct {
Filename string `yaml:"filename"`
Expand All @@ -54,7 +55,7 @@ type Config struct {
func (c *Config) init() {
c.Source = []string{"./**/*"}
c.NamingConvention = SnakeCase
c.Tag = "sql"
c.Tag = DefaultStructTag
c.Driver = MySQL
c.Strict = true
c.Exec.Filename = DefaultGeneratedFile
Expand Down
4 changes: 2 additions & 2 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func TestConfig(t *testing.T) {
cfg := DefaultConfig()
require.ElementsMatch(t, []string{"./**/*"}, cfg.Source)
require.Equal(t, MySQL, cfg.Driver)
require.Equal(t, "sql", cfg.Tag)
require.Equal(t, SnakeCase, cfg.NamingConvention)
require.Equal(t, "generated.go", cfg.Exec.Filename)
require.Equal(t, DefaultStructTag, cfg.Tag)
require.Equal(t, DefaultGeneratedFile, cfg.Exec.Filename)

require.True(t, cfg.Strict)
require.False(t, cfg.SkipHeader)
Expand Down
2 changes: 1 addition & 1 deletion codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Init(cfg *config.Config) error {
return err
}

w, err := os.OpenFile(config.DefaultConfigFile, os.O_RDWR|os.O_CREATE, 0o644)
w, err := os.OpenFile(config.DefaultConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/init.yml.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ driver: {{ .Driver }}
naming_convention: {{ .NamingConvention }}

# Optional:
tag: {{ .Tag }}
struct_tag: {{ .Tag }}

# Optional: Where should any generated code go?
exec:
Expand Down

0 comments on commit 0ba8ebf

Please sign in to comment.