type.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. package ast
  2. import (
  3. "fmt"
  4. "sort"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/gen/api"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/api/util"
  7. )
  8. type (
  9. // TypeExpr describes an expression for TypeAlias and TypeStruct
  10. TypeExpr interface {
  11. Doc() []Expr
  12. Format() error
  13. Equal(v interface{}) bool
  14. NameExpr() Expr
  15. }
  16. // TypeAlias describes alias ast for api syntax
  17. TypeAlias struct {
  18. Name Expr
  19. Assign Expr
  20. DataType DataType
  21. DocExpr []Expr
  22. CommentExpr Expr
  23. }
  24. // TypeStruct describes structure ast for api syntax
  25. TypeStruct struct {
  26. Name Expr
  27. Struct Expr
  28. LBrace Expr
  29. RBrace Expr
  30. DocExpr []Expr
  31. Fields []*TypeField
  32. }
  33. // TypeField describes field ast for api syntax
  34. TypeField struct {
  35. IsAnonymous bool
  36. // Name is nil if IsAnonymous
  37. Name Expr
  38. DataType DataType
  39. Tag Expr
  40. DocExpr []Expr
  41. CommentExpr Expr
  42. }
  43. // DataType describes datatype for api syntax, the default implementation expressions are
  44. // Literal, Interface, Map, Array, Time, Pointer
  45. DataType interface {
  46. Expr() Expr
  47. Equal(dt DataType) bool
  48. Format() error
  49. IsNotNil() bool
  50. }
  51. // Literal describes the basic types of golang, non-reference types,
  52. // such as int, bool, Foo,...
  53. Literal struct {
  54. Literal Expr
  55. }
  56. // Interface describes the interface type of golang,Its fixed value is interface{}
  57. Interface struct {
  58. Literal Expr
  59. }
  60. // Map describes the map ast for api syntax
  61. Map struct {
  62. MapExpr Expr
  63. Map Expr
  64. LBrack Expr
  65. RBrack Expr
  66. Key Expr
  67. Value DataType
  68. }
  69. // Array describes the slice ast for api syntax
  70. Array struct {
  71. ArrayExpr Expr
  72. LBrack Expr
  73. RBrack Expr
  74. Literal DataType
  75. }
  76. // Time describes the time ast for api syntax
  77. Time struct {
  78. Literal Expr
  79. }
  80. // Pointer describes the pointer ast for api syntax
  81. Pointer struct {
  82. PointerExpr Expr
  83. Star Expr
  84. Name Expr
  85. }
  86. )
  87. // VisitTypeSpec implements from api.BaseApiParserVisitor
  88. func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{} {
  89. if ctx.TypeLit() != nil {
  90. return []TypeExpr{ctx.TypeLit().Accept(v).(TypeExpr)}
  91. }
  92. return ctx.TypeBlock().Accept(v)
  93. }
  94. // VisitTypeLit implements from api.BaseApiParserVisitor
  95. func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
  96. typeLit := ctx.TypeLitBody().Accept(v)
  97. alias, ok := typeLit.(*TypeAlias)
  98. if ok {
  99. return alias
  100. }
  101. doc := v.getDoc(ctx)
  102. st, ok := typeLit.(*TypeStruct)
  103. if ok {
  104. st.DocExpr = doc
  105. return st
  106. }
  107. return typeLit
  108. }
  109. // VisitTypeBlock implements from api.BaseApiParserVisitor
  110. func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{} {
  111. list := ctx.AllTypeBlockBody()
  112. var types []TypeExpr
  113. for _, each := range list {
  114. types = append(types, each.Accept(v).(TypeExpr))
  115. }
  116. return types
  117. }
  118. // VisitTypeLitBody implements from api.BaseApiParserVisitor
  119. func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{} {
  120. if ctx.TypeAlias() != nil {
  121. return ctx.TypeAlias().Accept(v)
  122. }
  123. return ctx.TypeStruct().Accept(v)
  124. }
  125. // VisitTypeBlockBody implements from api.BaseApiParserVisitor
  126. func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface{} {
  127. if ctx.TypeBlockAlias() != nil {
  128. return ctx.TypeBlockAlias().Accept(v).(*TypeAlias)
  129. }
  130. return ctx.TypeBlockStruct().Accept(v).(*TypeStruct)
  131. }
  132. // VisitTypeStruct implements from api.BaseApiParserVisitor
  133. func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{} {
  134. var st TypeStruct
  135. st.Name = v.newExprWithToken(ctx.GetStructName())
  136. if util.UnExport(ctx.GetStructName().GetText()) {
  137. }
  138. if ctx.GetStructToken() != nil {
  139. structExpr := v.newExprWithToken(ctx.GetStructToken())
  140. structTokenText := ctx.GetStructToken().GetText()
  141. if structTokenText != "struct" {
  142. v.panic(structExpr, fmt.Sprintf("expecting 'struct', found input '%s'", structTokenText))
  143. }
  144. if api.IsGolangKeyWord(structTokenText, "struct") {
  145. v.panic(structExpr, fmt.Sprintf("expecting 'struct', but found golang keyword '%s'", structTokenText))
  146. }
  147. st.Struct = structExpr
  148. }
  149. st.LBrace = v.newExprWithToken(ctx.GetLbrace())
  150. st.RBrace = v.newExprWithToken(ctx.GetRbrace())
  151. fields := ctx.AllField()
  152. for _, each := range fields {
  153. f := each.Accept(v)
  154. if f == nil {
  155. continue
  156. }
  157. st.Fields = append(st.Fields, f.(*TypeField))
  158. }
  159. return &st
  160. }
  161. // VisitTypeBlockStruct implements from api.BaseApiParserVisitor
  162. func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) interface{} {
  163. var st TypeStruct
  164. st.Name = v.newExprWithToken(ctx.GetStructName())
  165. if ctx.GetStructToken() != nil {
  166. structExpr := v.newExprWithToken(ctx.GetStructToken())
  167. structTokenText := ctx.GetStructToken().GetText()
  168. if structTokenText != "struct" {
  169. v.panic(structExpr, fmt.Sprintf("expecting 'struct', found imput '%s'", structTokenText))
  170. }
  171. if api.IsGolangKeyWord(structTokenText, "struct") {
  172. v.panic(structExpr, fmt.Sprintf("expecting 'struct', but found golang keyword '%s'", structTokenText))
  173. }
  174. st.Struct = structExpr
  175. }
  176. st.DocExpr = v.getDoc(ctx)
  177. st.LBrace = v.newExprWithToken(ctx.GetLbrace())
  178. st.RBrace = v.newExprWithToken(ctx.GetRbrace())
  179. fields := ctx.AllField()
  180. for _, each := range fields {
  181. f := each.Accept(v)
  182. if f == nil {
  183. continue
  184. }
  185. st.Fields = append(st.Fields, f.(*TypeField))
  186. }
  187. return &st
  188. }
  189. // VisitTypeBlockAlias implements from api.BaseApiParserVisitor
  190. func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interface{} {
  191. var alias TypeAlias
  192. alias.Name = v.newExprWithToken(ctx.GetAlias())
  193. alias.Assign = v.newExprWithToken(ctx.GetAssign())
  194. alias.DataType = ctx.DataType().Accept(v).(DataType)
  195. alias.DocExpr = v.getDoc(ctx)
  196. alias.CommentExpr = v.getComment(ctx)
  197. // todo: reopen if necessary
  198. v.panic(alias.Name, "unsupported alias")
  199. return &alias
  200. }
  201. // VisitTypeAlias implements from api.BaseApiParserVisitor
  202. func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{} {
  203. var alias TypeAlias
  204. alias.Name = v.newExprWithToken(ctx.GetAlias())
  205. alias.Assign = v.newExprWithToken(ctx.GetAssign())
  206. alias.DataType = ctx.DataType().Accept(v).(DataType)
  207. alias.DocExpr = v.getDoc(ctx)
  208. alias.CommentExpr = v.getComment(ctx)
  209. // todo: reopen if necessary
  210. v.panic(alias.Name, "unsupported alias")
  211. return &alias
  212. }
  213. // VisitField implements from api.BaseApiParserVisitor
  214. func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{} {
  215. iAnonymousFiled := ctx.AnonymousFiled()
  216. iNormalFieldContext := ctx.NormalField()
  217. if iAnonymousFiled != nil {
  218. return iAnonymousFiled.Accept(v).(*TypeField)
  219. }
  220. if iNormalFieldContext != nil {
  221. return iNormalFieldContext.Accept(v).(*TypeField)
  222. }
  223. return nil
  224. }
  225. // VisitNormalField implements from api.BaseApiParserVisitor
  226. func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{} {
  227. var field TypeField
  228. field.Name = v.newExprWithToken(ctx.GetFieldName())
  229. iDataTypeContext := ctx.DataType()
  230. if iDataTypeContext != nil {
  231. field.DataType = iDataTypeContext.Accept(v).(DataType)
  232. field.CommentExpr = v.getComment(ctx)
  233. }
  234. if ctx.GetTag() != nil {
  235. tagText := ctx.GetTag().GetText()
  236. tagExpr := v.newExprWithToken(ctx.GetTag())
  237. if !api.MatchTag(tagText) {
  238. v.panic(tagExpr, fmt.Sprintf("mismatched tag, found input '%s'", tagText))
  239. }
  240. field.Tag = tagExpr
  241. field.CommentExpr = v.getComment(ctx)
  242. }
  243. field.DocExpr = v.getDoc(ctx)
  244. return &field
  245. }
  246. // VisitAnonymousFiled implements from api.BaseApiParserVisitor
  247. func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interface{} {
  248. start := ctx.GetStart()
  249. stop := ctx.GetStop()
  250. var field TypeField
  251. field.IsAnonymous = true
  252. if ctx.GetStar() != nil {
  253. nameExpr := v.newExprWithTerminalNode(ctx.ID())
  254. field.DataType = &Pointer{
  255. PointerExpr: v.newExprWithText(ctx.GetStar().GetText()+ctx.ID().GetText(), start.GetLine(), start.GetColumn(), start.GetStart(), stop.GetStop()),
  256. Star: v.newExprWithToken(ctx.GetStar()),
  257. Name: nameExpr,
  258. }
  259. } else {
  260. nameExpr := v.newExprWithTerminalNode(ctx.ID())
  261. field.DataType = &Literal{Literal: nameExpr}
  262. }
  263. field.DocExpr = v.getDoc(ctx)
  264. field.CommentExpr = v.getComment(ctx)
  265. return &field
  266. }
  267. // VisitDataType implements from api.BaseApiParserVisitor
  268. func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{} {
  269. if ctx.ID() != nil {
  270. idExpr := v.newExprWithTerminalNode(ctx.ID())
  271. return &Literal{Literal: idExpr}
  272. }
  273. if ctx.MapType() != nil {
  274. t := ctx.MapType().Accept(v)
  275. return t
  276. }
  277. if ctx.ArrayType() != nil {
  278. return ctx.ArrayType().Accept(v)
  279. }
  280. if ctx.GetInter() != nil {
  281. return &Interface{Literal: v.newExprWithToken(ctx.GetInter())}
  282. }
  283. if ctx.GetTime() != nil {
  284. // todo: reopen if it is necessary
  285. timeExpr := v.newExprWithToken(ctx.GetTime())
  286. v.panic(timeExpr, "unsupported time.Time")
  287. return &Time{Literal: timeExpr}
  288. }
  289. if ctx.PointerType() != nil {
  290. return ctx.PointerType().Accept(v)
  291. }
  292. return ctx.TypeStruct().Accept(v)
  293. }
  294. // VisitPointerType implements from api.BaseApiParserVisitor
  295. func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{} {
  296. nameExpr := v.newExprWithTerminalNode(ctx.ID())
  297. return &Pointer{
  298. PointerExpr: v.newExprWithText(ctx.GetText(), ctx.GetStar().GetLine(), ctx.GetStar().GetColumn(), ctx.GetStar().GetStart(), ctx.ID().GetSymbol().GetStop()),
  299. Star: v.newExprWithToken(ctx.GetStar()),
  300. Name: nameExpr,
  301. }
  302. }
  303. // VisitMapType implements from api.BaseApiParserVisitor
  304. func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{} {
  305. return &Map{
  306. MapExpr: v.newExprWithText(ctx.GetText(), ctx.GetMapToken().GetLine(), ctx.GetMapToken().GetColumn(),
  307. ctx.GetMapToken().GetStart(), ctx.GetValue().GetStop().GetStop()),
  308. Map: v.newExprWithToken(ctx.GetMapToken()),
  309. LBrack: v.newExprWithToken(ctx.GetLbrack()),
  310. RBrack: v.newExprWithToken(ctx.GetRbrack()),
  311. Key: v.newExprWithToken(ctx.GetKey()),
  312. Value: ctx.GetValue().Accept(v).(DataType),
  313. }
  314. }
  315. // VisitArrayType implements from api.BaseApiParserVisitor
  316. func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{} {
  317. return &Array{
  318. ArrayExpr: v.newExprWithText(ctx.GetText(), ctx.GetLbrack().GetLine(), ctx.GetLbrack().GetColumn(), ctx.GetLbrack().GetStart(), ctx.DataType().GetStop().GetStop()),
  319. LBrack: v.newExprWithToken(ctx.GetLbrack()),
  320. RBrack: v.newExprWithToken(ctx.GetRbrack()),
  321. Literal: ctx.DataType().Accept(v).(DataType),
  322. }
  323. }
  324. // NameExpr returns the expression string of TypeAlias
  325. func (a *TypeAlias) NameExpr() Expr {
  326. return a.Name
  327. }
  328. // Doc returns the document of TypeAlias, like // some text
  329. func (a *TypeAlias) Doc() []Expr {
  330. return a.DocExpr
  331. }
  332. // Comment returns the comment of TypeAlias, like // some text
  333. func (a *TypeAlias) Comment() Expr {
  334. return a.CommentExpr
  335. }
  336. // Format provides a formatter for api command, now nothing to do
  337. func (a *TypeAlias) Format() error {
  338. return nil
  339. }
  340. // Equal compares whether the element literals in two TypeAlias are equal
  341. func (a *TypeAlias) Equal(v interface{}) bool {
  342. if v == nil {
  343. return false
  344. }
  345. alias := v.(*TypeAlias)
  346. if !a.Name.Equal(alias.Name) {
  347. return false
  348. }
  349. if !a.Assign.Equal(alias.Assign) {
  350. return false
  351. }
  352. if !a.DataType.Equal(alias.DataType) {
  353. return false
  354. }
  355. return EqualDoc(a, alias)
  356. }
  357. // Expr returns the expression string of Literal
  358. func (l *Literal) Expr() Expr {
  359. return l.Literal
  360. }
  361. // Format provides a formatter for api command, now nothing to do
  362. func (l *Literal) Format() error {
  363. // todo
  364. return nil
  365. }
  366. // Equal compares whether the element literals in two Literal are equal
  367. func (l *Literal) Equal(dt DataType) bool {
  368. if dt == nil {
  369. return false
  370. }
  371. v, ok := dt.(*Literal)
  372. if !ok {
  373. return false
  374. }
  375. return l.Literal.Equal(v.Literal)
  376. }
  377. // IsNotNil returns whether the instance is nil or not
  378. func (l *Literal) IsNotNil() bool {
  379. return l != nil
  380. }
  381. // Expr returns the expression string of Interface
  382. func (i *Interface) Expr() Expr {
  383. return i.Literal
  384. }
  385. // Format provides a formatter for api command, now nothing to do
  386. func (i *Interface) Format() error {
  387. // todo
  388. return nil
  389. }
  390. // Equal compares whether the element literals in two Interface are equal
  391. func (i *Interface) Equal(dt DataType) bool {
  392. if dt == nil {
  393. return false
  394. }
  395. v, ok := dt.(*Interface)
  396. if !ok {
  397. return false
  398. }
  399. return i.Literal.Equal(v.Literal)
  400. }
  401. // IsNotNil returns whether the instance is nil or not
  402. func (i *Interface) IsNotNil() bool {
  403. return i != nil
  404. }
  405. // Expr returns the expression string of Map
  406. func (m *Map) Expr() Expr {
  407. return m.MapExpr
  408. }
  409. // Format provides a formatter for api command, now nothing to do
  410. func (m *Map) Format() error {
  411. // todo
  412. return nil
  413. }
  414. // Equal compares whether the element literals in two Map are equal
  415. func (m *Map) Equal(dt DataType) bool {
  416. if dt == nil {
  417. return false
  418. }
  419. v, ok := dt.(*Map)
  420. if !ok {
  421. return false
  422. }
  423. if !m.Key.Equal(v.Key) {
  424. return false
  425. }
  426. if !m.Value.Equal(v.Value) {
  427. return false
  428. }
  429. if !m.MapExpr.Equal(v.MapExpr) {
  430. return false
  431. }
  432. return m.Map.Equal(v.Map)
  433. }
  434. // IsNotNil returns whether the instance is nil or not
  435. func (m *Map) IsNotNil() bool {
  436. return m != nil
  437. }
  438. // Expr returns the expression string of Array
  439. func (a *Array) Expr() Expr {
  440. return a.ArrayExpr
  441. }
  442. // Format provides a formatter for api command, now nothing to do
  443. func (a *Array) Format() error {
  444. // todo
  445. return nil
  446. }
  447. // Equal compares whether the element literals in two Array are equal
  448. func (a *Array) Equal(dt DataType) bool {
  449. if dt == nil {
  450. return false
  451. }
  452. v, ok := dt.(*Array)
  453. if !ok {
  454. return false
  455. }
  456. if !a.ArrayExpr.Equal(v.ArrayExpr) {
  457. return false
  458. }
  459. return a.Literal.Equal(v.Literal)
  460. }
  461. // IsNotNil returns whether the instance is nil or not
  462. func (a *Array) IsNotNil() bool {
  463. return a != nil
  464. }
  465. // Expr returns the expression string of Time
  466. func (t *Time) Expr() Expr {
  467. return t.Literal
  468. }
  469. // Format provides a formatter for api command, now nothing to do
  470. func (t *Time) Format() error {
  471. // todo
  472. return nil
  473. }
  474. // Equal compares whether the element literals in two Time are equal
  475. func (t *Time) Equal(dt DataType) bool {
  476. if dt == nil {
  477. return false
  478. }
  479. v, ok := dt.(*Time)
  480. if !ok {
  481. return false
  482. }
  483. return t.Literal.Equal(v.Literal)
  484. }
  485. // IsNotNil returns whether the instance is nil or not
  486. func (t *Time) IsNotNil() bool {
  487. return t != nil
  488. }
  489. // Expr returns the expression string of Pointer
  490. func (p *Pointer) Expr() Expr {
  491. return p.PointerExpr
  492. }
  493. // Format provides a formatter for api command, now nothing to do
  494. func (p *Pointer) Format() error {
  495. return nil
  496. }
  497. // Equal compares whether the element literals in two Pointer are equal
  498. func (p *Pointer) Equal(dt DataType) bool {
  499. if dt == nil {
  500. return false
  501. }
  502. v, ok := dt.(*Pointer)
  503. if !ok {
  504. return false
  505. }
  506. if !p.PointerExpr.Equal(v.PointerExpr) {
  507. return false
  508. }
  509. if !p.Star.Equal(v.Star) {
  510. return false
  511. }
  512. return p.Name.Equal(v.Name)
  513. }
  514. // IsNotNil returns whether the instance is nil or not
  515. func (p *Pointer) IsNotNil() bool {
  516. return p != nil
  517. }
  518. // NameExpr returns the expression string of TypeStruct
  519. func (s *TypeStruct) NameExpr() Expr {
  520. return s.Name
  521. }
  522. // Equal compares whether the element literals in two TypeStruct are equal
  523. func (s *TypeStruct) Equal(dt interface{}) bool {
  524. if dt == nil {
  525. return false
  526. }
  527. v, ok := dt.(*TypeStruct)
  528. if !ok {
  529. return false
  530. }
  531. if !s.Name.Equal(v.Name) {
  532. return false
  533. }
  534. var expectDoc, actualDoc []Expr
  535. expectDoc = append(expectDoc, s.DocExpr...)
  536. actualDoc = append(actualDoc, v.DocExpr...)
  537. sort.Slice(expectDoc, func(i, j int) bool {
  538. return expectDoc[i].Line() < expectDoc[j].Line()
  539. })
  540. for index, each := range actualDoc {
  541. if !each.Equal(actualDoc[index]) {
  542. return false
  543. }
  544. }
  545. if s.Struct != nil {
  546. if s.Struct != nil {
  547. if !s.Struct.Equal(v.Struct) {
  548. return false
  549. }
  550. }
  551. }
  552. if len(s.Fields) != len(v.Fields) {
  553. return false
  554. }
  555. var expected, acual []*TypeField
  556. expected = append(expected, s.Fields...)
  557. acual = append(acual, v.Fields...)
  558. sort.Slice(expected, func(i, j int) bool {
  559. return expected[i].DataType.Expr().Line() < expected[j].DataType.Expr().Line()
  560. })
  561. sort.Slice(acual, func(i, j int) bool {
  562. return acual[i].DataType.Expr().Line() < acual[j].DataType.Expr().Line()
  563. })
  564. for index, each := range expected {
  565. ac := acual[index]
  566. if !each.Equal(ac) {
  567. return false
  568. }
  569. }
  570. return true
  571. }
  572. // Doc returns the document of TypeStruct, like // some text
  573. func (s *TypeStruct) Doc() []Expr {
  574. return s.DocExpr
  575. }
  576. // Format provides a formatter for api command, now nothing to do
  577. func (s *TypeStruct) Format() error {
  578. // todo
  579. return nil
  580. }
  581. // Equal compares whether the element literals in two TypeField are equal
  582. func (t *TypeField) Equal(v interface{}) bool {
  583. if v == nil {
  584. return false
  585. }
  586. f, ok := v.(*TypeField)
  587. if !ok {
  588. return false
  589. }
  590. if t.IsAnonymous != f.IsAnonymous {
  591. return false
  592. }
  593. if !t.DataType.Equal(f.DataType) {
  594. return false
  595. }
  596. if !t.IsAnonymous {
  597. if !t.Name.Equal(f.Name) {
  598. return false
  599. }
  600. if t.Tag != nil {
  601. if !t.Tag.Equal(f.Tag) {
  602. return false
  603. }
  604. }
  605. }
  606. return EqualDoc(t, f)
  607. }
  608. // Doc returns the document of TypeField, like // some text
  609. func (t *TypeField) Doc() []Expr {
  610. return t.DocExpr
  611. }
  612. // Comment returns the comment of TypeField, like // some text
  613. func (t *TypeField) Comment() Expr {
  614. return t.CommentExpr
  615. }
  616. // Format provides a formatter for api command, now nothing to do
  617. func (t *TypeField) Format() error {
  618. // todo
  619. return nil
  620. }