mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-25 02:08:44 +08:00
24fb29a356
* change column to read from information_schema * reactor generate mode from datasource * reactor generate mode from datasource * add primary key check logic * resolve rebase conflicts * add naming style * add filename test case * resolve rebase conflicts * reactor test * add test case * change shell script to makefile * update rpc new * update gen_test.go * format code * format code * update test * generates alias
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package gen
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
)
|
|
|
|
func genFields(fields []parser.Field) (string, error) {
|
|
var list []string
|
|
for _, field := range fields {
|
|
result, err := genField(field)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
list = append(list, result)
|
|
}
|
|
return strings.Join(list, "\n"), nil
|
|
}
|
|
|
|
func genField(field parser.Field) (string, error) {
|
|
tag, err := genTag(field.Name.Source())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
output, err := util.With("types").
|
|
Parse(text).
|
|
Execute(map[string]interface{}{
|
|
"name": field.Name.ToCamel(),
|
|
"type": field.DataType,
|
|
"tag": tag,
|
|
"hasComment": field.Comment != "",
|
|
"comment": field.Comment,
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return output.String(), nil
|
|
}
|