2020-07-29 17:11:41 +08:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-01-18 10:36:38 +08:00
|
|
|
func genFields(table Table, fields []*parser.Field) (string, error) {
|
2020-08-19 10:41:19 +08:00
|
|
|
var list []string
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2020-07-29 17:11:41 +08:00
|
|
|
for _, field := range fields {
|
2022-01-18 10:36:38 +08:00
|
|
|
result, err := genField(table, field)
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-11-18 15:32:53 +08:00
|
|
|
|
2020-07-29 17:11:41 +08:00
|
|
|
list = append(list, result)
|
|
|
|
}
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2020-08-19 10:41:19 +08:00
|
|
|
return strings.Join(list, "\n"), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
|
|
|
|
2022-01-18 10:36:38 +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 {
|
2020-08-19 10:41:19 +08:00
|
|
|
return "", err
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2020-10-21 14:59:35 +08:00
|
|
|
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field)
|
2020-10-21 14:59:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:13:18 +08:00
|
|
|
output, err := util.With("types").
|
2020-10-21 14:59:35 +08:00
|
|
|
Parse(text).
|
2023-01-24 17:51:03 +08:00
|
|
|
Execute(map[string]any{
|
2022-04-11 10:11:40 +08:00
|
|
|
"name": util.SafeString(field.Name.ToCamel()),
|
2020-08-19 10:41:19 +08:00
|
|
|
"type": field.DataType,
|
|
|
|
"tag": tag,
|
|
|
|
"hasComment": field.Comment != "",
|
|
|
|
"comment": field.Comment,
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2020-08-19 10:41:19 +08:00
|
|
|
})
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-11-18 15:32:53 +08:00
|
|
|
|
2020-08-19 10:41:19 +08:00
|
|
|
return output.String(), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|