@@ -14,8 +14,9 @@ import (
1414
1515 "github.com/Masterminds/sprig/v3"
1616 "github.com/friendsofgo/errors"
17- "github.com/volatiletech/sqlboiler/v4/drivers"
1817 "github.com/volatiletech/strmangle"
18+
19+ "github.com/volatiletech/sqlboiler/v4/drivers"
1920)
2021
2122// templateData for sqlboiler templates
@@ -58,8 +59,12 @@ type templateData struct {
5859 RelationTag string
5960
6061 // Generate struct tags as camelCase or snake_case
62+ // Deprecated: use StructTagCases instead.
6163 StructTagCasing string
6264
65+ // Generate struct tags as camelCase or snake_case
66+ StructTagCases StructTagCases
67+
6368 // Contains field names that should have tags values set to '-'
6469 TagIgnore map [string ]struct {}
6570
@@ -133,7 +138,9 @@ func (t templateList) Templates() []string {
133138 return ret
134139}
135140
136- func loadTemplates (lazyTemplates []lazyTemplate , testTemplates bool , customFuncs template.FuncMap ) (* templateList , error ) {
141+ func loadTemplates (
142+ lazyTemplates []lazyTemplate , testTemplates bool , customFuncs template.FuncMap ,
143+ ) (* templateList , error ) {
137144 tpl := template .New ("" )
138145
139146 for _ , t := range lazyTemplates {
@@ -286,13 +293,14 @@ var templateFunctions = template.FuncMap{
286293 "ignore" : strmangle .Ignore ,
287294
288295 // String Slice ops
289- "join" : func (sep string , slice []string ) string { return strings .Join (slice , sep ) },
290- "joinSlices" : strmangle .JoinSlices ,
291- "stringMap" : strmangle .StringMap ,
292- "prefixStringSlice" : strmangle .PrefixStringSlice ,
293- "containsAny" : strmangle .ContainsAny ,
294- "generateTags" : strmangle .GenerateTags ,
295- "generateIgnoreTags" : strmangle .GenerateIgnoreTags ,
296+ "join" : func (sep string , slice []string ) string { return strings .Join (slice , sep ) },
297+ "joinSlices" : strmangle .JoinSlices ,
298+ "stringMap" : strmangle .StringMap ,
299+ "prefixStringSlice" : strmangle .PrefixStringSlice ,
300+ "containsAny" : strmangle .ContainsAny ,
301+ "generateTags" : strmangle .GenerateTags ,
302+ "generateTagWithCase" : generateTagWithCase ,
303+ "generateIgnoreTags" : strmangle .GenerateIgnoreTags ,
296304
297305 // Enum ops
298306 "parseEnumName" : strmangle .ParseEnumName ,
@@ -333,3 +341,32 @@ var templateFunctions = template.FuncMap{
333341 "columnDBTypes" : drivers .ColumnDBTypes ,
334342 "getTable" : drivers .GetTable ,
335343}
344+
345+ func generateTagWithCase (tagName , tagValue , alias string , c TagCase , nullable bool ) string {
346+ buf := strmangle .GetBuffer ()
347+ defer strmangle .PutBuffer (buf )
348+
349+ buf .WriteString (tagName )
350+ buf .WriteString (`:"` )
351+ switch c {
352+ case TagCaseSnake :
353+ // we use snake case by default, so we can simply render the value to the buffer
354+ buf .WriteString (tagValue )
355+ case TagCaseTitle :
356+ buf .WriteString (strmangle .TitleCase (tagValue ))
357+ case TagCaseCamel :
358+ buf .WriteString (strmangle .CamelCase (tagValue ))
359+ case TagCaseAlias :
360+ buf .WriteString (alias )
361+ default :
362+ buf .WriteString (tagValue )
363+ }
364+
365+ if nullable {
366+ buf .WriteString (",omitempty" )
367+ }
368+
369+ buf .WriteString (`" ` )
370+
371+ return buf .String ()
372+ }
0 commit comments