api.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package ast
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  6. )
  7. type Api struct {
  8. LinePrefix string
  9. Syntax *SyntaxExpr
  10. Import []*ImportExpr
  11. importM map[string]PlaceHolder
  12. Info *InfoExpr
  13. Type []TypeExpr
  14. typeM map[string]PlaceHolder
  15. Service []*Service
  16. serviceM map[string]PlaceHolder
  17. handlerM map[string]PlaceHolder
  18. routeM map[string]PlaceHolder
  19. }
  20. func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} {
  21. defer func() {
  22. if p := recover(); p != nil {
  23. panic(fmt.Errorf("%+v", p))
  24. }
  25. }()
  26. var final Api
  27. final.importM = map[string]PlaceHolder{}
  28. final.typeM = map[string]PlaceHolder{}
  29. final.serviceM = map[string]PlaceHolder{}
  30. final.handlerM = map[string]PlaceHolder{}
  31. final.routeM = map[string]PlaceHolder{}
  32. for _, each := range ctx.AllSpec() {
  33. root := each.Accept(v).(*Api)
  34. if root.Syntax != nil {
  35. if final.Syntax != nil {
  36. v.panic(root.Syntax.Syntax, fmt.Sprintf("mutiple syntax declaration"))
  37. }
  38. final.Syntax = root.Syntax
  39. }
  40. for _, imp := range root.Import {
  41. if _, ok := final.importM[imp.Value.Text()]; ok {
  42. v.panic(imp.Import, fmt.Sprintf("duplicate import '%s'", imp.Value.Text()))
  43. }
  44. final.importM[imp.Value.Text()] = Holder
  45. final.Import = append(final.Import, imp)
  46. }
  47. if root.Info != nil {
  48. infoM := map[string]PlaceHolder{}
  49. if final.Info != nil {
  50. v.panic(root.Info.Info, fmt.Sprintf("mutiple info declaration"))
  51. }
  52. for _, value := range root.Info.Kvs {
  53. if _, ok := infoM[value.Key.Text()]; ok {
  54. v.panic(value.Key, fmt.Sprintf("duplicate key '%s'", value.Key.Text()))
  55. }
  56. infoM[value.Key.Text()] = Holder
  57. }
  58. final.Info = root.Info
  59. }
  60. for _, tp := range root.Type {
  61. if _, ok := final.typeM[tp.NameExpr().Text()]; ok {
  62. v.panic(tp.NameExpr(), fmt.Sprintf("duplicate type '%s'", tp.NameExpr().Text()))
  63. }
  64. final.typeM[tp.NameExpr().Text()] = Holder
  65. final.Type = append(final.Type, tp)
  66. }
  67. for _, service := range root.Service {
  68. if _, ok := final.serviceM[service.ServiceApi.Name.Text()]; !ok && len(final.serviceM) > 0 {
  69. v.panic(service.ServiceApi.Name, fmt.Sprintf("mutiple service declaration"))
  70. }
  71. if service.AtServer != nil {
  72. atServerM := map[string]PlaceHolder{}
  73. for _, kv := range service.AtServer.Kv {
  74. if _, ok := atServerM[kv.Key.Text()]; ok {
  75. v.panic(kv.Key, fmt.Sprintf("duplicate key '%s'", kv.Key.Text()))
  76. }
  77. atServerM[kv.Key.Text()] = Holder
  78. }
  79. }
  80. for _, route := range service.ServiceApi.ServiceRoute {
  81. uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), route.Route.Path.Text())
  82. if _, ok := final.routeM[uniqueRoute]; ok {
  83. v.panic(route.Route.Method, fmt.Sprintf("duplicate route '%s'", uniqueRoute))
  84. }
  85. final.routeM[uniqueRoute] = Holder
  86. var handlerExpr Expr
  87. if route.AtServer != nil {
  88. atServerM := map[string]PlaceHolder{}
  89. for _, kv := range route.AtServer.Kv {
  90. if _, ok := atServerM[kv.Key.Text()]; ok {
  91. v.panic(kv.Key, fmt.Sprintf("duplicate key '%s'", kv.Key.Text()))
  92. }
  93. atServerM[kv.Key.Text()] = Holder
  94. if kv.Key.Text() == "handler" {
  95. handlerExpr = kv.Value
  96. }
  97. }
  98. }
  99. if route.AtHandler != nil {
  100. handlerExpr = route.AtHandler.Name
  101. }
  102. if handlerExpr == nil {
  103. v.panic(route.Route.Method, fmt.Sprintf("mismtached handler"))
  104. }
  105. if handlerExpr.Text() == "" {
  106. v.panic(handlerExpr, fmt.Sprintf("mismtached handler"))
  107. }
  108. if _, ok := final.handlerM[handlerExpr.Text()]; ok {
  109. v.panic(handlerExpr, fmt.Sprintf("duplicate handler '%s'", handlerExpr.Text()))
  110. }
  111. final.handlerM[handlerExpr.Text()] = Holder
  112. }
  113. final.Service = append(final.Service, service)
  114. }
  115. }
  116. return &final
  117. }
  118. func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
  119. var root Api
  120. if ctx.SyntaxLit() != nil {
  121. root.Syntax = ctx.SyntaxLit().Accept(v).(*SyntaxExpr)
  122. }
  123. if ctx.ImportSpec() != nil {
  124. root.Import = ctx.ImportSpec().Accept(v).([]*ImportExpr)
  125. }
  126. if ctx.InfoSpec() != nil {
  127. root.Info = ctx.InfoSpec().Accept(v).(*InfoExpr)
  128. }
  129. if ctx.TypeSpec() != nil {
  130. tp := ctx.TypeSpec().Accept(v)
  131. root.Type = tp.([]TypeExpr)
  132. }
  133. if ctx.ServiceSpec() != nil {
  134. root.Service = []*Service{ctx.ServiceSpec().Accept(v).(*Service)}
  135. }
  136. return &root
  137. }
  138. func (a *Api) Format() error {
  139. // todo
  140. return nil
  141. }
  142. func (a *Api) Equal(v interface{}) bool {
  143. if v == nil {
  144. return false
  145. }
  146. root, ok := v.(*Api)
  147. if !ok {
  148. return false
  149. }
  150. if !a.Syntax.Equal(root.Syntax) {
  151. return false
  152. }
  153. if len(a.Import) != len(root.Import) {
  154. return false
  155. }
  156. var expectingImport, actualImport []*ImportExpr
  157. expectingImport = append(expectingImport, a.Import...)
  158. actualImport = append(actualImport, root.Import...)
  159. sort.Slice(expectingImport, func(i, j int) bool {
  160. return expectingImport[i].Value.Text() < expectingImport[j].Value.Text()
  161. })
  162. sort.Slice(actualImport, func(i, j int) bool {
  163. return actualImport[i].Value.Text() < actualImport[j].Value.Text()
  164. })
  165. for index, each := range expectingImport {
  166. ac := actualImport[index]
  167. if !each.Equal(ac) {
  168. return false
  169. }
  170. }
  171. if !a.Info.Equal(root.Info) {
  172. return false
  173. }
  174. if len(a.Type) != len(root.Type) {
  175. return false
  176. }
  177. var expectingType, actualType []TypeExpr
  178. expectingType = append(expectingType, a.Type...)
  179. actualType = append(actualType, root.Type...)
  180. sort.Slice(expectingType, func(i, j int) bool {
  181. return expectingType[i].NameExpr().Text() < expectingType[j].NameExpr().Text()
  182. })
  183. sort.Slice(actualType, func(i, j int) bool {
  184. return actualType[i].NameExpr().Text() < actualType[j].NameExpr().Text()
  185. })
  186. for index, each := range expectingType {
  187. ac := actualType[index]
  188. if !each.Equal(ac) {
  189. return false
  190. }
  191. }
  192. if len(a.Service) != len(root.Service) {
  193. return false
  194. }
  195. var expectingService, actualService []*Service
  196. expectingService = append(expectingService, a.Service...)
  197. actualService = append(actualService, root.Service...)
  198. for index, each := range expectingService {
  199. ac := actualService[index]
  200. if !each.Equal(ac) {
  201. return false
  202. }
  203. }
  204. return true
  205. }