findonebyfield.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package gen
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  6. "github.com/tal-tech/go-zero/tools/goctl/util"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  8. )
  9. func genFindOneByField(table Table, withCache bool) (string, error) {
  10. t := util.With("findOneByField").Parse(template.FindOneByField)
  11. var list []string
  12. camelTableName := table.Name.ToCamel()
  13. for _, field := range table.Fields {
  14. if field.IsPrimaryKey || !field.IsUniqueKey {
  15. continue
  16. }
  17. camelFieldName := field.Name.ToCamel()
  18. output, err := t.Execute(map[string]interface{}{
  19. "upperStartCamelObject": camelTableName,
  20. "upperField": camelFieldName,
  21. "in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).UnTitle(), field.DataType),
  22. "withCache": withCache,
  23. "cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
  24. "cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
  25. "primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
  26. "lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
  27. "lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
  28. "upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
  29. "originalField": field.Name.Source(),
  30. "originalPrimaryField": table.PrimaryKey.Name.Source(),
  31. })
  32. if err != nil {
  33. return "", err
  34. }
  35. list = append(list, output.String())
  36. }
  37. return strings.Join(list, "\n"), nil
  38. }