findone.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package gen
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  4. "github.com/tal-tech/go-zero/tools/goctl/util"
  5. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  6. )
  7. func genFindOne(table Table, withCache bool) (string, string, error) {
  8. camel := table.Name.ToCamel()
  9. text, err := util.LoadTemplate(category, findOneTemplateFile, template.FindOne)
  10. if err != nil {
  11. return "", "", err
  12. }
  13. output, err := util.With("findOne").
  14. Parse(text).
  15. Execute(map[string]interface{}{
  16. "withCache": withCache,
  17. "upperStartCamelObject": camel,
  18. "lowerStartCamelObject": stringx.From(camel).Untitle(),
  19. "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
  20. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  21. "dataType": table.PrimaryKey.DataType,
  22. "cacheKey": table.PrimaryCacheKey.KeyExpression,
  23. "cacheKeyVariable": table.PrimaryCacheKey.KeyLeft,
  24. })
  25. if err != nil {
  26. return "", "", err
  27. }
  28. text, err = util.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
  29. if err != nil {
  30. return "", "", err
  31. }
  32. findOneMethod, err := util.With("findOneMethod").
  33. Parse(text).
  34. Execute(map[string]interface{}{
  35. "upperStartCamelObject": camel,
  36. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  37. "dataType": table.PrimaryKey.DataType,
  38. })
  39. if err != nil {
  40. return "", "", err
  41. }
  42. return output.String(), findOneMethod.String(), nil
  43. }