new.go 874 B

1234567891011121314151617181920212223242526272829303132333435
  1. package gen
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
  5. "github.com/zeromicro/go-zero/tools/goctl/util"
  6. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  7. )
  8. func genNew(table Table, withCache, postgreSql bool) (string, error) {
  9. text, err := pathx.LoadTemplate(category, modelNewTemplateFile, template.New)
  10. if err != nil {
  11. return "", err
  12. }
  13. t := fmt.Sprintf(`"%s"`, wrapWithRawString(table.Name.Source(), postgreSql))
  14. if postgreSql {
  15. t = "`" + fmt.Sprintf(`"%s"."%s"`, table.Db.Source(), table.Name.Source()) + "`"
  16. }
  17. output, err := util.With("new").
  18. Parse(text).
  19. Execute(map[string]any{
  20. "table": t,
  21. "withCache": withCache,
  22. "upperStartCamelObject": table.Name.ToCamel(),
  23. "data": table,
  24. })
  25. if err != nil {
  26. return "", err
  27. }
  28. return output.String(), nil
  29. }