field.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package gen
  2. import (
  3. "strings"
  4. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/parser"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/template"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  7. )
  8. func genFields(fields []*parser.Field) (string, error) {
  9. var list []string
  10. for _, field := range fields {
  11. result, err := genField(field)
  12. if err != nil {
  13. return "", err
  14. }
  15. list = append(list, result)
  16. }
  17. return strings.Join(list, "\n"), nil
  18. }
  19. func genField(field *parser.Field) (string, error) {
  20. tag, err := genTag(field.Name.Source())
  21. if err != nil {
  22. return "", err
  23. }
  24. text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field)
  25. if err != nil {
  26. return "", err
  27. }
  28. output, err := util.With("types").
  29. Parse(text).
  30. Execute(map[string]interface{}{
  31. "name": field.Name.ToCamel(),
  32. "type": field.DataType,
  33. "tag": tag,
  34. "hasComment": field.Comment != "",
  35. "comment": field.Comment,
  36. })
  37. if err != nil {
  38. return "", err
  39. }
  40. return output.String(), nil
  41. }