1
0

vars.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package gen
  2. import (
  3. "strings"
  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. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  8. )
  9. func genVars(table Table, withCache, postgreSql bool) (string, error) {
  10. keys := make([]string, 0)
  11. keys = append(keys, table.PrimaryCacheKey.VarExpression)
  12. for _, v := range table.UniqueCacheKey {
  13. keys = append(keys, v.VarExpression)
  14. }
  15. camel := table.Name.ToCamel()
  16. text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars)
  17. if err != nil {
  18. return "", err
  19. }
  20. output, err := util.With("var").Parse(text).
  21. GoFmt(true).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(), postgreSql),
  27. "withCache": withCache,
  28. "postgreSql": postgreSql,
  29. "data": table,
  30. })
  31. if err != nil {
  32. return "", err
  33. }
  34. return output.String(), nil
  35. }