protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Known Issue: unknown behaviour for proto.Equal

Open awalterschulze opened this issue 11 years ago • 5 comments

From awalterschulze on May 07, 2014 21:17:58

proto.Equal(a, b Message) bool has not been tested with all gogoprotobuf scenarios.

If you are using this please comment so I can up the priority.

Original issue: http://code.google.com/p/gogoprotobuf/issues/detail?id=13

awalterschulze avatar Dec 03 '14 12:12 awalterschulze

This should be reported in another issue, but you could check out in test/thetest.proto

message NidRepCustom {
    repeated bytes Id = 1 [(gogoproto.customtype) = "Uuid", (gogoproto.nullable) = false];
    repeated bytes Value = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false];
}

The equal method does the following

func (uuid Uuid) Equal(other Uuid) bool {
    return bytes.Equal(uuid[0:], other[0:])
}

Does this help?

awalterschulze avatar Dec 04 '15 13:12 awalterschulze

I've run into problems with proto.Equal() and a proto where we use gogoproto.customtype for a bytes field. I hope this is the right place to report.

This is the proto: https://github.com/cockroachdb/cockroach/blob/befea82/pkg/roachpb/metadata.proto#L73

And the error is:

panic: interface conversion: interface {} is roachpb.RKey, not []uint8

It's because of this special handling of bytes fields in proto.Equal: https://github.com/golang/protobuf/blob/ef00c02/proto/equal.go#L218

Any ideas for a work-around?

andreimatei avatar Apr 12 '17 16:04 andreimatei

You are welcome to contribute a fix with a test, but an easier fix might be to just generate equal methods. Generating equal methods can be done in two ways:

  1. Using the gogoprotobuf equal extension https://godoc.org/github.com/gogo/protobuf/plugin/equal
  2. Using my new project https://github.com/awalterschulze/goderive

awalterschulze avatar Apr 12 '17 17:04 awalterschulze

Thanks! The equal extension looks good! But I've run into another issue with it, reported in #283.

andreimatei avatar Apr 12 '17 18:04 andreimatei

When using a customtype that is an array proto.Equal does not work, outputs log message proto: don't know how to compare ....

The switch at https://github.com/gogo/protobuf/blob/5628607bb4c51c3157aacc3a50f0ab707582b805/proto/equal.go#L151 does not appear to handle reflect.Array case which explains why this does not work.

Before digging into the code I also tried defining Equal func for my customtype since I was expecting that it may be used, but proto.Equal does not appear to try to use it at all.

tigrannajaryan avatar May 29 '20 00:05 tigrannajaryan