util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  15. func GetType(api *spec.ApiSpec, t string) spec.Type {
  16. for _, tp := range api.Types {
  17. if tp.Name == t {
  18. return tp
  19. }
  20. }
  21. return emptyType
  22. }
  23. func isLetterDigit(r rune) bool {
  24. return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || ('0' <= r && r <= '9')
  25. }
  26. func isSpace(r rune) bool {
  27. return r == ' ' || r == '\t'
  28. }
  29. func isNewline(r rune) bool {
  30. return r == '\n' || r == '\r'
  31. }
  32. func read(r *bufio.Reader) (rune, error) {
  33. ch, _, err := r.ReadRune()
  34. return ch, err
  35. }
  36. func readLine(r *bufio.Reader) (string, error) {
  37. line, _, err := r.ReadLine()
  38. if err != nil {
  39. return "", err
  40. } else {
  41. return string(line), nil
  42. }
  43. }
  44. func skipSpaces(r *bufio.Reader) error {
  45. for {
  46. next, err := read(r)
  47. if err != nil {
  48. return err
  49. }
  50. if !isSpace(next) {
  51. return unread(r)
  52. }
  53. }
  54. }
  55. func unread(r *bufio.Reader) error {
  56. return r.UnreadRune()
  57. }
  58. func MatchStruct(api string) (*ApiStruct, error) {
  59. var result ApiStruct
  60. scanner := bufio.NewScanner(strings.NewReader(api))
  61. var parseInfo = false
  62. var parseImport = false
  63. var parseType = false
  64. var parseService = false
  65. var segment string
  66. for scanner.Scan() {
  67. line := strings.TrimSpace(scanner.Text())
  68. if line == "info(" {
  69. parseInfo = true
  70. }
  71. if line == ")" && parseInfo {
  72. parseInfo = false
  73. result.Info = segment + ")"
  74. segment = ""
  75. continue
  76. }
  77. if isImportBeginLine(line) {
  78. parseImport = true
  79. }
  80. if parseImport && (isTypeBeginLine(line) || isServiceBeginLine(line)) {
  81. parseImport = false
  82. result.Imports = segment
  83. segment = line + "\n"
  84. continue
  85. }
  86. if isTypeBeginLine(line) {
  87. parseType = true
  88. }
  89. if isServiceBeginLine(line) {
  90. if parseType {
  91. parseType = false
  92. result.StructBody = segment
  93. segment = line + "\n"
  94. continue
  95. }
  96. parseService = true
  97. }
  98. segment += scanner.Text() + "\n"
  99. }
  100. if !parseService {
  101. return nil, errors.New("no service defined")
  102. }
  103. result.Service = segment
  104. return &result, nil
  105. }
  106. func isImportBeginLine(line string) bool {
  107. return strings.HasPrefix(line, "import") && strings.HasSuffix(line, ".api")
  108. }
  109. func isTypeBeginLine(line string) bool {
  110. return strings.HasPrefix(line, "type")
  111. }
  112. func isServiceBeginLine(line string) bool {
  113. return strings.HasPrefix(line, "@server(") || (strings.HasPrefix(line, "service") && strings.HasSuffix(line, "{"))
  114. }