Skip to content

Commit

Permalink
let generateTagWithCase handle alias
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 21, 2023
1 parent 0149243 commit 4610784
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
TagCaseCamel TagCase = "camel"
TagCaseSnake TagCase = "snake"
TagCaseTitle TagCase = "title"
TagCaseAlias TagCase = "alias"
)

// Config for the running of the commands
Expand Down
4 changes: 3 additions & 1 deletion boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ var templateFunctions = template.FuncMap{
"getTable": drivers.GetTable,
}

func generateTagWithCase(tagName, tagValue string, c TagCase, nullable bool) string {
func generateTagWithCase(tagName, tagValue, alias string, c TagCase, nullable bool) string {
buf := strmangle.GetBuffer()
defer strmangle.PutBuffer(buf)

Expand All @@ -356,6 +356,8 @@ func generateTagWithCase(tagName, tagValue string, c TagCase, nullable bool) str
buf.WriteString(strmangle.TitleCase(tagValue))
case TagCaseCamel:
buf.WriteString(strmangle.CamelCase(tagValue))
case TagCaseAlias:
buf.WriteString(alias)
default:
buf.WriteString(tagValue)
}
Expand Down
23 changes: 14 additions & 9 deletions templates/main/00_struct.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ type {{$alias.UpSingular}} struct {
{{- /* render column alias and column type */ -}}
{{ $colAlias }} {{ $column.Type -}}

{{- /* handle struct tags */ -}}
{{- /*
handle struct tags
StructTagCasing will be replaced with $.StructTagCases
however we need to keep this backward compatible
$.StructTagCasing will only be used when it's set to "alias"
*/ -}}
`
{{- if eq $.StructTagCasing "alias" -}}
{{- generateTags $.Tags $colAlias -}}
{{- generateTagWithCase "json" $colAlias "default" $column.Nullable -}}
{{- generateTagWithCase "yaml" $colAlias "default" $column.Nullable -}}
{{- generateTagWithCase "toml" $colAlias "default" $column.Nullable -}}
{{- generateTagWithCase "boil" $colAlias "default" $column.Nullable -}}
{{- generateTagWithCase "json" $column.Name $colAlias "alias" $column.Nullable -}}
{{- generateTagWithCase "yaml" $column.Name $colAlias "alias" $column.Nullable -}}
{{- generateTagWithCase "toml" $column.Name $colAlias "alias" $column.Nullable -}}
{{- generateTagWithCase "boil" $column.Name $colAlias "alias" $column.Nullable -}}
{{- else -}}
{{- generateTags $.Tags $column.Name }}
{{- generateTagWithCase "json" $column.Name $.StructTagCases.Json $column.Nullable -}}
{{- generateTagWithCase "yaml" $column.Name $.StructTagCases.Yaml $column.Nullable -}}
{{- generateTagWithCase "toml" $column.Name $.StructTagCases.Toml $column.Nullable -}}
{{- generateTagWithCase "boil" $column.Name $.StructTagCases.Boil $column.Nullable -}}
{{- generateTagWithCase "json" $column.Name $colAlias $.StructTagCases.Json $column.Nullable -}}
{{- generateTagWithCase "yaml" $column.Name $colAlias $.StructTagCases.Yaml $column.Nullable -}}
{{- generateTagWithCase "toml" $column.Name $colAlias $.StructTagCases.Toml $column.Nullable -}}
{{- generateTagWithCase "boil" $column.Name $colAlias $.StructTagCases.Boil $column.Nullable -}}
{{- end -}}
`
Expand Down

0 comments on commit 4610784

Please sign in to comment.