api.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package ast
  2. import (
  3. "fmt"
  4. "path"
  5. "sort"
  6. "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
  7. )
  8. const (
  9. prefixKey = "prefix"
  10. groupKey = "group"
  11. )
  12. // Api describes syntax for api
  13. type Api struct {
  14. LinePrefix string
  15. Syntax *SyntaxExpr
  16. Import []*ImportExpr
  17. importM map[string]PlaceHolder
  18. Info *InfoExpr
  19. Type []TypeExpr
  20. typeM map[string]PlaceHolder
  21. Service []*Service
  22. serviceM map[string]PlaceHolder
  23. handlerM map[string]PlaceHolder
  24. routeM map[string]PlaceHolder
  25. }
  26. // VisitApi implements from api.BaseApiParserVisitor
  27. func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} {
  28. var final Api
  29. final.importM = map[string]PlaceHolder{}
  30. final.typeM = map[string]PlaceHolder{}
  31. final.serviceM = map[string]PlaceHolder{}
  32. final.handlerM = map[string]PlaceHolder{}
  33. final.routeM = map[string]PlaceHolder{}
  34. for _, each := range ctx.AllSpec() {
  35. root := each.Accept(v).(*Api)
  36. v.acceptSyntax(root, &final)
  37. v.acceptImport(root, &final)
  38. v.acceptInfo(root, &final)
  39. v.acceptType(root, &final)
  40. v.acceptService(root, &final)
  41. }
  42. return &final
  43. }
  44. func (v *ApiVisitor) acceptService(root, final *Api) {
  45. for _, service := range root.Service {
  46. if _, ok := final.serviceM[service.ServiceApi.Name.Text()]; !ok && len(final.serviceM) > 0 {
  47. v.panic(service.ServiceApi.Name, "multiple service declaration")
  48. }
  49. v.duplicateServerItemCheck(service)
  50. var prefix, group string
  51. if service.AtServer != nil {
  52. p := service.AtServer.Kv.Get(prefixKey)
  53. if p != nil {
  54. prefix = p.Text()
  55. }
  56. g := service.AtServer.Kv.Get(groupKey)
  57. if g != nil {
  58. group = g.Text()
  59. }
  60. }
  61. for _, route := range service.ServiceApi.ServiceRoute {
  62. uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), path.Join(prefix, route.Route.Path.Text()))
  63. if _, ok := final.routeM[uniqueRoute]; ok {
  64. v.panic(route.Route.Method, fmt.Sprintf("duplicate route '%s'", uniqueRoute))
  65. }
  66. final.routeM[uniqueRoute] = Holder
  67. var handlerExpr Expr
  68. if route.AtServer != nil {
  69. atServerM := map[string]PlaceHolder{}
  70. for _, kv := range route.AtServer.Kv {
  71. if _, ok := atServerM[kv.Key.Text()]; ok {
  72. v.panic(kv.Key, fmt.Sprintf("duplicate key '%s'", kv.Key.Text()))
  73. }
  74. atServerM[kv.Key.Text()] = Holder
  75. if kv.Key.Text() == "handler" {
  76. handlerExpr = kv.Value
  77. }
  78. }
  79. }
  80. if route.AtHandler != nil {
  81. handlerExpr = route.AtHandler.Name
  82. }
  83. if handlerExpr == nil {
  84. v.panic(route.Route.Method, "mismatched handler")
  85. }
  86. if handlerExpr.Text() == "" {
  87. v.panic(handlerExpr, "mismatched handler")
  88. }
  89. handlerKey := handlerExpr.Text()
  90. if len(group) > 0 {
  91. handlerKey = fmt.Sprintf("%s/%s", group, handlerExpr.Text())
  92. }
  93. if _, ok := final.handlerM[handlerKey]; ok {
  94. v.panic(handlerExpr, fmt.Sprintf("duplicate handler '%s'", handlerExpr.Text()))
  95. }
  96. final.handlerM[handlerKey] = Holder
  97. }
  98. final.Service = append(final.Service, service)
  99. }
  100. }
  101. func (v *ApiVisitor) duplicateServerItemCheck(service *Service) {
  102. if service.AtServer != nil {
  103. atServerM := map[string]PlaceHolder{}
  104. for _, kv := range service.AtServer.Kv {
  105. if _, ok := atServerM[kv.Key.Text()]; ok {
  106. v.panic(kv.Key, fmt.Sprintf("duplicate key '%s'", kv.Key.Text()))
  107. }
  108. atServerM[kv.Key.Text()] = Holder
  109. }
  110. }
  111. }
  112. func (v *ApiVisitor) acceptType(root, final *Api) {
  113. for _, tp := range root.Type {
  114. if _, ok := final.typeM[tp.NameExpr().Text()]; ok {
  115. v.panic(tp.NameExpr(), fmt.Sprintf("duplicate type '%s'", tp.NameExpr().Text()))
  116. }
  117. final.typeM[tp.NameExpr().Text()] = Holder
  118. final.Type = append(final.Type, tp)
  119. }
  120. }
  121. func (v *ApiVisitor) acceptInfo(root, final *Api) {
  122. if root.Info != nil {
  123. infoM := map[string]PlaceHolder{}
  124. if final.Info != nil {
  125. v.panic(root.Info.Info, "multiple info declaration")
  126. }
  127. for _, value := range root.Info.Kvs {
  128. if _, ok := infoM[value.Key.Text()]; ok {
  129. v.panic(value.Key, fmt.Sprintf("duplicate key '%s'", value.Key.Text()))
  130. }
  131. infoM[value.Key.Text()] = Holder
  132. }
  133. final.Info = root.Info
  134. }
  135. }
  136. func (v *ApiVisitor) acceptImport(root, final *Api) {
  137. for _, imp := range root.Import {
  138. if _, ok := final.importM[imp.Value.Text()]; ok {
  139. v.panic(imp.Import, fmt.Sprintf("duplicate import '%s'", imp.Value.Text()))
  140. }
  141. final.importM[imp.Value.Text()] = Holder
  142. final.Import = append(final.Import, imp)
  143. }
  144. }
  145. func (v *ApiVisitor) acceptSyntax(root, final *Api) {
  146. if root.Syntax != nil {
  147. if final.Syntax != nil {
  148. v.panic(root.Syntax.Syntax, "multiple syntax declaration")
  149. }
  150. final.Syntax = root.Syntax
  151. }
  152. }
  153. // VisitSpec implements from api.BaseApiParserVisitor
  154. func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
  155. var root Api
  156. if ctx.SyntaxLit() != nil {
  157. root.Syntax = ctx.SyntaxLit().Accept(v).(*SyntaxExpr)
  158. }
  159. if ctx.ImportSpec() != nil {
  160. root.Import = ctx.ImportSpec().Accept(v).([]*ImportExpr)
  161. }
  162. if ctx.InfoSpec() != nil {
  163. root.Info = ctx.InfoSpec().Accept(v).(*InfoExpr)
  164. }
  165. if ctx.TypeSpec() != nil {
  166. tp := ctx.TypeSpec().Accept(v)
  167. root.Type = tp.([]TypeExpr)
  168. }
  169. if ctx.ServiceSpec() != nil {
  170. root.Service = []*Service{ctx.ServiceSpec().Accept(v).(*Service)}
  171. }
  172. return &root
  173. }
  174. // Format provides a formatter for api command, now nothing to do
  175. func (a *Api) Format() error {
  176. // todo
  177. return nil
  178. }
  179. // Equal compares whether the element literals in two Api are equal
  180. func (a *Api) Equal(v interface{}) bool {
  181. if v == nil || a == nil {
  182. return false
  183. }
  184. root, ok := v.(*Api)
  185. if !ok {
  186. return false
  187. }
  188. if !a.Syntax.Equal(root.Syntax) {
  189. return false
  190. }
  191. if len(a.Import) != len(root.Import) {
  192. return false
  193. }
  194. var expectingImport, actualImport []*ImportExpr
  195. expectingImport = append(expectingImport, a.Import...)
  196. actualImport = append(actualImport, root.Import...)
  197. sort.Slice(expectingImport, func(i, j int) bool {
  198. return expectingImport[i].Value.Text() < expectingImport[j].Value.Text()
  199. })
  200. sort.Slice(actualImport, func(i, j int) bool {
  201. return actualImport[i].Value.Text() < actualImport[j].Value.Text()
  202. })
  203. for index, each := range expectingImport {
  204. ac := actualImport[index]
  205. if !each.Equal(ac) {
  206. return false
  207. }
  208. }
  209. if !a.Info.Equal(root.Info) {
  210. return false
  211. }
  212. if len(a.Type) != len(root.Type) {
  213. return false
  214. }
  215. var expectingType, actualType []TypeExpr
  216. expectingType = append(expectingType, a.Type...)
  217. actualType = append(actualType, root.Type...)
  218. sort.Slice(expectingType, func(i, j int) bool {
  219. return expectingType[i].NameExpr().Text() < expectingType[j].NameExpr().Text()
  220. })
  221. sort.Slice(actualType, func(i, j int) bool {
  222. return actualType[i].NameExpr().Text() < actualType[j].NameExpr().Text()
  223. })
  224. for index, each := range expectingType {
  225. ac := actualType[index]
  226. if !each.Equal(ac) {
  227. return false
  228. }
  229. }
  230. if len(a.Service) != len(root.Service) {
  231. return false
  232. }
  233. var expectingService, actualService []*Service
  234. expectingService = append(expectingService, a.Service...)
  235. actualService = append(actualService, root.Service...)
  236. for index, each := range expectingService {
  237. ac := actualService[index]
  238. if !each.Equal(ac) {
  239. return false
  240. }
  241. }
  242. return true
  243. }