genctrl_generate_interface.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 genctrl
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/cmd/gf/v2/internal/consts"
  10. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  11. "github.com/gogf/gf/v2/container/gmap"
  12. "github.com/gogf/gf/v2/container/gset"
  13. "github.com/gogf/gf/v2/frame/g"
  14. "github.com/gogf/gf/v2/os/gfile"
  15. "github.com/gogf/gf/v2/text/gstr"
  16. "github.com/gogf/gf/v2/util/gconv"
  17. )
  18. type apiInterfaceGenerator struct{}
  19. func newApiInterfaceGenerator() *apiInterfaceGenerator {
  20. return &apiInterfaceGenerator{}
  21. }
  22. func (c *apiInterfaceGenerator) Generate(apiModuleFolderPath string, apiModuleApiItems []apiItem) (err error) {
  23. if len(apiModuleApiItems) == 0 {
  24. return nil
  25. }
  26. var firstApiItem = apiModuleApiItems[0]
  27. if err = c.doGenerate(apiModuleFolderPath, firstApiItem.Module, apiModuleApiItems); err != nil {
  28. return
  29. }
  30. return
  31. }
  32. func (c *apiInterfaceGenerator) doGenerate(apiModuleFolderPath string, module string, items []apiItem) (err error) {
  33. var (
  34. moduleFilePath = gfile.Join(apiModuleFolderPath, fmt.Sprintf(`%s.go`, module))
  35. importPathMap = gmap.NewListMap()
  36. importPaths []string
  37. )
  38. // all import paths.
  39. importPathMap.Set("\t"+`"context"`, 1)
  40. importPathMap.Set("\t"+``, 1)
  41. for _, item := range items {
  42. importPathMap.Set(fmt.Sprintf("\t"+`"%s"`, item.Import), 1)
  43. }
  44. importPaths = gconv.Strings(importPathMap.Keys())
  45. // interface definitions.
  46. var (
  47. doneApiItemSet = gset.NewStrSet()
  48. interfaceDefinition string
  49. interfaceContent = gstr.TrimLeft(gstr.ReplaceByMap(consts.TemplateGenCtrlApiInterface, g.MapStrStr{
  50. "{Module}": module,
  51. "{ImportPaths}": gstr.Join(importPaths, "\n"),
  52. }))
  53. )
  54. for _, item := range items {
  55. if doneApiItemSet.Contains(item.String()) {
  56. continue
  57. }
  58. // retrieve all api items of the same module.
  59. subItems := c.getSubItemsByModuleAndVersion(items, item.Module, item.Version)
  60. var (
  61. method string
  62. methods = make([]string, 0)
  63. interfaceName = fmt.Sprintf(`I%s%s`, gstr.CaseCamel(item.Module), gstr.UcFirst(item.Version))
  64. )
  65. for _, subItem := range subItems {
  66. method = fmt.Sprintf(
  67. "\t%s(ctx context.Context, req *%s.%sReq) (res *%s.%sRes, err error)",
  68. subItem.MethodName, subItem.Version, subItem.MethodName, subItem.Version, subItem.MethodName,
  69. )
  70. methods = append(methods, method)
  71. doneApiItemSet.Add(subItem.String())
  72. }
  73. interfaceDefinition += fmt.Sprintf("type %s interface {", interfaceName)
  74. interfaceDefinition += "\n"
  75. interfaceDefinition += gstr.Join(methods, "\n")
  76. interfaceDefinition += "\n"
  77. interfaceDefinition += fmt.Sprintf("}")
  78. interfaceDefinition += "\n\n"
  79. }
  80. interfaceContent = gstr.TrimLeft(gstr.ReplaceByMap(interfaceContent, g.MapStrStr{
  81. "{Interfaces}": interfaceDefinition,
  82. }))
  83. err = gfile.PutContents(moduleFilePath, interfaceContent)
  84. mlog.Printf(`generated: %s`, moduleFilePath)
  85. return
  86. }
  87. func (c *apiInterfaceGenerator) getSubItemsByModule(items []apiItem, module string) (subItems []apiItem) {
  88. for _, item := range items {
  89. if item.Module == module {
  90. subItems = append(subItems, item)
  91. }
  92. }
  93. return
  94. }
  95. func (c *apiInterfaceGenerator) getSubItemsByModuleAndVersion(items []apiItem, module, version string) (subItems []apiItem) {
  96. for _, item := range items {
  97. if item.Module == module && item.Version == version {
  98. subItems = append(subItems, item)
  99. }
  100. }
  101. return
  102. }