go-zero/tools/goctl/model/sql/gen/field.go

54 lines
1.1 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gen
import (
"strings"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/parser"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
2020-07-29 17:11:41 +08:00
)
func genFields(table Table, fields []*parser.Field) (string, error) {
var list []string
2021-01-14 13:24:24 +08:00
2020-07-29 17:11:41 +08:00
for _, field := range fields {
result, err := genField(table, field)
2020-07-29 17:11:41 +08:00
if err != nil {
return "", err
}
2020-07-29 17:11:41 +08:00
list = append(list, result)
}
2021-01-14 13:24:24 +08:00
return strings.Join(list, "\n"), nil
2020-07-29 17:11:41 +08:00
}
func genField(table Table, field *parser.Field) (string, error) {
tag, err := genTag(table, field.NameOriginal)
2020-07-29 17:11:41 +08:00
if err != nil {
return "", err
2020-07-29 17:11:41 +08:00
}
text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field)
if err != nil {
return "", err
}
output, err := util.With("types").
Parse(text).
Execute(map[string]any{
"name": util.SafeString(field.Name.ToCamel()),
"type": field.DataType,
"tag": tag,
"hasComment": field.Comment != "",
"comment": field.Comment,
"data": table,
})
2020-07-29 17:11:41 +08:00
if err != nil {
return "", err
}
return output.String(), nil
2020-07-29 17:11:41 +08:00
}