vars.go 1009 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package gen
  2. import (
  3. "strings"
  4. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  7. )
  8. func genVars(table Table, withCache bool) (string, error) {
  9. keys := make([]string, 0)
  10. for _, v := range table.CacheKey {
  11. keys = append(keys, v.VarExpression)
  12. }
  13. camel := table.Name.ToCamel()
  14. text, err := util.LoadTemplate(category, varTemplateFile, template.Vars)
  15. if err != nil {
  16. return "", err
  17. }
  18. output, err := util.With("var").
  19. Parse(text).
  20. GoFmt(true).
  21. Execute(map[string]interface{}{
  22. "lowerStartCamelObject": stringx.From(camel).Untitle(),
  23. "upperStartCamelObject": camel,
  24. "cacheKeys": strings.Join(keys, "\n"),
  25. "autoIncrement": table.PrimaryKey.AutoIncrement,
  26. "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
  27. "withCache": withCache,
  28. })
  29. if err != nil {
  30. return "", err
  31. }
  32. return output.String(), nil
  33. }