You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/model/sql/gen/field.go

54 lines
1.1 KiB
Go

4 years ago
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"
4 years ago
)
func genFields(table Table, fields []*parser.Field) (string, error) {
var list []string
4 years ago
for _, field := range fields {
result, err := genField(table, field)
4 years ago
if err != nil {
return "", err
}
4 years ago
list = append(list, result)
}
return strings.Join(list, "\n"), nil
4 years ago
}
func genField(table Table, field *parser.Field) (string, error) {
tag, err := genTag(table, field.NameOriginal)
4 years ago
if err != nil {
return "", err
4 years ago
}
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,
})
4 years ago
if err != nil {
return "", err
}
return output.String(), nil
4 years ago
}