Skip to content

Commit a5b691c

Browse files
authored
fix: fix toSnakeCase to handle input in CamelCase format (#2245)
1 parent f9a6345 commit a5b691c

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

internal/codegen/golang/field.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package golang
22

33
import (
44
"fmt"
5+
"regexp"
56
"sort"
67
"strings"
78

@@ -57,7 +58,14 @@ func SetCaseStyle(name string, style string) string {
5758
}
5859
}
5960

61+
var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")
62+
6063
func toSnakeCase(s string) string {
64+
if !strings.ContainsRune(s, '_') {
65+
s = camelPattern.ReplaceAllStringFunc(s, func(x string) string {
66+
return x[:1] + "_" + x[1:]
67+
})
68+
}
6169
return strings.ToLower(s)
6270
}
6371

internal/codegen/golang/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
335335
colName := columnName(c.Column, i)
336336
tagName := colName
337337

338-
// overide col/tag with expected model name
338+
// override col/tag with expected model name
339339
if c.embed != nil {
340340
colName = c.embed.modelName
341341
tagName = SetCaseStyle(colName, "snake")

internal/endtoend/testdata/sqlc_embed/postgresql/pgx/go/query.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)