Skip to content

Commit

Permalink
fix: fix toSnakeCase to handle input in CamelCase format
Browse files Browse the repository at this point in the history
  • Loading branch information
orisano committed Apr 30, 2023
1 parent 5555393 commit 2d136f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golang

import (
"fmt"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -57,7 +58,14 @@ func SetCaseStyle(name string, style string) string {
}
}

var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")

func toSnakeCase(s string) string {
if !strings.ContainsRune(s, '_') {
s = camelPattern.ReplaceAllStringFunc(s, func(x string) string {
return x[:1] + "_" + x[1:]
})
}
return strings.ToLower(s)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
colName := columnName(c.Column, i)
tagName := colName

// overide col/tag with expected model name
// override col/tag with expected model name
if c.embed != nil {
colName = c.embed.modelName
tagName = SetCaseStyle(colName, "snake")
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d136f1

Please sign in to comment.