vars.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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, postgreSql 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(), postgreSql),
  26. "withCache": withCache,
  27. "postgreSql": postgreSql,
  28. })
  29. if err != nil {
  30. return "", err
  31. }
  32. return output.String(), nil
  33. }