Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Proposal: add proto.Compare #1691

Open
Open
@ericchiang

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /