From 571dce61f77953bddc2c3676674b6ffeab4e318e Mon Sep 17 00:00:00 2001 From: RQ <935299904@qq.com> Date: Wed, 23 Nov 2022 00:02:43 +0800 Subject: [PATCH 1/3] Generate multiple files --- migration/artisan.go | 7 +++--- table2struct.go | 57 ++++++++++++++++++++++++++++++++++++++++--- test/generate_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 test/generate_test.go diff --git a/migration/artisan.go b/migration/artisan.go index d179d27..0c49c82 100644 --- a/migration/artisan.go +++ b/migration/artisan.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/gohouse/gorose" ) @@ -18,6 +19,6 @@ var err error func init() { } func main() { - conn,err = gorose.Open("mysql", "gcore:gcore@tcp(192.168.200.248:3306)/test?charset=utf8") - fmt.Println(conn,err) -} \ No newline at end of file + conn, err = gorose.Open("mysql", "gcore:gcore@tcp(192.168.200.248:3306)/test?charset=utf8") + fmt.Println(conn, err) +} diff --git a/table2struct.go b/table2struct.go index cba7579..c66333c 100644 --- a/table2struct.go +++ b/table2struct.go @@ -4,14 +4,16 @@ import ( "database/sql" "errors" "fmt" - _ "github.com/go-sql-driver/mysql" "log" "os" "os/exec" + "path/filepath" "strings" + + _ "github.com/go-sql-driver/mysql" ) -//map for converting mysql type to golang types +// map for converting mysql type to golang types var typeForMysqlToGo = map[string]string{ "int": "int64", "integer": "int64", @@ -179,7 +181,13 @@ func (t *Table2Struct) Run() error { tableName = strings.ToUpper(tableName[0:1]) + tableName[1:] } depth := 1 - structContent += "type " + structName + " struct {\n" + + if t.config.SeperatFile { + structContent = "type " + structName + " struct {\n" + } else { + structContent += "type " + structName + " struct {\n" + } + for _, v := range item { //structContent += tab(depth) + v.ColumnName + " " + v.Type + " " + v.Json + "\n" // 字段注释 @@ -200,6 +208,15 @@ func (t *Table2Struct) Run() error { tab(depth), tableRealName) structContent += "}\n\n" } + // 开启单表单文件保存 文件名为表名 + if t.config.SeperatFile { + + saveResult(packageName, structContent, t, tableRealName) + } + } + + if t.config.SeperatFile { + return nil } // 如果有引入 time.Time, 则需要引入 time 包 @@ -214,6 +231,10 @@ func (t *Table2Struct) Run() error { if savePath == "" { savePath = "model.go" } + + if fi, e := os.Stat(savePath); e == nil && fi.IsDir() { + savePath = filepath.Join(savePath, "model.go") + } filePath := fmt.Sprintf("%s", savePath) f, err := os.Create(filePath) if err != nil { @@ -361,3 +382,33 @@ func (t *Table2Struct) camelCase(str string) string { func tab(depth int) string { return strings.Repeat("\t", depth) } + +func saveResult(packageName string, structContent string, t *Table2Struct, tableRealName string) error { + // 如果有引入 time.Time, 则需要引入 time 包 + var importContent string + if strings.Contains(structContent, "time.Time") { + importContent = "import \"time\"\n\n" + } + + // 写入文件struct + var savePath = t.savePath + // 是否指定保存路径 + + // if savePath == "" { + // savePath = tableRealName+".go" + // } + savePath = savePath + tableRealName + ".go" + filePath := fmt.Sprintf("%s", savePath) + f, err := os.Create(filePath) + if err != nil { + log.Println("Can not write file") + return err + } + defer f.Close() + + f.WriteString(packageName + importContent + structContent) + + cmd := exec.Command("gofmt", "-w", filePath) + cmd.Run() + return nil +} diff --git a/test/generate_test.go b/test/generate_test.go new file mode 100644 index 0000000..935ee3f --- /dev/null +++ b/test/generate_test.go @@ -0,0 +1,57 @@ +package test + +import ( + "fmt" + "testing" + "github.com/gohouse/converter" +) + +func TestGenerateTest(t *testing.T) { + // 初始化 + t2t := converter.NewTable2Struct() + // 个性化配置 + t2t.Config(&converter.T2tConfig{ + // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加 + RmTagIfUcFirsted: false, + // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转 + TagToLower: false, + // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 + UcFirstOnly: true, + //// 每个struct放入单独的文件,默认false,放入同一个文件 + SeperatFile: true, + // struct 转驼峰命名,默认false + StructNameToHump: true, + // json tag是否转为驼峰,默认为false,不转换 + JsonTagToHump: true, + }) + + + // 开始迁移转换 + err := t2t. + // 指定某个表,如果不指定,则默认全部表都迁移 + // Table("user"). + // 表前缀 + // Prefix("prefix_"). + // 是否添加json tag + + EnableJsonTag(true). + + + // 日期类型 + DateToTime(true). + + // 生成struct的包名(默认为空的话, 则取名为: package model) + PackageName("models"). + // tag字段的key值,默认是orm + TagKey("orm"). + // 是否添加结构体方法获取表名 + RealNameMethod("TableName"). + // 生成的结构体保存路径 + SavePath("E:\\golang\\rui_ji\\models\\"). + // 数据库dsn,这里可以使用 t2t.DB() 代替,参数为 *sql.DB 对象 + Dsn("root:1233211234567@tcp(localhost:3306)/reggie?charset=utf8"). + // 执行 + Run() + + fmt.Println(err) +} From 18416a3bb089ee35c668b387a059e8e532f3247b Mon Sep 17 00:00:00 2001 From: RQ <935299904@qq.com> Date: Wed, 23 Nov 2022 10:00:09 +0800 Subject: [PATCH 2/3] Optimization path --- table2struct.go | 11 ++++------- test/generate_test.go | 5 +++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/table2struct.go b/table2struct.go index c66333c..6ac1384 100644 --- a/table2struct.go +++ b/table2struct.go @@ -216,9 +216,10 @@ func (t *Table2Struct) Run() error { } if t.config.SeperatFile { + log.Println("gen model finish!!!") return nil } - + // 如果有引入 time.Time, 则需要引入 time 包 var importContent string if strings.Contains(structContent, "time.Time") { @@ -383,7 +384,7 @@ func tab(depth int) string { return strings.Repeat("\t", depth) } -func saveResult(packageName string, structContent string, t *Table2Struct, tableRealName string) error { +func saveResult(packageName string, structContent string, t *Table2Struct, fileName string) error { // 如果有引入 time.Time, 则需要引入 time 包 var importContent string if strings.Contains(structContent, "time.Time") { @@ -393,11 +394,7 @@ func saveResult(packageName string, structContent string, t *Table2Struct, table // 写入文件struct var savePath = t.savePath // 是否指定保存路径 - - // if savePath == "" { - // savePath = tableRealName+".go" - // } - savePath = savePath + tableRealName + ".go" + savePath = filepath.Join(savePath, fileName+".go") filePath := fmt.Sprintf("%s", savePath) f, err := os.Create(filePath) if err != nil { diff --git a/test/generate_test.go b/test/generate_test.go index 935ee3f..91bf526 100644 --- a/test/generate_test.go +++ b/test/generate_test.go @@ -18,7 +18,7 @@ func TestGenerateTest(t *testing.T) { // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 UcFirstOnly: true, //// 每个struct放入单独的文件,默认false,放入同一个文件 - SeperatFile: true, + SeperatFile: false, // struct 转驼峰命名,默认false StructNameToHump: true, // json tag是否转为驼峰,默认为false,不转换 @@ -47,7 +47,8 @@ func TestGenerateTest(t *testing.T) { // 是否添加结构体方法获取表名 RealNameMethod("TableName"). // 生成的结构体保存路径 - SavePath("E:\\golang\\rui_ji\\models\\"). + SavePath("E:\\golang\\rui_ji\\models"). + // SavePath("E:\\golang\\rui_ji\\models"). // 数据库dsn,这里可以使用 t2t.DB() 代替,参数为 *sql.DB 对象 Dsn("root:1233211234567@tcp(localhost:3306)/reggie?charset=utf8"). // 执行 From 4dc2b19248ad300ec47d94302991d957c39e82be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=99=B4?= Date: Tue, 23 May 2023 10:41:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++----- cmd/cli.go | 2 +- examples/t2t.go | 32 ++++++++++++++++++++++++++++++-- migration/artisan.go | 2 ++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2086587..548f65d 100644 --- a/README.md +++ b/README.md @@ -77,15 +77,15 @@ func main() { TagToLower: false, // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 UcFirstOnly: false, - //// 每个struct放入单独的文件,默认false,放入同一个文件(暂未提供) - //SeperatFile: false, + // 每个struct放入单独的文件,默认false,放入同一个文件,true 单文件 + SeperatFile: true, }) // 开始迁移转换 err := t2t. // 指定某个表,如果不指定,则默认全部表都迁移 - Table("user"). + // Table("user"). // 表前缀 - Prefix("prefix_"). + // Prefix("prefix_"). // 是否添加json tag EnableJsonTag(true). // 生成struct的包名(默认为空的话, 则取名为: package model) @@ -94,8 +94,9 @@ func main() { TagKey("orm"). // 是否添加结构体方法获取表名 RealNameMethod("TableName"). + // 生成的结构体保存路径 - SavePath("/Users/fizz/go/src/github.com/gohouse/gupiao/model/model.go"). + SavePath("/Users/fizz/go/src/github.com/gohouse/gupiao/model"). // 数据库dsn,这里可以使用 t2t.DB() 代替,参数为 *sql.DB 对象 Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). // 执行 diff --git a/cmd/cli.go b/cmd/cli.go index 60a1736..a3f63b1 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -51,7 +51,7 @@ func parser() { // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 UcFirstOnly: false, //// 每个struct放入单独的文件,默认false,放入同一个文件(暂未提供) - //SeperatFile: false, + SeperatFile: true, }) // 开始迁移转换 err := t2t. diff --git a/examples/t2t.go b/examples/t2t.go index 5205c01..e216a4d 100644 --- a/examples/t2t.go +++ b/examples/t2t.go @@ -6,11 +6,39 @@ import ( ) func main() { + // 初始化 t2t := converter.NewTable2Struct() - + // 个性化配置 + t2t.Config(&converter.T2tConfig{ + // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加 + RmTagIfUcFirsted: false, + // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转 + TagToLower: false, + // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换 + UcFirstOnly: false, + // 每个struct放入单独的文件,默认false,放入同一个文件,true 单文件 + SeperatFile: true, + }) + // 开始迁移转换 err := t2t. - SavePath("/home/go/project/model/model.go"). + // 指定某个表,如果不指定,则默认全部表都迁移 + Table("user"). + // 表前缀 + Prefix("prefix_"). + // 是否添加json tag + EnableJsonTag(true). + // 生成struct的包名(默认为空的话, 则取名为: package model) + PackageName("model"). + // tag字段的key值,默认是orm + TagKey("orm"). + // 是否添加结构体方法获取表名 + RealNameMethod("TableName"). + // 生成的结构体保存路径 + SavePath("/Users/fizz/go/src/github.com/gohouse/gupiao/model/"). + // 数据库dsn,这里可以使用 t2t.DB() 代替,参数为 *sql.DB 对象 Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). + // 执行 Run() + fmt.Println(err) } diff --git a/migration/artisan.go b/migration/artisan.go index 0c49c82..fba0026 100644 --- a/migration/artisan.go +++ b/migration/artisan.go @@ -1,5 +1,6 @@ package main +/* import ( "fmt" @@ -22,3 +23,4 @@ func main() { conn, err = gorose.Open("mysql", "gcore:gcore@tcp(192.168.200.248:3306)/test?charset=utf8") fmt.Println(conn, err) } +*/