golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.79k stars 1.58k forks source link

protocmp: add SortSlices option #1651

Closed ash2k closed 1 month ago

ash2k commented 1 month ago

Is your feature request related to a problem? Please describe.

I need to compare two protobuf messages in tests. I use cmp + protocmp for that. A message has a repeated field but for that certain type (repeated message) ordering doesn't matter.

Describe the solution you'd like

I'd like to have a protocmp.SortSlices() option for protocmp.Transform() so that slices are sorted before they are transformed. There are probably other options that are applicable.

Describe alternatives you've considered

cmpopts.SortSlices() doesn't work because the slice/repeated field is a field, it's not top level. So, protocmp transforms it into protocmp.Message and hence cmpopts.SortSlices() doesn't work on it when used with the actual field element type.

Additional context

I'm going to use cmpopts.SortSlices() with filtering + protocmp.Message as a workaround but this is quite a hassle.

dsnet commented 1 month ago

Hi, protocmp already has protocmp.SortRepeated and protocmp.SortRepeatedFields. Is there a reason those don't suite your needs?

ash2k commented 1 month ago

Wow, I overlooked that function. Thank you!