generate.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package generate
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "github.com/zeromicro/go-zero/tools/goctl/config"
  6. "github.com/zeromicro/go-zero/tools/goctl/model/mongo/template"
  7. "github.com/zeromicro/go-zero/tools/goctl/util"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  11. )
  12. // Context defines the model generation data what they needs
  13. type Context struct {
  14. Types []string
  15. Cache bool
  16. Easy bool
  17. Output string
  18. Cfg *config.Config
  19. }
  20. // Do executes model template and output the result into the specified file path
  21. func Do(ctx *Context) error {
  22. if ctx.Cfg == nil {
  23. return errors.New("missing config")
  24. }
  25. if err := generateTypes(ctx); err != nil {
  26. return err
  27. }
  28. if err := generateModel(ctx); err != nil {
  29. return err
  30. }
  31. if err := generateCustomModel(ctx); err != nil {
  32. return err
  33. }
  34. return generateError(ctx)
  35. }
  36. func generateModel(ctx *Context) error {
  37. for _, t := range ctx.Types {
  38. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_model_gen")
  39. if err != nil {
  40. return err
  41. }
  42. text, err := pathx.LoadTemplate(category, modelTemplateFile, template.ModelText)
  43. if err != nil {
  44. return err
  45. }
  46. output := filepath.Join(ctx.Output, fn+".go")
  47. if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]any{
  48. "Type": stringx.From(t).Title(),
  49. "lowerType": stringx.From(t).Untitle(),
  50. "Cache": ctx.Cache,
  51. }, output, true); err != nil {
  52. return err
  53. }
  54. }
  55. return nil
  56. }
  57. func generateCustomModel(ctx *Context) error {
  58. for _, t := range ctx.Types {
  59. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_model")
  60. if err != nil {
  61. return err
  62. }
  63. text, err := pathx.LoadTemplate(category, modelCustomTemplateFile, template.ModelCustomText)
  64. if err != nil {
  65. return err
  66. }
  67. output := filepath.Join(ctx.Output, fn+".go")
  68. err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]any{
  69. "Type": stringx.From(t).Title(),
  70. "lowerType": stringx.From(t).Untitle(),
  71. "snakeType": stringx.From(t).ToSnake(),
  72. "Cache": ctx.Cache,
  73. "Easy": ctx.Easy,
  74. }, output, false)
  75. if err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }
  81. func generateTypes(ctx *Context) error {
  82. for _, t := range ctx.Types {
  83. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_types")
  84. if err != nil {
  85. return err
  86. }
  87. text, err := pathx.LoadTemplate(category, modelTypesTemplateFile, template.ModelTypesText)
  88. if err != nil {
  89. return err
  90. }
  91. output := filepath.Join(ctx.Output, fn+".go")
  92. if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]any{
  93. "Type": stringx.From(t).Title(),
  94. }, output, false); err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }
  100. func generateError(ctx *Context) error {
  101. text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
  102. if err != nil {
  103. return err
  104. }
  105. output := filepath.Join(ctx.Output, "error.go")
  106. return util.With("error").Parse(text).GoFmt(true).SaveTo(ctx, output, false)
  107. }