Skip to content

Commit 144e71b

Browse files
committed
fix: snake case style
1 parent 98c6ef7 commit 144e71b

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

pkg/sql2code/parser/parser.go

+76-16
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,10 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
365365
goFieldName = goFieldName[len(columnPrefix):]
366366
}
367367
jsonName := colName
368-
if opt.JSONNamedType != 0 {
369-
jsonName = xstrings.FirstRuneToLower(xstrings.ToCamelCase(colName)) // name type use camel case
368+
if opt.JSONNamedType == 0 { // snake case
369+
jsonName = customToSnake(jsonName)
370+
} else {
371+
jsonName = customToCamel(jsonName) // camel case (default)
370372
}
371373
field := tmplField{
372374
Name: toCamel(goFieldName),
@@ -484,12 +486,12 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
484486
return nil, err
485487
}
486488

487-
handlerStructCode, err := getHandlerStructCodes(data)
489+
handlerStructCode, err := getHandlerStructCodes(data, opt.JSONNamedType)
488490
if err != nil {
489491
return nil, err
490492
}
491493

492-
modelStructCode, importPaths, err := getModelStructCode(data, importPath, opt.IsEmbed)
494+
modelStructCode, importPaths, err := getModelStructCode(data, importPath, opt.IsEmbed, opt.JSONNamedType)
493495
if err != nil {
494496
return nil, err
495497
}
@@ -499,7 +501,7 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
499501
return nil, err
500502
}
501503

502-
protoFileCode, err := getProtoFileCode(data, opt.IsWebProto, opt.IsExtendedAPI)
504+
protoFileCode, err := getProtoFileCode(data, opt.JSONNamedType, opt.IsWebProto, opt.IsExtendedAPI)
503505
if err != nil {
504506
return nil, err
505507
}
@@ -521,7 +523,7 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
521523
}
522524

523525
// nolint
524-
func getModelStructCode(data tmplData, importPaths []string, isEmbed bool) (string, []string, error) {
526+
func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonNamedType int) (string, []string, error) {
525527
// filter to ignore field fields
526528
var newFields = []tmplField{}
527529
var newImportPaths = []string{}
@@ -611,7 +613,11 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool) (stri
611613
structCode := string(code)
612614
// restore the real embedded fields
613615
if isEmbed {
614-
structCode = strings.ReplaceAll(structCode, __mysqlModel__, replaceFields[__mysqlModel__])
616+
gormEmbed := replaceFields[__mysqlModel__]
617+
if jsonNamedType == 0 { // snake case
618+
gormEmbed += "2" // ggorm.Model2
619+
}
620+
structCode = strings.ReplaceAll(structCode, __mysqlModel__, gormEmbed)
615621
structCode = strings.ReplaceAll(structCode, __type__, replaceFields[__type__])
616622
}
617623

@@ -673,7 +679,7 @@ func getUpdateFieldsCode(data tmplData, isEmbed bool) (string, error) {
673679
return buf.String(), nil
674680
}
675681

676-
func getHandlerStructCodes(data tmplData) (string, error) {
682+
func getHandlerStructCodes(data tmplData, jsonNamedType int) (string, error) {
677683
newFields := []tmplField{}
678684
for _, field := range data.Fields {
679685
if field.DBDriver == DBDriverMongodb { // mongodb
@@ -687,7 +693,11 @@ func getHandlerStructCodes(data tmplData) (string, error) {
687693
field.GoType = "[]*model." + strings.ReplaceAll(field.GoType, "[]*", "")
688694
}
689695
}
690-
field.JSONName = customToCamel(field.ColName)
696+
if jsonNamedType == 0 { // snake case
697+
field.JSONName = customToSnake(field.ColName)
698+
} else {
699+
field.JSONName = customToCamel(field.ColName) // camel case (default)
700+
}
691701
newFields = append(newFields, field)
692702
}
693703
data.Fields = newFields
@@ -752,8 +762,8 @@ func getModelJSONCode(data tmplData) (string, error) {
752762
return modelJSONCode, nil
753763
}
754764

755-
func getProtoFileCode(data tmplData, isWebProto bool, isExtendedAPI bool) (string, error) {
756-
data.Fields = goTypeToProto(data.Fields)
765+
func getProtoFileCode(data tmplData, jsonNamedType int, isWebProto bool, isExtendedAPI bool) (string, error) {
766+
data.Fields = goTypeToProto(data.Fields, jsonNamedType)
757767

758768
var err error
759769
builder := strings.Builder{}
@@ -1007,7 +1017,7 @@ func mysqlToGoType(colTp *types.FieldType, style NullStyle) (name string, path s
10071017
}
10081018

10091019
// nolint
1010-
func goTypeToProto(fields []tmplField) []tmplField {
1020+
func goTypeToProto(fields []tmplField, jsonNameType int) []tmplField {
10111021
var newFields []tmplField
10121022
for _, field := range fields {
10131023
switch field.GoType {
@@ -1047,7 +1057,13 @@ func goTypeToProto(fields []tmplField) []tmplField {
10471057
field.GoType = "uint64"
10481058
}
10491059
}
1050-
field.JSONName = customToCamel(field.ColName)
1060+
1061+
if jsonNameType == 0 { // snake case
1062+
field.JSONName = customToSnake(field.ColName)
1063+
} else {
1064+
field.JSONName = customToCamel(field.ColName) // camel case (default)
1065+
}
1066+
10511067
newFields = append(newFields, field)
10521068
}
10531069
return newFields
@@ -1085,6 +1101,7 @@ var acronym = map[string]struct{}{
10851101
"IP": {},
10861102
}
10871103

1104+
// nolint
10881105
func toCamel(s string) string {
10891106
s = strings.TrimSpace(s)
10901107
if s == "" {
@@ -1125,7 +1142,16 @@ func toCamel(s string) string {
11251142
}
11261143
}
11271144
}
1128-
return n.String()
1145+
str := n.String()
1146+
1147+
if len(str) > 2 {
1148+
if str[len(str)-2:] == "Id" {
1149+
str = str[:len(str)-2] + "ID"
1150+
} else if str[len(str)-2:] == "Ip" {
1151+
str = str[:len(str)-2] + "IP"
1152+
}
1153+
}
1154+
return str
11291155
}
11301156

11311157
func firstLetterToLow(str string) string {
@@ -1141,8 +1167,42 @@ func firstLetterToLow(str string) string {
11411167
}
11421168

11431169
func customToCamel(str string) string {
1144-
if strings.ToLower(str) == "id" {
1170+
str = firstLetterToLow(toCamel(str))
1171+
1172+
if len(str) == 2 {
1173+
if str == "iD" {
1174+
str = "id"
1175+
} else if str == "iP" {
1176+
str = "ip"
1177+
}
1178+
}
1179+
1180+
return str
1181+
}
1182+
1183+
func customToSnake(str string) string {
1184+
if len(str) == 0 {
11451185
return str
11461186
}
1147-
return firstLetterToLow(toCamel(str))
1187+
1188+
index := 0
1189+
for _, c := range str {
1190+
if c != '_' {
1191+
break
1192+
}
1193+
index++
1194+
}
1195+
if index != 0 {
1196+
str = str[index:]
1197+
}
1198+
1199+
if len(str) == 2 {
1200+
if str == "iD" {
1201+
str = "id"
1202+
} else if str == "iP" {
1203+
str = "ip"
1204+
}
1205+
}
1206+
1207+
return xstrings.ToSnakeCase(str)
11481208
}

pkg/sql2code/parser/parser_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,20 @@ func TestParseSQLs(t *testing.T) {
115115
}
116116
}
117117

118-
func Test_toCamel(t *testing.T) {
119-
names := []string{"id", "user_id", "productId", "orderId", "user_name", "host_ip", "teacherId"}
118+
func TestConvertNames(t *testing.T) {
119+
names := []string{"_id", "id", "iD", "user_id", "productId", "orderID", "user_name", "ip", "iP", "host_ip", "myIP"}
120120
var convertNames []string
121121
var convertNames2 []string
122+
var convertNames3 []string
122123
for _, name := range names {
123124
convertNames = append(convertNames, toCamel(name))
124125
convertNames2 = append(convertNames2, customToCamel(name))
126+
convertNames3 = append(convertNames3, customToSnake(name))
125127
}
126-
t.Log(convertNames)
127-
t.Log(convertNames2)
128+
t.Log("source: ", names)
129+
t.Log("toCamel: ", convertNames)
130+
t.Log("customToCamel:", convertNames2)
131+
t.Log("customToSnake:", convertNames3)
128132
}
129133

130134
func Test_parseOption(t *testing.T) {
@@ -173,7 +177,7 @@ func Test_goTypeToProto(t *testing.T) {
173177
{GoType: "uint"},
174178
{GoType: "time.Time"},
175179
}
176-
v := goTypeToProto(fields)
180+
v := goTypeToProto(fields, 1)
177181
assert.NotNil(t, v)
178182
}
179183

0 commit comments

Comments
 (0)