1
0

service.go 14 KB

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