gendao_do.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gendao
  7. import (
  8. "context"
  9. "fmt"
  10. "strings"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/os/gfile"
  13. "github.com/gogf/gf/v2/text/gregex"
  14. "github.com/gogf/gf/v2/text/gstr"
  15. "github.com/gogf/gf/cmd/gf/v2/internal/consts"
  16. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  17. "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
  18. )
  19. func generateDo(ctx context.Context, in CGenDaoInternalInput) {
  20. var dirPathDo = gfile.Join(in.Path, in.DoPath)
  21. if in.Clear {
  22. doClear(ctx, dirPathDo, false)
  23. }
  24. in.NoJsonTag = true
  25. in.DescriptionTag = false
  26. in.NoModelComment = false
  27. // Model content.
  28. for i, tableName := range in.TableNames {
  29. fieldMap, err := in.DB.TableFields(ctx, tableName)
  30. if err != nil {
  31. mlog.Fatalf("fetching tables fields failed for table '%s':\n%v", tableName, err)
  32. }
  33. var (
  34. newTableName = in.NewTableNames[i]
  35. doFilePath = gfile.Join(dirPathDo, gstr.CaseSnake(newTableName)+".go")
  36. structDefinition, _ = generateStructDefinition(ctx, generateStructDefinitionInput{
  37. CGenDaoInternalInput: in,
  38. TableName: tableName,
  39. StructName: gstr.CaseCamel(newTableName),
  40. FieldMap: fieldMap,
  41. IsDo: true,
  42. })
  43. )
  44. // replace all types to interface{}.
  45. structDefinition, _ = gregex.ReplaceStringFuncMatch(
  46. "([A-Z]\\w*?)\\s+([\\w\\*\\.]+?)\\s+(//)",
  47. structDefinition,
  48. func(match []string) string {
  49. // If the type is already a pointer/slice/map, it does nothing.
  50. if !gstr.HasPrefix(match[2], "*") && !gstr.HasPrefix(match[2], "[]") && !gstr.HasPrefix(match[2], "map") {
  51. return fmt.Sprintf(`%s interface{} %s`, match[1], match[3])
  52. }
  53. return match[0]
  54. },
  55. )
  56. modelContent := generateDoContent(
  57. ctx,
  58. in,
  59. tableName,
  60. gstr.CaseCamel(newTableName),
  61. structDefinition,
  62. )
  63. err = gfile.PutContents(doFilePath, strings.TrimSpace(modelContent))
  64. if err != nil {
  65. mlog.Fatalf(`writing content to "%s" failed: %v`, doFilePath, err)
  66. } else {
  67. utils.GoFmt(doFilePath)
  68. mlog.Print("generated:", doFilePath)
  69. }
  70. }
  71. }
  72. func generateDoContent(
  73. ctx context.Context, in CGenDaoInternalInput, tableName, tableNameCamelCase, structDefine string,
  74. ) string {
  75. doContent := gstr.ReplaceByMap(
  76. getTemplateFromPathOrDefault(in.TplDaoDoPath, consts.TemplateGenDaoDoContent),
  77. g.MapStrStr{
  78. tplVarTableName: tableName,
  79. tplVarPackageImports: getImportPartContent(ctx, structDefine, true, nil),
  80. tplVarTableNameCamelCase: tableNameCamelCase,
  81. tplVarStructDefine: structDefine,
  82. },
  83. )
  84. doContent = replaceDefaultVar(in, doContent)
  85. return doContent
  86. }