format.go 5.5 KB

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