Skip to content

fix: fix toSnakeCase to handle input in camel case format #2245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.