genservice_calculate.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 genservice
  7. import (
  8. "fmt"
  9. "go/parser"
  10. "go/token"
  11. "github.com/gogf/gf/v2/container/garray"
  12. "github.com/gogf/gf/v2/text/gregex"
  13. "github.com/gogf/gf/v2/text/gstr"
  14. )
  15. type packageItem struct {
  16. Alias string
  17. Path string
  18. RawImport string
  19. }
  20. func (c CGenService) calculateImportedPackages(fileContent string) (packages []packageItem, err error) {
  21. f, err := parser.ParseFile(token.NewFileSet(), "", fileContent, parser.ImportsOnly)
  22. if err != nil {
  23. return nil, err
  24. }
  25. packages = make([]packageItem, 0)
  26. for _, s := range f.Imports {
  27. if s.Path != nil {
  28. if s.Name != nil {
  29. // If it has alias, and it is not `_`.
  30. if pkgAlias := s.Name.String(); pkgAlias != "_" {
  31. packages = append(packages, packageItem{
  32. Alias: pkgAlias,
  33. Path: s.Path.Value,
  34. RawImport: pkgAlias + " " + s.Path.Value,
  35. })
  36. }
  37. } else {
  38. // no alias
  39. packages = append(packages, packageItem{
  40. Alias: "",
  41. Path: s.Path.Value,
  42. RawImport: s.Path.Value,
  43. })
  44. }
  45. }
  46. }
  47. return packages, nil
  48. }
  49. func (c CGenService) calculateCodeCommented(in CGenServiceInput, fileContent string, srcCodeCommentedMap map[string]string) error {
  50. matches, err := gregex.MatchAllString(`((((//.*)|(/\*[\s\S]*?\*/))\s)+)func \((.+?)\) ([\s\S]+?) {`, fileContent)
  51. if err != nil {
  52. return err
  53. }
  54. for _, match := range matches {
  55. var (
  56. structName string
  57. structMatch []string
  58. funcReceiver = gstr.Trim(match[1+5])
  59. receiverArray = gstr.SplitAndTrim(funcReceiver, " ")
  60. functionHead = gstr.Trim(gstr.Replace(match[2+5], "\n", ""))
  61. commentedInfo = ""
  62. )
  63. if len(receiverArray) > 1 {
  64. structName = receiverArray[1]
  65. } else if len(receiverArray) == 1 {
  66. structName = receiverArray[0]
  67. }
  68. structName = gstr.Trim(structName, "*")
  69. // Case of:
  70. // Xxx(\n ctx context.Context, req *v1.XxxReq,\n) -> Xxx(ctx context.Context, req *v1.XxxReq)
  71. functionHead = gstr.Replace(functionHead, `,)`, `)`)
  72. functionHead, _ = gregex.ReplaceString(`\(\s+`, `(`, functionHead)
  73. functionHead, _ = gregex.ReplaceString(`\s{2,}`, ` `, functionHead)
  74. if !gstr.IsLetterUpper(functionHead[0]) {
  75. continue
  76. }
  77. // Match and pick the struct name from receiver.
  78. if structMatch, err = gregex.MatchString(in.StPattern, structName); err != nil {
  79. return err
  80. }
  81. if len(structMatch) < 1 {
  82. continue
  83. }
  84. structName = gstr.CaseCamel(structMatch[1])
  85. commentedInfo = match[1]
  86. if len(commentedInfo) > 0 {
  87. srcCodeCommentedMap[fmt.Sprintf("%s-%s", structName, functionHead)] = commentedInfo
  88. }
  89. }
  90. return nil
  91. }
  92. func (c CGenService) calculateInterfaceFunctions(
  93. in CGenServiceInput, fileContent string, srcPkgInterfaceMap map[string]*garray.StrArray,
  94. ) (err error) {
  95. var (
  96. ok bool
  97. matches [][]string
  98. srcPkgInterfaceFuncArray *garray.StrArray
  99. )
  100. // calculate struct name and its functions according function definitions.
  101. matches, err = gregex.MatchAllString(`func \((.+?)\) ([\s\S]+?) {`, fileContent)
  102. if err != nil {
  103. return err
  104. }
  105. for _, match := range matches {
  106. var (
  107. structName string
  108. structMatch []string
  109. funcReceiver = gstr.Trim(match[1])
  110. receiverArray = gstr.SplitAndTrim(funcReceiver, " ")
  111. functionHead = gstr.Trim(gstr.Replace(match[2], "\n", ""))
  112. )
  113. if len(receiverArray) > 1 {
  114. structName = receiverArray[1]
  115. } else if len(receiverArray) == 1 {
  116. structName = receiverArray[0]
  117. }
  118. structName = gstr.Trim(structName, "*")
  119. // Case of:
  120. // Xxx(\n ctx context.Context, req *v1.XxxReq,\n) -> Xxx(ctx context.Context, req *v1.XxxReq)
  121. functionHead = gstr.Replace(functionHead, `,)`, `)`)
  122. functionHead, _ = gregex.ReplaceString(`\(\s+`, `(`, functionHead)
  123. functionHead, _ = gregex.ReplaceString(`\s{2,}`, ` `, functionHead)
  124. if !gstr.IsLetterUpper(functionHead[0]) {
  125. continue
  126. }
  127. // Match and pick the struct name from receiver.
  128. if structMatch, err = gregex.MatchString(in.StPattern, structName); err != nil {
  129. return err
  130. }
  131. if len(structMatch) < 1 {
  132. continue
  133. }
  134. structName = gstr.CaseCamel(structMatch[1])
  135. if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
  136. srcPkgInterfaceMap[structName] = garray.NewStrArray()
  137. srcPkgInterfaceFuncArray = srcPkgInterfaceMap[structName]
  138. }
  139. srcPkgInterfaceFuncArray.Append(functionHead)
  140. }
  141. // calculate struct name according type definitions.
  142. matches, err = gregex.MatchAllString(`type (.+) struct\s*{`, fileContent)
  143. if err != nil {
  144. return err
  145. }
  146. for _, match := range matches {
  147. var (
  148. structName string
  149. structMatch []string
  150. )
  151. if structMatch, err = gregex.MatchString(in.StPattern, match[1]); err != nil {
  152. return err
  153. }
  154. if len(structMatch) < 1 {
  155. continue
  156. }
  157. structName = gstr.CaseCamel(structMatch[1])
  158. if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
  159. srcPkgInterfaceMap[structName] = garray.NewStrArray()
  160. }
  161. }
  162. return nil
  163. }