service.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 Service struct {
  8. AtServer *AtServer
  9. ServiceApi *ServiceApi
  10. }
  11. type KV []*KvExpr
  12. type AtServer struct {
  13. AtServerToken Expr
  14. Lp Expr
  15. Rp Expr
  16. Kv KV
  17. }
  18. type ServiceApi struct {
  19. ServiceToken Expr
  20. Name Expr
  21. Lbrace Expr
  22. Rbrace Expr
  23. ServiceRoute []*ServiceRoute
  24. }
  25. type ServiceRoute struct {
  26. AtDoc *AtDoc
  27. AtServer *AtServer
  28. AtHandler *AtHandler
  29. Route *Route
  30. }
  31. type AtDoc struct {
  32. AtDocToken Expr
  33. Lp Expr
  34. Rp Expr
  35. LineDoc Expr
  36. Kv []*KvExpr
  37. }
  38. type AtHandler struct {
  39. AtHandlerToken Expr
  40. Name Expr
  41. DocExpr []Expr
  42. CommentExpr Expr
  43. }
  44. type Route struct {
  45. Method Expr
  46. Path Expr
  47. Req *Body
  48. ReturnToken Expr
  49. Reply *Body
  50. DocExpr []Expr
  51. CommentExpr Expr
  52. }
  53. type Body struct {
  54. Lp Expr
  55. Rp Expr
  56. Name DataType
  57. }
  58. func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{} {
  59. var serviceSpec Service
  60. if ctx.AtServer() != nil {
  61. serviceSpec.AtServer = ctx.AtServer().Accept(v).(*AtServer)
  62. }
  63. serviceSpec.ServiceApi = ctx.ServiceApi().Accept(v).(*ServiceApi)
  64. return &serviceSpec
  65. }
  66. func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{} {
  67. var atServer AtServer
  68. atServer.AtServerToken = v.newExprWithTerminalNode(ctx.ATSERVER())
  69. atServer.Lp = v.newExprWithToken(ctx.GetLp())
  70. atServer.Rp = v.newExprWithToken(ctx.GetRp())
  71. for _, each := range ctx.AllKvLit() {
  72. atServer.Kv = append(atServer.Kv, each.Accept(v).(*KvExpr))
  73. }
  74. return &atServer
  75. }
  76. func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{} {
  77. var serviceApi ServiceApi
  78. serviceApi.ServiceToken = v.newExprWithToken(ctx.GetServiceToken())
  79. serviceName := ctx.ServiceName()
  80. serviceApi.Name = v.newExprWithText(serviceName.GetText(), serviceName.GetStart().GetLine(), serviceName.GetStart().GetColumn(), serviceName.GetStart().GetStart(), serviceName.GetStop().GetStop())
  81. serviceApi.Lbrace = v.newExprWithToken(ctx.GetLbrace())
  82. serviceApi.Rbrace = v.newExprWithToken(ctx.GetRbrace())
  83. for _, each := range ctx.AllServiceRoute() {
  84. serviceApi.ServiceRoute = append(serviceApi.ServiceRoute, each.Accept(v).(*ServiceRoute))
  85. }
  86. return &serviceApi
  87. }
  88. func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{} {
  89. var serviceRoute ServiceRoute
  90. if ctx.AtDoc() != nil {
  91. serviceRoute.AtDoc = ctx.AtDoc().Accept(v).(*AtDoc)
  92. }
  93. if ctx.AtServer() != nil {
  94. serviceRoute.AtServer = ctx.AtServer().Accept(v).(*AtServer)
  95. } else if ctx.AtHandler() != nil {
  96. serviceRoute.AtHandler = ctx.AtHandler().Accept(v).(*AtHandler)
  97. }
  98. serviceRoute.Route = ctx.Route().Accept(v).(*Route)
  99. return &serviceRoute
  100. }
  101. func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{} {
  102. var atDoc AtDoc
  103. atDoc.AtDocToken = v.newExprWithTerminalNode(ctx.ATDOC())
  104. if ctx.STRING() != nil {
  105. atDoc.LineDoc = v.newExprWithTerminalNode(ctx.STRING())
  106. } else {
  107. for _, each := range ctx.AllKvLit() {
  108. atDoc.Kv = append(atDoc.Kv, each.Accept(v).(*KvExpr))
  109. }
  110. }
  111. atDoc.Lp = v.newExprWithToken(ctx.GetLp())
  112. atDoc.Rp = v.newExprWithToken(ctx.GetRp())
  113. if ctx.GetLp() != nil {
  114. if ctx.GetRp() == nil {
  115. v.panic(atDoc.Lp, "mismatched ')'")
  116. }
  117. }
  118. if ctx.GetRp() != nil {
  119. if ctx.GetLp() == nil {
  120. v.panic(atDoc.Rp, "mismatched '('")
  121. }
  122. }
  123. return &atDoc
  124. }
  125. func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
  126. var atHandler AtHandler
  127. astHandlerExpr := v.newExprWithTerminalNode(ctx.ATHANDLER())
  128. atHandler.AtHandlerToken = astHandlerExpr
  129. atHandler.Name = v.newExprWithTerminalNode(ctx.ID())
  130. atHandler.DocExpr = v.getDoc(ctx)
  131. atHandler.CommentExpr = v.getComment(ctx)
  132. return &atHandler
  133. }
  134. func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
  135. var route Route
  136. path := ctx.Path()
  137. methodExpr := v.newExprWithToken(ctx.GetHttpMethod())
  138. route.Method = methodExpr
  139. route.Path = v.newExprWithText(path.GetText(), path.GetStart().GetLine(), path.GetStart().GetColumn(), path.GetStart().GetStart(), path.GetStop().GetStop())
  140. if ctx.GetRequest() != nil {
  141. req := ctx.GetRequest().Accept(v)
  142. if req != nil {
  143. route.Req = req.(*Body)
  144. }
  145. }
  146. if ctx.GetResponse() != nil {
  147. reply := ctx.GetResponse().Accept(v)
  148. if reply != nil {
  149. route.Reply = reply.(*Body)
  150. }
  151. }
  152. if ctx.GetReturnToken() != nil {
  153. returnExpr := v.newExprWithToken(ctx.GetReturnToken())
  154. if ctx.GetReturnToken().GetText() != "returns" {
  155. v.panic(returnExpr, fmt.Sprintf("expecting returns, found input '%s'", ctx.GetReturnToken().GetText()))
  156. }
  157. route.ReturnToken = returnExpr
  158. }
  159. route.DocExpr = v.getDoc(ctx)
  160. route.CommentExpr = v.getComment(ctx)
  161. return &route
  162. }
  163. func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
  164. if ctx.ID() == nil {
  165. return nil
  166. }
  167. idRxpr := v.newExprWithTerminalNode(ctx.ID())
  168. if api.IsGolangKeyWord(idRxpr.Text()) {
  169. v.panic(idRxpr, fmt.Sprintf("expecting 'ID', but found golang keyword '%s'", idRxpr.Text()))
  170. }
  171. v.exportCheck(idRxpr)
  172. return &Body{
  173. Lp: v.newExprWithToken(ctx.GetLp()),
  174. Rp: v.newExprWithToken(ctx.GetRp()),
  175. Name: &Literal{Literal: idRxpr},
  176. }
  177. }
  178. // note: forward compatible
  179. func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
  180. if ctx.DataType() == nil {
  181. return nil
  182. }
  183. dt := ctx.DataType().Accept(v).(DataType)
  184. if dt == nil {
  185. return nil
  186. }
  187. switch dataType := dt.(type) {
  188. case *Array:
  189. lit := dataType.Literal
  190. switch lit.(type) {
  191. case *Literal, *Pointer:
  192. if api.IsGolangKeyWord(lit.Expr().Text()) {
  193. v.panic(lit.Expr(), fmt.Sprintf("expecting 'ID', but found golang keyword '%s'", lit.Expr().Text()))
  194. }
  195. default:
  196. v.panic(dt.Expr(), fmt.Sprintf("unsupport %s", dt.Expr().Text()))
  197. }
  198. v.log.Warning("%s %d:%d deprecated array type near '%s'", v.prefix, dataType.ArrayExpr.Line(), dataType.ArrayExpr.Column(), dataType.ArrayExpr.Text())
  199. case *Literal:
  200. lit := dataType.Literal.Text()
  201. if api.IsGolangKeyWord(dataType.Literal.Text()) {
  202. v.panic(dataType.Literal, fmt.Sprintf("expecting 'ID', but found golang keyword '%s'", dataType.Literal.Text()))
  203. }
  204. if api.IsBasicType(lit) {
  205. v.panic(dt.Expr(), fmt.Sprintf("unsupport %s", dt.Expr().Text()))
  206. }
  207. default:
  208. v.panic(dt.Expr(), fmt.Sprintf("unsupport %s", dt.Expr().Text()))
  209. }
  210. return &Body{
  211. Lp: v.newExprWithToken(ctx.GetLp()),
  212. Rp: v.newExprWithToken(ctx.GetRp()),
  213. Name: dt,
  214. }
  215. }
  216. func (b *Body) Format() error {
  217. // todo
  218. return nil
  219. }
  220. func (b *Body) Equal(v interface{}) bool {
  221. if v == nil {
  222. return false
  223. }
  224. body, ok := v.(*Body)
  225. if !ok {
  226. return false
  227. }
  228. if !b.Lp.Equal(body.Lp) {
  229. return false
  230. }
  231. if !b.Rp.Equal(body.Rp) {
  232. return false
  233. }
  234. return b.Name.Equal(body.Name)
  235. }
  236. func (r *Route) Format() error {
  237. // todo
  238. return nil
  239. }
  240. func (r *Route) Doc() []Expr {
  241. return r.DocExpr
  242. }
  243. func (r *Route) Comment() Expr {
  244. return r.CommentExpr
  245. }
  246. func (r *Route) Equal(v interface{}) bool {
  247. if v == nil {
  248. return false
  249. }
  250. route, ok := v.(*Route)
  251. if !ok {
  252. return false
  253. }
  254. if !r.Method.Equal(route.Method) {
  255. return false
  256. }
  257. if !r.Path.Equal(route.Path) {
  258. return false
  259. }
  260. if r.Req != nil {
  261. if !r.Req.Equal(route.Req) {
  262. return false
  263. }
  264. }
  265. if r.ReturnToken != nil {
  266. if !r.ReturnToken.Equal(route.ReturnToken) {
  267. return false
  268. }
  269. }
  270. if r.Reply != nil {
  271. if !r.Reply.Equal(route.Reply) {
  272. return false
  273. }
  274. }
  275. return EqualDoc(r, route)
  276. }
  277. func (a *AtHandler) Doc() []Expr {
  278. return a.DocExpr
  279. }
  280. func (a *AtHandler) Comment() Expr {
  281. return a.CommentExpr
  282. }
  283. func (a *AtHandler) Format() error {
  284. // todo
  285. return nil
  286. }
  287. func (a *AtHandler) Equal(v interface{}) bool {
  288. if v == nil {
  289. return false
  290. }
  291. atHandler, ok := v.(*AtHandler)
  292. if !ok {
  293. return false
  294. }
  295. if !a.AtHandlerToken.Equal(atHandler.AtHandlerToken) {
  296. return false
  297. }
  298. if !a.Name.Equal(atHandler.Name) {
  299. return false
  300. }
  301. return EqualDoc(a, atHandler)
  302. }
  303. func (a *AtDoc) Format() error {
  304. // todo
  305. return nil
  306. }
  307. func (a *AtDoc) Equal(v interface{}) bool {
  308. if v == nil {
  309. return false
  310. }
  311. atDoc, ok := v.(*AtDoc)
  312. if !ok {
  313. return false
  314. }
  315. if !a.AtDocToken.Equal(atDoc.AtDocToken) {
  316. return false
  317. }
  318. if a.Lp.IsNotNil() {
  319. if !a.Lp.Equal(atDoc.Lp) {
  320. return false
  321. }
  322. }
  323. if a.Rp.IsNotNil() {
  324. if !a.Rp.Equal(atDoc.Rp) {
  325. return false
  326. }
  327. }
  328. if a.LineDoc != nil {
  329. if !a.LineDoc.Equal(atDoc.LineDoc) {
  330. return false
  331. }
  332. }
  333. var expecting, actual []*KvExpr
  334. expecting = append(expecting, a.Kv...)
  335. actual = append(actual, atDoc.Kv...)
  336. if len(expecting) != len(actual) {
  337. return false
  338. }
  339. for index, each := range expecting {
  340. ac := actual[index]
  341. if !each.Equal(ac) {
  342. return false
  343. }
  344. }
  345. return true
  346. }
  347. func (a *AtServer) Format() error {
  348. // todo
  349. return nil
  350. }
  351. func (a *AtServer) Equal(v interface{}) bool {
  352. if v == nil {
  353. return false
  354. }
  355. atServer, ok := v.(*AtServer)
  356. if !ok {
  357. return false
  358. }
  359. if !a.AtServerToken.Equal(atServer.AtServerToken) {
  360. return false
  361. }
  362. if !a.Lp.Equal(atServer.Lp) {
  363. return false
  364. }
  365. if !a.Rp.Equal(atServer.Rp) {
  366. return false
  367. }
  368. var expecting, actual []*KvExpr
  369. expecting = append(expecting, a.Kv...)
  370. actual = append(actual, atServer.Kv...)
  371. if len(expecting) != len(actual) {
  372. return false
  373. }
  374. sort.Slice(expecting, func(i, j int) bool {
  375. return expecting[i].Key.Text() < expecting[j].Key.Text()
  376. })
  377. sort.Slice(actual, func(i, j int) bool {
  378. return actual[i].Key.Text() < actual[j].Key.Text()
  379. })
  380. for index, each := range expecting {
  381. ac := actual[index]
  382. if !each.Equal(ac) {
  383. return false
  384. }
  385. }
  386. return true
  387. }
  388. func (s *ServiceRoute) Equal(v interface{}) bool {
  389. if v == nil {
  390. return false
  391. }
  392. sr, ok := v.(*ServiceRoute)
  393. if !ok {
  394. return false
  395. }
  396. if !s.AtDoc.Equal(sr.AtDoc) {
  397. return false
  398. }
  399. if s.AtServer != nil {
  400. if !s.AtServer.Equal(sr.AtServer) {
  401. return false
  402. }
  403. }
  404. if s.AtHandler != nil {
  405. if !s.AtHandler.Equal(sr.AtHandler) {
  406. return false
  407. }
  408. }
  409. return s.Route.Equal(sr.Route)
  410. }
  411. func (s *ServiceRoute) Format() error {
  412. // todo
  413. return nil
  414. }
  415. func (s *ServiceRoute) GetHandler() Expr {
  416. if s.AtHandler != nil {
  417. return s.AtHandler.Name
  418. }
  419. return s.AtServer.Kv.Get("handler")
  420. }
  421. func (a *ServiceApi) Format() error {
  422. // todo
  423. return nil
  424. }
  425. func (a *ServiceApi) Equal(v interface{}) bool {
  426. if v == nil {
  427. return false
  428. }
  429. api, ok := v.(*ServiceApi)
  430. if !ok {
  431. return false
  432. }
  433. if !a.ServiceToken.Equal(api.ServiceToken) {
  434. return false
  435. }
  436. if !a.Name.Equal(api.Name) {
  437. return false
  438. }
  439. if !a.Lbrace.Equal(api.Lbrace) {
  440. return false
  441. }
  442. if !a.Rbrace.Equal(api.Rbrace) {
  443. return false
  444. }
  445. var expecting, acutal []*ServiceRoute
  446. expecting = append(expecting, a.ServiceRoute...)
  447. acutal = append(acutal, api.ServiceRoute...)
  448. if len(expecting) != len(acutal) {
  449. return false
  450. }
  451. sort.Slice(expecting, func(i, j int) bool {
  452. return expecting[i].Route.Path.Text() < expecting[j].Route.Path.Text()
  453. })
  454. sort.Slice(acutal, func(i, j int) bool {
  455. return acutal[i].Route.Path.Text() < acutal[j].Route.Path.Text()
  456. })
  457. for index, each := range expecting {
  458. ac := acutal[index]
  459. if !each.Equal(ac) {
  460. return false
  461. }
  462. }
  463. return true
  464. }
  465. func (s *Service) Format() error {
  466. // todo
  467. return nil
  468. }
  469. func (s *Service) Equal(v interface{}) bool {
  470. if v == nil {
  471. return false
  472. }
  473. service, ok := v.(*Service)
  474. if !ok {
  475. return false
  476. }
  477. if s.AtServer != nil {
  478. if !s.AtServer.Equal(service.AtServer) {
  479. return false
  480. }
  481. }
  482. return s.ServiceApi.Equal(service.ServiceApi)
  483. }
  484. func (kv KV) Get(key string) Expr {
  485. for _, each := range kv {
  486. if each.Key.Text() == key {
  487. return each.Value
  488. }
  489. }
  490. return nil
  491. }