Skip to content

Commit ee030b7

Browse files
committed
fix: fix toSnakeCase to handle input in CamelCase format
1 parent 5555393 commit ee030b7

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

internal/codegen/golang/field.go

Lines changed: 6 additions & 1 deletion
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,8 +58,12 @@ 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 {
61-
return strings.ToLower(s)
64+
return strings.ToLower(camelPattern.ReplaceAllStringFunc(s, func(x string) string {
65+
return x[:1] + "_" + x[1:]
66+
}))
6267
}
6368

6469
func toCamelCase(s string) string {

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)