protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

protoc-gen-go: generated Equal method

Open glenjamin opened this issue 3 years ago • 5 comments

Is your feature request related to a problem? Please describe. Although https://godoc.org/google.golang.org/protobuf/testing/protocmp#Transform provides a custom handler for go-cmp, it has to be explicitly specified every time a comparison is performed

Describe the solution you'd like It would be preferrable if no custom transformer was required to compare protobuf structs.

This can be achieved by implementing the Equal method in the generated code, which would be used by go-cmp automatically.

• Types that have an Equal method may use that method to determine equality. This allows package authors to determine the equality operation for the types that they define. 

Describe alternatives you've considered I've also suggested that go-cmp allow global options https://github.com/google/go-cmp/issues/241

Additional context n/a

glenjamin avatar Nov 04 '20 15:11 glenjamin

As with almost all other proposals to generate some helper method, the main concerns are binary bloat and increased API surface for generated code.

dsnet avatar Nov 04 '20 18:11 dsnet

Putting the question of extra generated code to one side for a moment;

Is there a recommended way to compare protobuf types for value-equality?

glenjamin avatar Nov 08 '20 08:11 glenjamin

In production code, using proto.Equal is recommended for its speed.

In test code, using cmp is recommended with cmp.Comparer(proto.Equal) or protocmp.Transform for its flexibility.

dsnet avatar Nov 08 '20 08:11 dsnet

Because go makes no requirement for all methods on a type to be defined in the same file, one can always extend a generated message type with hand-written files in the same package defining any additional methods if you need them. In this case, because such a method can just delegate to proto.Equal it's not like it's there's a huge maintenance burden associated with doing that. That said, if you need to do it for a large or changing set of message types it could become more troublesome without support from the generator.

adam-azarchs avatar Jan 22 '21 04:01 adam-azarchs

I wrote protoc-gen-go-equal to generate Equal messages for different project. Maybe it will fit you too.

melias122 avatar Dec 29 '22 20:12 melias122