equal.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. /*
  29. The equal plugin generates an Equal and a VerboseEqual method for each message.
  30. These equal methods are quite obvious.
  31. The only difference is that VerboseEqual returns a non nil error if it is not equal.
  32. This error contains more detail on exactly which part of the message was not equal to the other message.
  33. The idea is that this is useful for debugging.
  34. Equal is enabled using the following extensions:
  35. - equal
  36. - equal_all
  37. While VerboseEqual is enable dusing the following extensions:
  38. - verbose_equal
  39. - verbose_equal_all
  40. The equal plugin also generates a test given it is enabled using one of the following extensions:
  41. - testgen
  42. - testgen_all
  43. Let us look at:
  44. github.com/gogo/protobuf/test/example/example.proto
  45. Btw all the output can be seen at:
  46. github.com/gogo/protobuf/test/example/*
  47. The following message:
  48. option (gogoproto.equal_all) = true;
  49. option (gogoproto.verbose_equal_all) = true;
  50. message B {
  51. optional A A = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
  52. repeated bytes G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false];
  53. }
  54. given to the equal plugin, will generate the following code:
  55. func (this *B) VerboseEqual(that interface{}) error {
  56. if that == nil {
  57. if this == nil {
  58. return nil
  59. }
  60. return fmt2.Errorf("that == nil && this != nil")
  61. }
  62. that1, ok := that.(*B)
  63. if !ok {
  64. return fmt2.Errorf("that is not of type *B")
  65. }
  66. if that1 == nil {
  67. if this == nil {
  68. return nil
  69. }
  70. return fmt2.Errorf("that is type *B but is nil && this != nil")
  71. } else if this == nil {
  72. return fmt2.Errorf("that is type *B but is not nil && this == nil")
  73. }
  74. if !this.A.Equal(&that1.A) {
  75. return fmt2.Errorf("A this(%v) Not Equal that(%v)", this.A, that1.A)
  76. }
  77. if len(this.G) != len(that1.G) {
  78. return fmt2.Errorf("G this(%v) Not Equal that(%v)", len(this.G), len(that1.G))
  79. }
  80. for i := range this.G {
  81. if !this.G[i].Equal(that1.G[i]) {
  82. return fmt2.Errorf("G this[%v](%v) Not Equal that[%v](%v)", i, this.G[i], i, that1.G[i])
  83. }
  84. }
  85. if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
  86. return fmt2.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized)
  87. }
  88. return nil
  89. }
  90. func (this *B) Equal(that interface{}) bool {
  91. if that == nil {
  92. if this == nil {
  93. return true
  94. }
  95. return false
  96. }
  97. that1, ok := that.(*B)
  98. if !ok {
  99. return false
  100. }
  101. if that1 == nil {
  102. if this == nil {
  103. return true
  104. }
  105. return false
  106. } else if this == nil {
  107. return false
  108. }
  109. if !this.A.Equal(&that1.A) {
  110. return false
  111. }
  112. if len(this.G) != len(that1.G) {
  113. return false
  114. }
  115. for i := range this.G {
  116. if !this.G[i].Equal(that1.G[i]) {
  117. return false
  118. }
  119. }
  120. if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
  121. return false
  122. }
  123. return true
  124. }
  125. and the following test code:
  126. func TestBVerboseEqual(t *testing8.T) {
  127. popr := math_rand8.New(math_rand8.NewSource(time8.Now().UnixNano()))
  128. p := NewPopulatedB(popr, false)
  129. dAtA, err := github_com_gogo_protobuf_proto2.Marshal(p)
  130. if err != nil {
  131. panic(err)
  132. }
  133. msg := &B{}
  134. if err := github_com_gogo_protobuf_proto2.Unmarshal(dAtA, msg); err != nil {
  135. panic(err)
  136. }
  137. if err := p.VerboseEqual(msg); err != nil {
  138. t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
  139. }
  140. */
  141. package equal
  142. import (
  143. "github.com/gogo/protobuf/gogoproto"
  144. "github.com/gogo/protobuf/proto"
  145. descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
  146. "github.com/gogo/protobuf/protoc-gen-gogo/generator"
  147. "github.com/gogo/protobuf/vanity"
  148. )
  149. type plugin struct {
  150. *generator.Generator
  151. generator.PluginImports
  152. fmtPkg generator.Single
  153. bytesPkg generator.Single
  154. protoPkg generator.Single
  155. }
  156. func NewPlugin() *plugin {
  157. return &plugin{}
  158. }
  159. func (p *plugin) Name() string {
  160. return "equal"
  161. }
  162. func (p *plugin) Init(g *generator.Generator) {
  163. p.Generator = g
  164. }
  165. func (p *plugin) Generate(file *generator.FileDescriptor) {
  166. p.PluginImports = generator.NewPluginImports(p.Generator)
  167. p.fmtPkg = p.NewImport("fmt")
  168. p.bytesPkg = p.NewImport("bytes")
  169. p.protoPkg = p.NewImport("github.com/gogo/protobuf/proto")
  170. for _, msg := range file.Messages() {
  171. if msg.DescriptorProto.GetOptions().GetMapEntry() {
  172. continue
  173. }
  174. if gogoproto.HasVerboseEqual(file.FileDescriptorProto, msg.DescriptorProto) {
  175. p.generateMessage(file, msg, true)
  176. }
  177. if gogoproto.HasEqual(file.FileDescriptorProto, msg.DescriptorProto) {
  178. p.generateMessage(file, msg, false)
  179. }
  180. }
  181. }
  182. func (p *plugin) generateNullableField(fieldname string, verbose bool) {
  183. p.P(`if this.`, fieldname, ` != nil && that1.`, fieldname, ` != nil {`)
  184. p.In()
  185. p.P(`if *this.`, fieldname, ` != *that1.`, fieldname, `{`)
  186. p.In()
  187. if verbose {
  188. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", *this.`, fieldname, `, *that1.`, fieldname, `)`)
  189. } else {
  190. p.P(`return false`)
  191. }
  192. p.Out()
  193. p.P(`}`)
  194. p.Out()
  195. p.P(`} else if this.`, fieldname, ` != nil {`)
  196. p.In()
  197. if verbose {
  198. p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` == nil && that.`, fieldname, ` != nil")`)
  199. } else {
  200. p.P(`return false`)
  201. }
  202. p.Out()
  203. p.P(`} else if that1.`, fieldname, ` != nil {`)
  204. }
  205. func (p *plugin) generateMsgNullAndTypeCheck(ccTypeName string, verbose bool) {
  206. p.P(`if that == nil {`)
  207. p.In()
  208. p.P(`if this == nil {`)
  209. p.In()
  210. if verbose {
  211. p.P(`return nil`)
  212. } else {
  213. p.P(`return true`)
  214. }
  215. p.Out()
  216. p.P(`}`)
  217. if verbose {
  218. p.P(`return `, p.fmtPkg.Use(), `.Errorf("that == nil && this != nil")`)
  219. } else {
  220. p.P(`return false`)
  221. }
  222. p.Out()
  223. p.P(`}`)
  224. p.P(``)
  225. p.P(`that1, ok := that.(*`, ccTypeName, `)`)
  226. p.P(`if !ok {`)
  227. p.In()
  228. p.P(`that2, ok := that.(`, ccTypeName, `)`)
  229. p.P(`if ok {`)
  230. p.In()
  231. p.P(`that1 = &that2`)
  232. p.Out()
  233. p.P(`} else {`)
  234. p.In()
  235. if verbose {
  236. p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is not of type *`, ccTypeName, `")`)
  237. } else {
  238. p.P(`return false`)
  239. }
  240. p.Out()
  241. p.P(`}`)
  242. p.Out()
  243. p.P(`}`)
  244. p.P(`if that1 == nil {`)
  245. p.In()
  246. p.P(`if this == nil {`)
  247. p.In()
  248. if verbose {
  249. p.P(`return nil`)
  250. } else {
  251. p.P(`return true`)
  252. }
  253. p.Out()
  254. p.P(`}`)
  255. if verbose {
  256. p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is type *`, ccTypeName, ` but is nil && this != nil")`)
  257. } else {
  258. p.P(`return false`)
  259. }
  260. p.Out()
  261. p.P(`} else if this == nil {`)
  262. p.In()
  263. if verbose {
  264. p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is type *`, ccTypeName, ` but is not nil && this == nil")`)
  265. } else {
  266. p.P(`return false`)
  267. }
  268. p.Out()
  269. p.P(`}`)
  270. }
  271. func (p *plugin) generateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto, verbose bool) {
  272. proto3 := gogoproto.IsProto3(file.FileDescriptorProto)
  273. fieldname := p.GetOneOfFieldName(message, field)
  274. repeated := field.IsRepeated()
  275. ctype := gogoproto.IsCustomType(field)
  276. nullable := gogoproto.IsNullable(field)
  277. isDuration := gogoproto.IsStdDuration(field)
  278. isTimestamp := gogoproto.IsStdTime(field)
  279. // oneof := field.OneofIndex != nil
  280. if !repeated {
  281. if ctype || isTimestamp {
  282. if nullable {
  283. p.P(`if that1.`, fieldname, ` == nil {`)
  284. p.In()
  285. p.P(`if this.`, fieldname, ` != nil {`)
  286. p.In()
  287. if verbose {
  288. p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` != nil && that1.`, fieldname, ` == nil")`)
  289. } else {
  290. p.P(`return false`)
  291. }
  292. p.Out()
  293. p.P(`}`)
  294. p.Out()
  295. p.P(`} else if !this.`, fieldname, `.Equal(*that1.`, fieldname, `) {`)
  296. } else {
  297. p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`)
  298. }
  299. p.In()
  300. if verbose {
  301. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
  302. } else {
  303. p.P(`return false`)
  304. }
  305. p.Out()
  306. p.P(`}`)
  307. } else if isDuration {
  308. if nullable {
  309. p.generateNullableField(fieldname, verbose)
  310. } else {
  311. p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`)
  312. }
  313. p.In()
  314. if verbose {
  315. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
  316. } else {
  317. p.P(`return false`)
  318. }
  319. p.Out()
  320. p.P(`}`)
  321. } else {
  322. if field.IsMessage() || p.IsGroup(field) {
  323. if nullable {
  324. p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`)
  325. } else {
  326. p.P(`if !this.`, fieldname, `.Equal(&that1.`, fieldname, `) {`)
  327. }
  328. } else if field.IsBytes() {
  329. p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
  330. } else if field.IsString() {
  331. if nullable && !proto3 {
  332. p.generateNullableField(fieldname, verbose)
  333. } else {
  334. p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`)
  335. }
  336. } else {
  337. if nullable && !proto3 {
  338. p.generateNullableField(fieldname, verbose)
  339. } else {
  340. p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`)
  341. }
  342. }
  343. p.In()
  344. if verbose {
  345. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
  346. } else {
  347. p.P(`return false`)
  348. }
  349. p.Out()
  350. p.P(`}`)
  351. }
  352. } else {
  353. p.P(`if len(this.`, fieldname, `) != len(that1.`, fieldname, `) {`)
  354. p.In()
  355. if verbose {
  356. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", len(this.`, fieldname, `), len(that1.`, fieldname, `))`)
  357. } else {
  358. p.P(`return false`)
  359. }
  360. p.Out()
  361. p.P(`}`)
  362. p.P(`for i := range this.`, fieldname, ` {`)
  363. p.In()
  364. if ctype && !p.IsMap(field) {
  365. p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`)
  366. } else if isTimestamp {
  367. if nullable {
  368. p.P(`if !this.`, fieldname, `[i].Equal(*that1.`, fieldname, `[i]) {`)
  369. } else {
  370. p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`)
  371. }
  372. } else if isDuration {
  373. if nullable {
  374. p.P(`if dthis, dthat := this.`, fieldname, `[i], that1.`, fieldname, `[i]; (dthis != nil && dthat != nil && *dthis != *dthat) || (dthis != nil && dthat == nil) || (dthis == nil && dthat != nil) {`)
  375. } else {
  376. p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
  377. }
  378. } else {
  379. if p.IsMap(field) {
  380. m := p.GoMapType(nil, field)
  381. valuegoTyp, _ := p.GoType(nil, m.ValueField)
  382. valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField)
  383. nullable, valuegoTyp, valuegoAliasTyp = generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp)
  384. mapValue := m.ValueAliasField
  385. if mapValue.IsMessage() || p.IsGroup(mapValue) {
  386. if nullable && valuegoTyp == valuegoAliasTyp {
  387. p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`)
  388. } else {
  389. // Equal() has a pointer receiver, but map value is a value type
  390. a := `this.` + fieldname + `[i]`
  391. b := `that1.` + fieldname + `[i]`
  392. if valuegoTyp != valuegoAliasTyp {
  393. // cast back to the type that has the generated methods on it
  394. a = `(` + valuegoTyp + `)(` + a + `)`
  395. b = `(` + valuegoTyp + `)(` + b + `)`
  396. }
  397. p.P(`a := `, a)
  398. p.P(`b := `, b)
  399. if nullable {
  400. p.P(`if !a.Equal(b) {`)
  401. } else {
  402. p.P(`if !(&a).Equal(&b) {`)
  403. }
  404. }
  405. } else if mapValue.IsBytes() {
  406. if ctype {
  407. if nullable {
  408. p.P(`if !this.`, fieldname, `[i].Equal(*that1.`, fieldname, `[i]) { //nullable`)
  409. } else {
  410. p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) { //not nullable`)
  411. }
  412. } else {
  413. p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`)
  414. }
  415. } else if mapValue.IsString() {
  416. p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
  417. } else {
  418. p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
  419. }
  420. } else if field.IsMessage() || p.IsGroup(field) {
  421. if nullable {
  422. p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`)
  423. } else {
  424. p.P(`if !this.`, fieldname, `[i].Equal(&that1.`, fieldname, `[i]) {`)
  425. }
  426. } else if field.IsBytes() {
  427. p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`)
  428. } else if field.IsString() {
  429. p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
  430. } else {
  431. p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
  432. }
  433. }
  434. p.In()
  435. if verbose {
  436. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", i, this.`, fieldname, `[i], i, that1.`, fieldname, `[i])`)
  437. } else {
  438. p.P(`return false`)
  439. }
  440. p.Out()
  441. p.P(`}`)
  442. p.Out()
  443. p.P(`}`)
  444. }
  445. }
  446. func (p *plugin) generateMessage(file *generator.FileDescriptor, message *generator.Descriptor, verbose bool) {
  447. ccTypeName := generator.CamelCaseSlice(message.TypeName())
  448. if verbose {
  449. p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
  450. } else {
  451. p.P(`func (this *`, ccTypeName, `) Equal(that interface{}) bool {`)
  452. }
  453. p.In()
  454. p.generateMsgNullAndTypeCheck(ccTypeName, verbose)
  455. oneofs := make(map[string]struct{})
  456. for _, field := range message.Field {
  457. oneof := field.OneofIndex != nil
  458. if oneof {
  459. fieldname := p.GetFieldName(message, field)
  460. if _, ok := oneofs[fieldname]; ok {
  461. continue
  462. } else {
  463. oneofs[fieldname] = struct{}{}
  464. }
  465. p.P(`if that1.`, fieldname, ` == nil {`)
  466. p.In()
  467. p.P(`if this.`, fieldname, ` != nil {`)
  468. p.In()
  469. if verbose {
  470. p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` != nil && that1.`, fieldname, ` == nil")`)
  471. } else {
  472. p.P(`return false`)
  473. }
  474. p.Out()
  475. p.P(`}`)
  476. p.Out()
  477. p.P(`} else if this.`, fieldname, ` == nil {`)
  478. p.In()
  479. if verbose {
  480. p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` == nil && that1.`, fieldname, ` != nil")`)
  481. } else {
  482. p.P(`return false`)
  483. }
  484. p.Out()
  485. if verbose {
  486. p.P(`} else if err := this.`, fieldname, `.VerboseEqual(that1.`, fieldname, `); err != nil {`)
  487. } else {
  488. p.P(`} else if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`)
  489. }
  490. p.In()
  491. if verbose {
  492. p.P(`return err`)
  493. } else {
  494. p.P(`return false`)
  495. }
  496. p.Out()
  497. p.P(`}`)
  498. } else {
  499. p.generateField(file, message, field, verbose)
  500. }
  501. }
  502. if message.DescriptorProto.HasExtension() {
  503. if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) {
  504. fieldname := "XXX_InternalExtensions"
  505. p.P(`thismap := `, p.protoPkg.Use(), `.GetUnsafeExtensionsMap(this)`)
  506. p.P(`thatmap := `, p.protoPkg.Use(), `.GetUnsafeExtensionsMap(that1)`)
  507. p.P(`for k, v := range thismap {`)
  508. p.In()
  509. p.P(`if v2, ok := thatmap[k]; ok {`)
  510. p.In()
  511. p.P(`if !v.Equal(&v2) {`)
  512. p.In()
  513. if verbose {
  514. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", k, thismap[k], k, thatmap[k])`)
  515. } else {
  516. p.P(`return false`)
  517. }
  518. p.Out()
  519. p.P(`}`)
  520. p.Out()
  521. p.P(`} else {`)
  522. p.In()
  523. if verbose {
  524. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, `[%v] Not In that", k)`)
  525. } else {
  526. p.P(`return false`)
  527. }
  528. p.Out()
  529. p.P(`}`)
  530. p.Out()
  531. p.P(`}`)
  532. p.P(`for k, _ := range thatmap {`)
  533. p.In()
  534. p.P(`if _, ok := thismap[k]; !ok {`)
  535. p.In()
  536. if verbose {
  537. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, `[%v] Not In this", k)`)
  538. } else {
  539. p.P(`return false`)
  540. }
  541. p.Out()
  542. p.P(`}`)
  543. p.Out()
  544. p.P(`}`)
  545. } else {
  546. fieldname := "XXX_extensions"
  547. p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
  548. p.In()
  549. if verbose {
  550. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
  551. } else {
  552. p.P(`return false`)
  553. }
  554. p.Out()
  555. p.P(`}`)
  556. }
  557. }
  558. if gogoproto.HasUnrecognized(file.FileDescriptorProto, message.DescriptorProto) {
  559. fieldname := "XXX_unrecognized"
  560. p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
  561. p.In()
  562. if verbose {
  563. p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
  564. } else {
  565. p.P(`return false`)
  566. }
  567. p.Out()
  568. p.P(`}`)
  569. }
  570. if verbose {
  571. p.P(`return nil`)
  572. } else {
  573. p.P(`return true`)
  574. }
  575. p.Out()
  576. p.P(`}`)
  577. //Generate Equal methods for oneof fields
  578. m := proto.Clone(message.DescriptorProto).(*descriptor.DescriptorProto)
  579. for _, field := range m.Field {
  580. oneof := field.OneofIndex != nil
  581. if !oneof {
  582. continue
  583. }
  584. ccTypeName := p.OneOfTypeName(message, field)
  585. if verbose {
  586. p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
  587. } else {
  588. p.P(`func (this *`, ccTypeName, `) Equal(that interface{}) bool {`)
  589. }
  590. p.In()
  591. p.generateMsgNullAndTypeCheck(ccTypeName, verbose)
  592. vanity.TurnOffNullableForNativeTypes(field)
  593. p.generateField(file, message, field, verbose)
  594. if verbose {
  595. p.P(`return nil`)
  596. } else {
  597. p.P(`return true`)
  598. }
  599. p.Out()
  600. p.P(`}`)
  601. }
  602. }
  603. func init() {
  604. generator.RegisterPlugin(NewPlugin())
  605. }