generate.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. Output string
  17. Cfg *config.Config
  18. }
  19. // Do executes model template and output the result into the specified file path
  20. func Do(ctx *Context) error {
  21. if ctx.Cfg == nil {
  22. return errors.New("missing config")
  23. }
  24. if err := generateTypes(ctx); err != nil {
  25. return err
  26. }
  27. if err := generateModel(ctx); err != nil {
  28. return err
  29. }
  30. if err := generateCustomModel(ctx); err != nil {
  31. return err
  32. }
  33. return generateError(ctx)
  34. }
  35. func generateModel(ctx *Context) error {
  36. for _, t := range ctx.Types {
  37. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_model_gen")
  38. if err != nil {
  39. return err
  40. }
  41. text, err := pathx.LoadTemplate(category, modelTemplateFile, template.ModelText)
  42. if err != nil {
  43. return err
  44. }
  45. output := filepath.Join(ctx.Output, fn+".go")
  46. if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{
  47. "Type": stringx.From(t).Title(),
  48. "lowerType": stringx.From(t).Untitle(),
  49. "Cache": ctx.Cache,
  50. }, output, true); err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }
  56. func generateCustomModel(ctx *Context) error {
  57. for _, t := range ctx.Types {
  58. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_model")
  59. if err != nil {
  60. return err
  61. }
  62. text, err := pathx.LoadTemplate(category, modelCustomTemplateFile, template.ModelCustomText)
  63. if err != nil {
  64. return err
  65. }
  66. output := filepath.Join(ctx.Output, fn+".go")
  67. err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{
  68. "Type": stringx.From(t).Title(),
  69. "lowerType": stringx.From(t).Untitle(),
  70. "Cache": ctx.Cache,
  71. }, output, false)
  72. if err != nil {
  73. return err
  74. }
  75. }
  76. return nil
  77. }
  78. func generateTypes(ctx *Context) error {
  79. for _, t := range ctx.Types {
  80. fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"types")
  81. if err != nil {
  82. return err
  83. }
  84. text, err := pathx.LoadTemplate(category, modelTypesTemplateFile, template.ModelTypesText)
  85. if err != nil {
  86. return err
  87. }
  88. output := filepath.Join(ctx.Output, fn+".go")
  89. if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{
  90. "Type": stringx.From(t).Title(),
  91. }, output, false);err!=nil{
  92. return err
  93. }
  94. }
  95. return nil
  96. }
  97. func generateError(ctx *Context) error {
  98. text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
  99. if err != nil {
  100. return err
  101. }
  102. output := filepath.Join(ctx.Output, "error.go")
  103. return util.With("error").Parse(text).GoFmt(true).SaveTo(ctx, output, false)
  104. }