Skip to content

Commit

Permalink
fix bug: crash when generate model with goctl. (#1777)
Browse files Browse the repository at this point in the history
* fix bug: crash when generate model with goctl.

situation: column name with line.

CREATE TABLE test (
id int NOT NULL AUTO_INCREMENT,
zh-cn text CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '中文简体',
PRIMARY KEY (id) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

* group imports

group imports

* Use

go-zero/tools/goctl/util/string.go
 func SafeString(in string) string {
instead of ReplaceAll

Co-authored-by: 方航 <fanghang@tange.ai>
  • Loading branch information
600ML and 方航 authored Apr 11, 2022
1 parent 6d9dfc0 commit fabea4c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func genField(table Table, field *parser.Field) (string, error) {
output, err := util.With("types").
Parse(text).
Execute(map[string]interface{}{
"name": field.Name.ToCamel(),
"name": util.SafeString(field.Name.ToCamel()),
"type": field.DataType,
"tag": tag,
"hasComment": field.Comment != "",
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/gen/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
expressionValues := make([]string, 0)
var count int
for _, field := range table.Fields {
camel := field.Name.ToCamel()
camel := util.SafeString(field.Name.ToCamel())
if camel == "CreateTime" || camel == "UpdateTime" {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/gen/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) {
expressionValues := make([]string, 0)
for _, field := range table.Fields {
camel := field.Name.ToCamel()
camel := util.SafeString(field.Name.ToCamel())
if camel == "CreateTime" || camel == "UpdateTime" {
continue
}
Expand Down

0 comments on commit fabea4c

Please sign in to comment.