From f74ba73d29d0bf43880f1af7be57de5d55eeb01c Mon Sep 17 00:00:00 2001 From: huangzj Date: Tue, 26 May 2020 09:12:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(ReverseEngine)=EF=BC=9A=E7=BB=99=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9A=84=E6=96=87=E4=BB=B6=E6=B7=BB=E5=8A=A0=E8=A1=A8?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=BD=93=E8=A1=A8=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=B8=8D=E4=B8=BA=E7=A9=BA=E7=9A=84=E6=97=B6=E5=80=99=E4=BC=9A?= =?UTF-8?q?=E5=9C=A8import=E4=B8=8B=E9=9D=A2=E7=94=9F=E6=88=90=E8=AF=A5?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- imp/ContentGenerate.go | 2 +- reverse/Engine.go | 6 +++--- support/FileContentSupport.go | 16 ++++++++++++---- support/FileContentSupport2.go | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/imp/ContentGenerate.go b/imp/ContentGenerate.go index 57e2029..6108000 100644 --- a/imp/ContentGenerate.go +++ b/imp/ContentGenerate.go @@ -6,5 +6,5 @@ type ContentGenerate interface { /* 内容制造接口 */ - GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string) string + GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string, string string) string } diff --git a/reverse/Engine.go b/reverse/Engine.go index dc5669f..793a1bf 100644 --- a/reverse/Engine.go +++ b/reverse/Engine.go @@ -154,9 +154,9 @@ func dbReverse(path string, tableName string, dbName string, db *sql.DB) { if nil == cms { log.Println("没有查到数据库字段信息略过") } - fms, hasTime := buildFieldMessage(cms) //创建字段名和类型的映射 - w := bufio.NewWriter(file) //进行文件的操作 - content := cg.GenerateFileContent(path, tableName, fms, hasTime, tagKey) //拼go文件 + fms, hasTime := buildFieldMessage(cms) //创建字段名和类型的映射 + w := bufio.NewWriter(file) //进行文件的操作 + content := cg.GenerateFileContent(path, tableName, fms, hasTime, tagKey, tableComment) //拼go文件 _, err := w.WriteString(content) checkError(err, "写入出错了:") fErr, cErr := w.Flush(), file.Close() diff --git a/support/FileContentSupport.go b/support/FileContentSupport.go index 5bc51d0..d328551 100644 --- a/support/FileContentSupport.go +++ b/support/FileContentSupport.go @@ -8,13 +8,14 @@ import ( type FileContentSupport struct{} -func (FileContentSupport) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string) string { +func (FileContentSupport) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string, tableComment string) string { var build strings.Builder pName := splitLast(filePath, static.Splice) - writePackage(&build, pName) //写文件的引用包 - writeImportIfTime(&build, hasTime) //如果有time.Time类型的需要写入import - buildStructHead(&build, fileName) //写入结构体头 + writePackage(&build, pName) //写文件的引用包 + writeImportIfTime(&build, hasTime) //如果有time.Time类型的需要写入import + writeTableComment(&build, tableComment) //写入表注释 + buildStructHead(&build, fileName) //写入结构体头 //对于没有字段的返回空定义的文件 if len(fms) == 0 { @@ -99,3 +100,10 @@ func splitLast(filePath string, split string) string { ss := strings.Split(filePath, split) return ss[len(ss)-1] } + +func writeTableComment(builder *strings.Builder, s string) { + if s != "" { + builder.WriteString("//" + s) + builder.WriteString("\n") + } +} diff --git a/support/FileContentSupport2.go b/support/FileContentSupport2.go index 60f041d..edb4aeb 100644 --- a/support/FileContentSupport2.go +++ b/support/FileContentSupport2.go @@ -7,7 +7,7 @@ import ( type FileContentSUpport2 struct{} -func (FileContentSUpport2) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string) string { +func (FileContentSUpport2) GenerateFileContent(filePath string, fileName string, fms []entity.FieldMessage, hasTime bool, tagKey string, string string) string { fmt.Println("12321321") return " " }