info.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package ast
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  4. )
  5. type InfoExpr struct {
  6. Info Expr
  7. Lp Expr
  8. Rp Expr
  9. Kvs []*KvExpr
  10. }
  11. func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
  12. var expr InfoExpr
  13. expr.Info = v.newExprWithToken(ctx.GetInfoToken())
  14. expr.Lp = v.newExprWithToken(ctx.GetLp())
  15. expr.Rp = v.newExprWithToken(ctx.GetRp())
  16. list := ctx.AllKvLit()
  17. for _, each := range list {
  18. kvExpr := each.Accept(v).(*KvExpr)
  19. expr.Kvs = append(expr.Kvs, kvExpr)
  20. }
  21. if v.infoFlag {
  22. v.panic(expr.Info, "duplicate declaration 'info'")
  23. }
  24. return &expr
  25. }
  26. func (i *InfoExpr) Format() error {
  27. // todo
  28. return nil
  29. }
  30. func (i *InfoExpr) Equal(v interface{}) bool {
  31. if v == nil {
  32. return false
  33. }
  34. info, ok := v.(*InfoExpr)
  35. if !ok {
  36. return false
  37. }
  38. if !i.Info.Equal(info.Info) {
  39. return false
  40. }
  41. var expected, actual []*KvExpr
  42. expected = append(expected, i.Kvs...)
  43. actual = append(actual, info.Kvs...)
  44. if len(expected) != len(actual) {
  45. return false
  46. }
  47. for index, each := range expected {
  48. ac := actual[index]
  49. if !each.Equal(ac) {
  50. return false
  51. }
  52. }
  53. return true
  54. }