format.go 5.7 KB

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