-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Description
Is your feature request related to a problem? Please describe.
This is a feature request to provide a method similar to proto.Equal that works with Go's slices and cmp packages. This comes up often when attempting to sort two lists of protos and determining a diff.
Describe the solution you'd like
// Compare provides an ordering of two message of the same type, returning -1 if
// x is "less than" y, 0 if x is equal to y, or +1 if x is "greater than" y. This is intended
// for use with [slices.SortFunc] to compare two slices within the same program.
//
// The result of Compare is consistent only within the same binary, and follows rules
// similar to [MarshalOptions.Deterministic]. Wither a message is "less than" or
// "greater than" another message is arbitrary and will change over time.
//
// This method uses the same rules as [Equal] to determine equality.
func Compare(x, y Message) int
Describe alternatives you've considered
A current alternative is to encode each method with MarshalOptions.Deterministic and use that as a source of ordering.
Additional context
I wrote a version of this internally at Google a while back (called "protosort"), and it turned out that it was common enough that someone else had written the same package as well. It'd be great if this was just supported out of the box by the proto package.
Metadata
Metadata
Assignees
Labels
No labels