blob: c36d4a9cd75b723ebfc75e3d5c3bdd5ad4d0a9ea [file] [log] [blame]
khenaidoo7d3c5582021-08-11 18:09:44 -04001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package proto
6
7import (
khenaidoo7d3c5582021-08-11 18:09:44 -04008 "reflect"
9
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053010 "google.golang.org/protobuf/reflect/protoreflect"
11 "google.golang.org/protobuf/runtime/protoiface"
khenaidoo7d3c5582021-08-11 18:09:44 -040012)
13
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053014// Equal reports whether two messages are equal,
15// by recursively comparing the fields of the message.
khenaidoo7d3c5582021-08-11 18:09:44 -040016//
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053017// - Bytes fields are equal if they contain identical bytes.
18// Empty bytes (regardless of nil-ness) are considered equal.
khenaidoo7d3c5582021-08-11 18:09:44 -040019//
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053020// - Floating-point fields are equal if they contain the same value.
21// Unlike the == operator, a NaN is equal to another NaN.
22//
23// - Other scalar fields are equal if they contain the same value.
24//
25// - Message fields are equal if they have
26// the same set of populated known and extension field values, and
27// the same set of unknown fields values.
28//
29// - Lists are equal if they are the same length and
30// each corresponding element is equal.
31//
32// - Maps are equal if they have the same set of keys and
33// the corresponding value for each key is equal.
34//
35// An invalid message is not equal to a valid message.
36// An invalid message is only equal to another invalid message of the
37// same type. An invalid message often corresponds to a nil pointer
38// of the concrete message type. For example, (*pb.M)(nil) is not equal
39// to &pb.M{}.
40// If two valid messages marshal to the same bytes under deterministic
41// serialization, then Equal is guaranteed to report true.
khenaidoo7d3c5582021-08-11 18:09:44 -040042func Equal(x, y Message) bool {
43 if x == nil || y == nil {
44 return x == nil && y == nil
45 }
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053046 if reflect.TypeOf(x).Kind() == reflect.Ptr && x == y {
47 // Avoid an expensive comparison if both inputs are identical pointers.
48 return true
49 }
khenaidoo7d3c5582021-08-11 18:09:44 -040050 mx := x.ProtoReflect()
51 my := y.ProtoReflect()
52 if mx.IsValid() != my.IsValid() {
53 return false
54 }
khenaidoo7d3c5582021-08-11 18:09:44 -040055
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053056 // Only one of the messages needs to implement the fast-path for it to work.
57 pmx := protoMethods(mx)
58 pmy := protoMethods(my)
59 if pmx != nil && pmy != nil && pmx.Equal != nil && pmy.Equal != nil {
60 return pmx.Equal(protoiface.EqualInput{MessageA: mx, MessageB: my}).Equal
khenaidoo7d3c5582021-08-11 18:09:44 -040061 }
62
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053063 vx := protoreflect.ValueOfMessage(mx)
64 vy := protoreflect.ValueOfMessage(my)
65 return vx.Equal(vy)
khenaidoo7d3c5582021-08-11 18:09:44 -040066}