findone.go 1.8 KB

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