@@ -365,8 +365,10 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
365
365
goFieldName = goFieldName [len (columnPrefix ):]
366
366
}
367
367
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)
370
372
}
371
373
field := tmplField {
372
374
Name : toCamel (goFieldName ),
@@ -484,12 +486,12 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
484
486
return nil , err
485
487
}
486
488
487
- handlerStructCode , err := getHandlerStructCodes (data )
489
+ handlerStructCode , err := getHandlerStructCodes (data , opt . JSONNamedType )
488
490
if err != nil {
489
491
return nil , err
490
492
}
491
493
492
- modelStructCode , importPaths , err := getModelStructCode (data , importPath , opt .IsEmbed )
494
+ modelStructCode , importPaths , err := getModelStructCode (data , importPath , opt .IsEmbed , opt . JSONNamedType )
493
495
if err != nil {
494
496
return nil , err
495
497
}
@@ -499,7 +501,7 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
499
501
return nil , err
500
502
}
501
503
502
- protoFileCode , err := getProtoFileCode (data , opt .IsWebProto , opt .IsExtendedAPI )
504
+ protoFileCode , err := getProtoFileCode (data , opt .JSONNamedType , opt . IsWebProto , opt .IsExtendedAPI )
503
505
if err != nil {
504
506
return nil , err
505
507
}
@@ -521,7 +523,7 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
521
523
}
522
524
523
525
// 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 ) {
525
527
// filter to ignore field fields
526
528
var newFields = []tmplField {}
527
529
var newImportPaths = []string {}
@@ -611,7 +613,11 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool) (stri
611
613
structCode := string (code )
612
614
// restore the real embedded fields
613
615
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 )
615
621
structCode = strings .ReplaceAll (structCode , __type__ , replaceFields [__type__ ])
616
622
}
617
623
@@ -673,7 +679,7 @@ func getUpdateFieldsCode(data tmplData, isEmbed bool) (string, error) {
673
679
return buf .String (), nil
674
680
}
675
681
676
- func getHandlerStructCodes (data tmplData ) (string , error ) {
682
+ func getHandlerStructCodes (data tmplData , jsonNamedType int ) (string , error ) {
677
683
newFields := []tmplField {}
678
684
for _ , field := range data .Fields {
679
685
if field .DBDriver == DBDriverMongodb { // mongodb
@@ -687,7 +693,11 @@ func getHandlerStructCodes(data tmplData) (string, error) {
687
693
field .GoType = "[]*model." + strings .ReplaceAll (field .GoType , "[]*" , "" )
688
694
}
689
695
}
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
+ }
691
701
newFields = append (newFields , field )
692
702
}
693
703
data .Fields = newFields
@@ -752,8 +762,8 @@ func getModelJSONCode(data tmplData) (string, error) {
752
762
return modelJSONCode , nil
753
763
}
754
764
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 )
757
767
758
768
var err error
759
769
builder := strings.Builder {}
@@ -1007,7 +1017,7 @@ func mysqlToGoType(colTp *types.FieldType, style NullStyle) (name string, path s
1007
1017
}
1008
1018
1009
1019
// nolint
1010
- func goTypeToProto (fields []tmplField ) []tmplField {
1020
+ func goTypeToProto (fields []tmplField , jsonNameType int ) []tmplField {
1011
1021
var newFields []tmplField
1012
1022
for _ , field := range fields {
1013
1023
switch field .GoType {
@@ -1047,7 +1057,13 @@ func goTypeToProto(fields []tmplField) []tmplField {
1047
1057
field .GoType = "uint64"
1048
1058
}
1049
1059
}
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
+
1051
1067
newFields = append (newFields , field )
1052
1068
}
1053
1069
return newFields
@@ -1085,6 +1101,7 @@ var acronym = map[string]struct{}{
1085
1101
"IP" : {},
1086
1102
}
1087
1103
1104
+ // nolint
1088
1105
func toCamel (s string ) string {
1089
1106
s = strings .TrimSpace (s )
1090
1107
if s == "" {
@@ -1125,7 +1142,16 @@ func toCamel(s string) string {
1125
1142
}
1126
1143
}
1127
1144
}
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
1129
1155
}
1130
1156
1131
1157
func firstLetterToLow (str string ) string {
@@ -1141,8 +1167,42 @@ func firstLetterToLow(str string) string {
1141
1167
}
1142
1168
1143
1169
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 {
1145
1185
return str
1146
1186
}
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 )
1148
1208
}
0 commit comments