format.go 5.7 KB

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