Skip to content

Commit

Permalink
add model some document
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabbbit committed Jan 2, 2021
1 parent 908b7b3 commit b39467c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 125 deletions.
56 changes: 43 additions & 13 deletions cli/codegen/po_model_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Field struct {
LowCamelName string
DbName string
MySQLTypeName string
Comment string
}

type Idx struct {
Expand All @@ -31,13 +32,13 @@ type Idx struct {
type PoModelStructInfo struct {
ddl string

Imports []string
PackageName string
ModelName string
TableName string
Fields []*Field
FieldsMap map[string]*Field
Idx []*Idx
TableComment string
PackageName string
ModelName string
TableName string
Fields []*Field
FieldsMap map[string]*Field
Idx []*Idx
}

func (m *PoModelStructInfo) GenCode(file io.Writer) error {
Expand All @@ -52,11 +53,6 @@ func (m *PoModelStructInfo) GenCode(file io.Writer) error {
return err
}

// gen import
if t, err = template.New("import").Parse(tpls.ModelImportTpl); err != nil {
return err
}

// gen define code
if t, err = template.New("define").Parse(tpls.ModelDefineTpl); err != nil {
return err
Expand All @@ -81,13 +77,15 @@ func NewPoModelStructInfo(tableName string, datasource string, packageName strin
panic(err)
}
m := &PoModelStructInfo{
Imports: []string{"time"},
FieldsMap: map[string]*Field{},
TableName: tableName,
ddl: result.DDL,
ModelName: strcase.ToCamel(tableName + "Model"),
PackageName: packageName,
}
if err = m.initTableComment(); err != nil {
panic(err)
}
if err = m.initFields(); err != nil {
panic(err)
}
Expand Down Expand Up @@ -147,6 +145,31 @@ func (m *PoModelStructInfo) initIndexs() error {
}
}

func (m *PoModelStructInfo) initTableComment() error {
buffer := new(bytes.Buffer)
buffer.WriteString(m.ddl)
ddlReader := bufio.NewReader(buffer)
var lineNum int
for {
var err error
lineStr, _, err := ddlReader.ReadLine()
lineNum++
if err != nil {
if err == io.EOF {
break
}
return err
}
if strings.Contains(string(lineStr), ") ENGINE=") {
// end line
keys := strings.Split(string(lineStr), "'")
m.TableComment = keys[len(keys)-2]
return nil
}
}
return nil
}

// 初始化 fields 字段
func (m *PoModelStructInfo) initFields() error {
buffer := new(bytes.Buffer)
Expand Down Expand Up @@ -176,6 +199,13 @@ func (m *PoModelStructInfo) initFields() error {
f.MySQLTypeName = strings.Split(keys[1], "(")[0] // 类型名
f.LowCamelName = strcase.ToLowerCamel(f.DbName)
f.Name = strcase.ToCamel(f.DbName)

// 找到备注
for i, key := range keys {
if key == "COMMENT" {
f.Comment = strings.ReplaceAll(strings.ReplaceAll(keys[i+1], "'", ""), ",", "")
}
}
if f.TypeName, err = getGolangTypeWithMysqlType(f.MySQLTypeName); err != nil {
return err
}
Expand Down
91 changes: 0 additions & 91 deletions cli/repo_mode.tpl.go

This file was deleted.

9 changes: 6 additions & 3 deletions cli/tpls/po_model.tpl.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package tpls

var ModelPackageTpl = `package {{.PackageName}}`
var ModelImportTpl = `import "time"`
var ModelPackageTpl = `// Code generated by ZelusCtl. DO NOT EDIT.
package {{.PackageName}}`

var ModelDefineTpl = `
// {{.ModelName}} {{.TableComment}}
type {{.ModelName}} struct { {{range .Fields}}
{{.Name}} {{.TypeName}} ` + "`gorm:\"{{.DbName}}\"`" + `{{end}}
{{.Name}} {{.TypeName}} ` + "`gorm:\"{{.DbName}}\"`" + ` // {{.Comment}}` + `{{end}}
}`
18 changes: 0 additions & 18 deletions cli/zelusctl_test.go

This file was deleted.

15 changes: 15 additions & 0 deletions example/internal/data/users_model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b39467c

Please sign in to comment.