1
0

util.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package parser
  2. import (
  3. "bufio"
  4. "errors"
  5. "strings"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  7. )
  8. var emptyType spec.Type
  9. type ApiStruct struct {
  10. Info string
  11. StructBody string
  12. Service string
  13. Imports string
  14. serviceBeginLine int
  15. }
  16. func GetType(api *spec.ApiSpec, t string) spec.Type {
  17. for _, tp := range api.Types {
  18. if tp.Name == t {
  19. return tp
  20. }
  21. }
  22. return emptyType
  23. }
  24. func isLetterDigit(r rune) bool {
  25. return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || ('0' <= r && r <= '9')
  26. }
  27. func isSpace(r rune) bool {
  28. return r == ' ' || r == '\t'
  29. }
  30. func isNewline(r rune) bool {
  31. return r == '\n' || r == '\r'
  32. }
  33. func read(r *bufio.Reader) (rune, error) {
  34. ch, _, err := r.ReadRune()
  35. return ch, err
  36. }
  37. func readLine(r *bufio.Reader) (string, error) {
  38. line, _, err := r.ReadLine()
  39. if err != nil {
  40. return "", err
  41. } else {
  42. return string(line), nil
  43. }
  44. }
  45. func skipSpaces(r *bufio.Reader) error {
  46. for {
  47. next, err := read(r)
  48. if err != nil {
  49. return err
  50. }
  51. if !isSpace(next) {
  52. return unread(r)
  53. }
  54. }
  55. }
  56. func unread(r *bufio.Reader) error {
  57. return r.UnreadRune()
  58. }
  59. func ParseApi(api string) (*ApiStruct, error) {
  60. var result ApiStruct
  61. scanner := bufio.NewScanner(strings.NewReader(api))
  62. var parseInfo = false
  63. var parseImport = false
  64. var parseType = false
  65. var parseService = false
  66. var segment string
  67. for scanner.Scan() {
  68. line := strings.TrimSpace(scanner.Text())
  69. if line == "info(" {
  70. parseInfo = true
  71. }
  72. if line == ")" && parseInfo {
  73. parseInfo = false
  74. result.Info = segment + ")"
  75. segment = ""
  76. continue
  77. }
  78. if isImportBeginLine(line) {
  79. parseImport = true
  80. }
  81. if parseImport && (isTypeBeginLine(line) || isServiceBeginLine(line)) {
  82. parseImport = false
  83. result.Imports = segment
  84. segment = line + "\n"
  85. continue
  86. }
  87. if isTypeBeginLine(line) {
  88. parseType = true
  89. }
  90. if isServiceBeginLine(line) {
  91. parseService = true
  92. if parseType {
  93. parseType = false
  94. result.StructBody = segment
  95. segment = line + "\n"
  96. continue
  97. }
  98. }
  99. segment += scanner.Text() + "\n"
  100. }
  101. if !parseService {
  102. return nil, errors.New("no service defined")
  103. }
  104. result.Service = segment
  105. result.serviceBeginLine = lineBeginOfService(api)
  106. return &result, nil
  107. }
  108. func isImportBeginLine(line string) bool {
  109. return strings.HasPrefix(line, "import") && (strings.HasSuffix(line, ".api") || strings.HasSuffix(line, `.api"`))
  110. }
  111. func isTypeBeginLine(line string) bool {
  112. return strings.HasPrefix(line, "type")
  113. }
  114. func isServiceBeginLine(line string) bool {
  115. return strings.HasPrefix(line, "@server") || (strings.HasPrefix(line, "service") && strings.HasSuffix(line, "{"))
  116. }
  117. func lineBeginOfService(api string) int {
  118. scanner := bufio.NewScanner(strings.NewReader(api))
  119. var number = 0
  120. for scanner.Scan() {
  121. line := strings.TrimSpace(scanner.Text())
  122. if isServiceBeginLine(line) {
  123. break
  124. }
  125. number++
  126. }
  127. return number
  128. }