vars.go 1.0 KB

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