Skip to content

#191. Configurable struct tags casing by cmdline argument struct-tag-casing #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *State) Run(includeTests bool) error {
PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
StructTagCasing: s.Config.StructTagCasing,
Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
Expand Down Expand Up @@ -142,6 +143,7 @@ func (s *State) Run(includeTests bool) error {
PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
StructTagCasing: s.Config.StructTagCasing,
Tags: s.Config.Tags,
Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
Expand Down
1 change: 1 addition & 0 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
NoHooks bool
NoAutoTimestamps bool
Wipe bool
StructTagCasing string

Postgres PostgresConfig
MySQL MySQLConfig
Expand Down
3 changes: 3 additions & 0 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type templateData struct {
// Tags control which
Tags []string

// Generate struct tags as camelCase or snake_case
StructTagCasing string

// StringFuncs are usable in templates with stringMap
StringFuncs map[string]func(string) string

Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func main() {
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version")
rootCmd.PersistentFlags().BoolP("tinyint-as-bool", "", false, "Map MySQL tinyint(1) in Go to bool instead of int8")
rootCmd.PersistentFlags().BoolP("wipe", "", false, "Delete the output folder (rm -rf) before generation to ensure sanity")
rootCmd.PersistentFlags().StringP("struct-tag-casing", "", "snake", "Decides the casing for go structure tag names. camel or snake (default snake)")

// hide flags not recommended for use
rootCmd.PersistentFlags().MarkHidden("replace")
Expand Down Expand Up @@ -141,6 +142,7 @@ func preRun(cmd *cobra.Command, args []string) error {
NoHooks: viper.GetBool("no-hooks"),
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
Wipe: viper.GetBool("wipe"),
StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake
}

// BUG: https://github.com/spf13/viper/issues/200
Expand Down
9 changes: 7 additions & 2 deletions templates/00_struct.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
{{- $tableNameSingular := .Table.Name | singular -}}
{{- $modelName := $tableNameSingular | titleCase -}}
{{- $modelNameCamel := $tableNameSingular | camelCase -}}

// {{$modelName}} is an object representing the database table.
type {{$modelName}} struct {
{{range $column := .Table.Columns -}}
{{titleCase $column.Name}} {{$column.Type}} `{{generateTags $dot.Tags $column.Name}}boil:"{{$column.Name}}" json:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}" toml:"{{$column.Name}}" yaml:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}"`
{{range $column := .Table.Columns }}
{{- if eq $dot.StructTagCasing "camel"}}
{{titleCase $column.Name}} {{$column.Type}} `{{generateTags $dot.Tags $column.Name}}boil:"{{$column.Name}}" json:"{{$column.Name | camelCase}}{{if $column.Nullable}},omitempty{{end}}" toml:"{{$column.Name | camelCase}}" yaml:"{{$column.Name | camelCase}}{{if $column.Nullable}},omitempty{{end}}"`
{{- else -}}
{{titleCase $column.Name}} {{$column.Type}} `{{generateTags $dot.Tags $column.Name}}boil:"{{$column.Name}}" json:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}" toml:"{{$column.Name}}" yaml:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}"`
{{end -}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Templates use tabs for indentation like Go code. The inconsistency is visible on the Github PR.

{{end -}}
{{- if .Table.IsJoinTable -}}
{{- else}}
Expand Down