format.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package format
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "go/format"
  7. "go/scanner"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "github.com/spf13/cobra"
  13. "github.com/zeromicro/go-zero/core/errorx"
  14. "github.com/zeromicro/go-zero/tools/goctl/api/parser"
  15. "github.com/zeromicro/go-zero/tools/goctl/api/util"
  16. "github.com/zeromicro/go-zero/tools/goctl/pkg/env"
  17. apiF "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/format"
  18. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  19. )
  20. const (
  21. leftParenthesis = "("
  22. rightParenthesis = ")"
  23. leftBrace = "{"
  24. rightBrace = "}"
  25. )
  26. var (
  27. // VarBoolUseStdin describes whether to use stdin or not.
  28. VarBoolUseStdin bool
  29. // VarBoolSkipCheckDeclare describes whether to skip.
  30. VarBoolSkipCheckDeclare bool
  31. // VarStringDir describes the directory.
  32. VarStringDir string
  33. // VarBoolIgnore describes whether to ignore.
  34. VarBoolIgnore bool
  35. )
  36. // GoFormatApi format api file
  37. func GoFormatApi(_ *cobra.Command, _ []string) error {
  38. var be errorx.BatchError
  39. if VarBoolUseStdin {
  40. if err := apiFormatReader(os.Stdin, VarStringDir, VarBoolSkipCheckDeclare); err != nil {
  41. be.Add(err)
  42. }
  43. } else {
  44. if len(VarStringDir) == 0 {
  45. return errors.New("missing -dir")
  46. }
  47. _, err := os.Lstat(VarStringDir)
  48. if err != nil {
  49. return errors.New(VarStringDir + ": No such file or directory")
  50. }
  51. err = filepath.Walk(VarStringDir, func(path string, fi os.FileInfo, errBack error) (err error) {
  52. if strings.HasSuffix(path, ".api") {
  53. if err := ApiFormatByPath(path, VarBoolSkipCheckDeclare); err != nil {
  54. be.Add(util.WrapErr(err, fi.Name()))
  55. }
  56. }
  57. return nil
  58. })
  59. be.Add(err)
  60. }
  61. if be.NotNil() {
  62. scanner.PrintError(os.Stderr, be.Err())
  63. os.Exit(1)
  64. }
  65. return be.Err()
  66. }
  67. // apiFormatReader
  68. // filename is needed when there are `import` literals.
  69. func apiFormatReader(reader io.Reader, filename string, skipCheckDeclare bool) error {
  70. data, err := io.ReadAll(reader)
  71. if err != nil {
  72. return err
  73. }
  74. result, err := apiFormat(string(data), skipCheckDeclare, filename)
  75. if err != nil {
  76. return err
  77. }
  78. _, err = fmt.Print(result)
  79. return err
  80. }
  81. // ApiFormatByPath format api from file path
  82. func ApiFormatByPath(apiFilePath string, skipCheckDeclare bool) error {
  83. if env.UseExperimental() {
  84. return apiF.File(apiFilePath)
  85. }
  86. data, err := os.ReadFile(apiFilePath)
  87. if err != nil {
  88. return err
  89. }
  90. abs, err := filepath.Abs(apiFilePath)
  91. if err != nil {
  92. return err
  93. }
  94. result, err := apiFormat(string(data), skipCheckDeclare, abs)
  95. if err != nil {
  96. return err
  97. }
  98. _, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(result, abs)
  99. if err != nil {
  100. return err
  101. }
  102. return os.WriteFile(apiFilePath, []byte(result), os.ModePerm)
  103. }
  104. func apiFormat(data string, skipCheckDeclare bool, filename ...string) (string, error) {
  105. var err error
  106. if skipCheckDeclare {
  107. _, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(data, filename...)
  108. } else {
  109. _, err = parser.ParseContent(data, filename...)
  110. }
  111. if err != nil {
  112. return "", err
  113. }
  114. var builder strings.Builder
  115. s := bufio.NewScanner(strings.NewReader(data))
  116. tapCount := 0
  117. newLineCount := 0
  118. var preLine string
  119. for s.Scan() {
  120. line := strings.TrimSpace(s.Text())
  121. if len(line) == 0 {
  122. if newLineCount > 0 {
  123. continue
  124. }
  125. newLineCount++
  126. } else {
  127. if preLine == rightBrace {
  128. builder.WriteString(pathx.NL)
  129. }
  130. newLineCount = 0
  131. }
  132. if tapCount == 0 {
  133. ft, err := formatGoTypeDef(line, s, &builder)
  134. if err != nil {
  135. return "", err
  136. }
  137. if ft {
  138. continue
  139. }
  140. }
  141. noCommentLine := util.RemoveComment(line)
  142. if noCommentLine == rightParenthesis || noCommentLine == rightBrace {
  143. tapCount--
  144. }
  145. if tapCount < 0 {
  146. line := strings.TrimSuffix(noCommentLine, rightBrace)
  147. line = strings.TrimSpace(line)
  148. if strings.HasSuffix(line, leftBrace) {
  149. tapCount++
  150. }
  151. }
  152. if line != "" {
  153. util.WriteIndent(&builder, tapCount)
  154. }
  155. builder.WriteString(line + pathx.NL)
  156. if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
  157. tapCount++
  158. }
  159. preLine = line
  160. }
  161. return strings.TrimSpace(builder.String()), nil
  162. }
  163. func formatGoTypeDef(line string, scanner *bufio.Scanner, builder *strings.Builder) (bool, error) {
  164. noCommentLine := util.RemoveComment(line)
  165. tokenCount := 0
  166. if strings.HasPrefix(noCommentLine, "type") && (strings.HasSuffix(noCommentLine, leftParenthesis) ||
  167. strings.HasSuffix(noCommentLine, leftBrace)) {
  168. var typeBuilder strings.Builder
  169. typeBuilder.WriteString(mayInsertStructKeyword(line, &tokenCount) + pathx.NL)
  170. for scanner.Scan() {
  171. noCommentLine := util.RemoveComment(scanner.Text())
  172. typeBuilder.WriteString(mayInsertStructKeyword(scanner.Text(), &tokenCount) + pathx.NL)
  173. if noCommentLine == rightBrace || noCommentLine == rightParenthesis {
  174. tokenCount--
  175. }
  176. if tokenCount == 0 {
  177. ts, err := format.Source([]byte(typeBuilder.String()))
  178. if err != nil {
  179. return false, errors.New("error format \n" + typeBuilder.String())
  180. }
  181. result := strings.ReplaceAll(string(ts), " struct ", " ")
  182. result = strings.ReplaceAll(result, "type ()", "")
  183. builder.WriteString(result)
  184. break
  185. }
  186. }
  187. return true, nil
  188. }
  189. return false, nil
  190. }
  191. func mayInsertStructKeyword(line string, token *int) string {
  192. insertStruct := func() string {
  193. if strings.Contains(line, " struct") {
  194. return line
  195. }
  196. index := strings.Index(line, leftBrace)
  197. return line[:index] + " struct " + line[index:]
  198. }
  199. noCommentLine := util.RemoveComment(line)
  200. if strings.HasSuffix(noCommentLine, leftBrace) {
  201. *token++
  202. return insertStruct()
  203. }
  204. if strings.HasSuffix(noCommentLine, rightBrace) {
  205. noCommentLine = strings.TrimSuffix(noCommentLine, rightBrace)
  206. noCommentLine = util.RemoveComment(noCommentLine)
  207. if strings.HasSuffix(noCommentLine, leftBrace) {
  208. return insertStruct()
  209. }
  210. }
  211. if strings.HasSuffix(noCommentLine, leftParenthesis) {
  212. *token++
  213. }
  214. if strings.Contains(noCommentLine, "`") {
  215. return util.UpperFirst(strings.TrimSpace(line))
  216. }
  217. return line
  218. }