equal

package
v5.5.2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

The equal plugin generates an Equal and a VerboseEqual method for each message. These equal methods are quite obvious. The only difference is that VerboseEqual returns a non nil error if it is not equal. This error contains more detail on exactly which part of the message was not equal to the other message. The idea is that this is useful for debugging.

Equal is enabled using the following extensions:

  • equal
  • equal_all

While VerboseEqual is enable dusing the following extensions:

  • verbose_equal
  • verbose_equal_all

The equal plugin also generates a test given it is enabled using one of the following extensions:

  • testgen
  • testgen_all

Let us look at:

github.com/gogo/protobuf/test/example/example.proto

Btw all the output can be seen at:

github.com/gogo/protobuf/test/example/*

The following message:

  option (gogoproto.equal_all) = true;
  option (gogoproto.verbose_equal_all) = true;

  message B {
	optional A A = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
	repeated bytes G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false];
  }

given to the equal plugin, will generate the following code:

func (this *B) VerboseEqual(that interface{}) error {
	if that == nil {
		if this == nil {
			return nil
		}
		return fmt2.Errorf("that == nil && this != nil")
	}

	that1, ok := that.(*B)
	if !ok {
		return fmt2.Errorf("that is not of type *B")
	}
	if that1 == nil {
		if this == nil {
			return nil
		}
		return fmt2.Errorf("that is type *B but is nil && this != nil")
	} else if this == nil {
		return fmt2.Errorf("that is type *B but is not nil && this == nil")
	}
	if !this.A.Equal(&that1.A) {
		return fmt2.Errorf("A this(%v) Not Equal that(%v)", this.A, that1.A)
	}
	if len(this.G) != len(that1.G) {
		return fmt2.Errorf("G this(%v) Not Equal that(%v)", len(this.G), len(that1.G))
	}
	for i := range this.G {
		if !this.G[i].Equal(that1.G[i]) {
			return fmt2.Errorf("G this[%v](%v) Not Equal that[%v](%v)", i, this.G[i], i, that1.G[i])
		}
	}
	if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
		return fmt2.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized)
	}
	return nil
}

func (this *B) Equal(that interface{}) bool {
	if that == nil {
		if this == nil {
			return true
		}
		return false
	}

	that1, ok := that.(*B)
	if !ok {
		return false
	}
	if that1 == nil {
		if this == nil {
			return true
		}
		return false
	} else if this == nil {
		return false
	}
	if !this.A.Equal(&that1.A) {
		return false
	}
	if len(this.G) != len(that1.G) {
		return false
	}
	for i := range this.G {
		if !this.G[i].Equal(that1.G[i]) {
			return false
		}
	}
	if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
		return false
	}
	return true
}

and the following test code:

func TestBVerboseEqual(t *testing8.T) {
	popr := math_rand8.New(math_rand8.NewSource(time8.Now().UnixNano()))
	p := NewPopulatedB(popr, false)
	dAtA, err := github_com_gogo_protobuf_proto2.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &B{}
	if err := github_com_gogo_protobuf_proto2.Unmarshal(dAtA, msg); err != nil {
		panic(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPlugin

func NewPlugin() *plugin

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL