feat(ReverseEngine):给生成的文件添加表注释,当表注释不为空的时候会在import下面生成该注释信息

This commit is contained in:
huangzj
2020-05-26 09:12:17 +08:00
parent e9502e1ac8
commit f74ba73d29
4 changed files with 17 additions and 9 deletions
+1 -1
View File
@@ -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
} }
+3 -3
View File
@@ -154,9 +154,9 @@ func dbReverse(path string, tableName string, dbName string, db *sql.DB) {
if nil == cms { if nil == cms {
log.Println("没有查到数据库字段信息略过") log.Println("没有查到数据库字段信息略过")
} }
fms, hasTime := buildFieldMessage(cms) //创建字段名和类型的映射 fms, hasTime := buildFieldMessage(cms) //创建字段名和类型的映射
w := bufio.NewWriter(file) //进行文件的操作 w := bufio.NewWriter(file) //进行文件的操作
content := cg.GenerateFileContent(path, tableName, fms, hasTime, tagKey) //拼go文件 content := cg.GenerateFileContent(path, tableName, fms, hasTime, tagKey, tableComment) //拼go文件
_, err := w.WriteString(content) _, err := w.WriteString(content)
checkError(err, "写入出错了:") checkError(err, "写入出错了:")
fErr, cErr := w.Flush(), file.Close() fErr, cErr := w.Flush(), file.Close()
+12 -4
View File
@@ -8,13 +8,14 @@ import (
type FileContentSupport struct{} 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 var build strings.Builder
pName := splitLast(filePath, static.Splice) pName := splitLast(filePath, static.Splice)
writePackage(&build, pName) //写文件的引用包 writePackage(&build, pName) //写文件的引用包
writeImportIfTime(&build, hasTime) //如果有time.Time类型的需要写入import writeImportIfTime(&build, hasTime) //如果有time.Time类型的需要写入import
buildStructHead(&build, fileName) //写入结构体头 writeTableComment(&build, tableComment) //写入表注释
buildStructHead(&build, fileName) //写入结构体头
//对于没有字段的返回空定义的文件 //对于没有字段的返回空定义的文件
if len(fms) == 0 { if len(fms) == 0 {
@@ -99,3 +100,10 @@ func splitLast(filePath string, split string) string {
ss := strings.Split(filePath, split) ss := strings.Split(filePath, split)
return ss[len(ss)-1] return ss[len(ss)-1]
} }
func writeTableComment(builder *strings.Builder, s string) {
if s != "" {
builder.WriteString("//" + s)
builder.WriteString("\n")
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ import (
type FileContentSUpport2 struct{} 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") fmt.Println("12321321")
return " " return " "
} }