service.go 14 KB

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