| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package pb |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "reflect" |
| 20 | |
| 21 | "google.golang.org/protobuf/encoding/protowire" |
| 22 | "google.golang.org/protobuf/proto" |
| 23 | "google.golang.org/protobuf/reflect/protoreflect" |
| 24 | |
| 25 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 26 | ) |
| 27 | |
| 28 | // Equal returns whether two proto.Message instances are equal using the following criteria: |
| 29 | // |
| 30 | // - Messages must share the same instance of the type descriptor |
| 31 | // - Known set fields are compared using semantics equality |
| 32 | // - Bytes are compared using bytes.Equal |
| 33 | // - Scalar values are compared with operator == |
| 34 | // - List and map types are equal if they have the same length and all elements are equal |
| 35 | // - Messages are equal if they share the same descriptor and all set fields are equal |
| 36 | // - Unknown fields are compared using byte equality |
| 37 | // - NaN values are not equal to each other |
| 38 | // - google.protobuf.Any values are unpacked before comparison |
| 39 | // - If the type descriptor for a protobuf.Any cannot be found, byte equality is used rather than |
| 40 | // semantic equality. |
| 41 | // |
| 42 | // This method of proto equality mirrors the behavior of the C++ protobuf MessageDifferencer |
| 43 | // whereas the golang proto.Equal implementation mirrors the Java protobuf equals() methods |
| 44 | // behaviors which needed to treat NaN values as equal due to Java semantics. |
| 45 | func Equal(x, y proto.Message) bool { |
| 46 | if x == nil || y == nil { |
| 47 | return x == nil && y == nil |
| 48 | } |
| 49 | xRef := x.ProtoReflect() |
| 50 | yRef := y.ProtoReflect() |
| 51 | return equalMessage(xRef, yRef) |
| 52 | } |
| 53 | |
| 54 | func equalMessage(mx, my protoreflect.Message) bool { |
| 55 | // Note, the original proto.Equal upon which this implementation is based does not specifically handle the |
| 56 | // case when both messages are invalid. It is assumed that the descriptors will be equal and that byte-wise |
| 57 | // comparison will be used, though the semantics of validity are neither clear, nor promised within the |
| 58 | // proto.Equal implementation. |
| 59 | if mx.IsValid() != my.IsValid() || mx.Descriptor() != my.Descriptor() { |
| 60 | return false |
| 61 | } |
| 62 | |
| 63 | // This is an innovation on the default proto.Equal where protobuf.Any values are unpacked before comparison |
| 64 | // as otherwise the Any values are compared by bytes rather than structurally. |
| 65 | if isAny(mx) && isAny(my) { |
| 66 | ax := mx.Interface().(*anypb.Any) |
| 67 | ay := my.Interface().(*anypb.Any) |
| 68 | // If the values are not the same type url, return false. |
| 69 | if ax.GetTypeUrl() != ay.GetTypeUrl() { |
| 70 | return false |
| 71 | } |
| 72 | // If the values are byte equal, then return true. |
| 73 | if bytes.Equal(ax.GetValue(), ay.GetValue()) { |
| 74 | return true |
| 75 | } |
| 76 | // Otherwise fall through to the semantic comparison of the any values. |
| 77 | x, err := ax.UnmarshalNew() |
| 78 | if err != nil { |
| 79 | return false |
| 80 | } |
| 81 | y, err := ay.UnmarshalNew() |
| 82 | if err != nil { |
| 83 | return false |
| 84 | } |
| 85 | // Recursively compare the unwrapped messages to ensure nested Any values are unwrapped accordingly. |
| 86 | return equalMessage(x.ProtoReflect(), y.ProtoReflect()) |
| 87 | } |
| 88 | |
| 89 | // Walk the set fields to determine field-wise equality |
| 90 | nx := 0 |
| 91 | equal := true |
| 92 | mx.Range(func(fd protoreflect.FieldDescriptor, vx protoreflect.Value) bool { |
| 93 | nx++ |
| 94 | equal = my.Has(fd) && equalField(fd, vx, my.Get(fd)) |
| 95 | return equal |
| 96 | }) |
| 97 | if !equal { |
| 98 | return false |
| 99 | } |
| 100 | // Establish the count of set fields on message y |
| 101 | ny := 0 |
| 102 | my.Range(func(protoreflect.FieldDescriptor, protoreflect.Value) bool { |
| 103 | ny++ |
| 104 | return true |
| 105 | }) |
| 106 | // If the number of set fields is not equal return false. |
| 107 | if nx != ny { |
| 108 | return false |
| 109 | } |
| 110 | |
| 111 | return equalUnknown(mx.GetUnknown(), my.GetUnknown()) |
| 112 | } |
| 113 | |
| 114 | func equalField(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) bool { |
| 115 | switch { |
| 116 | case fd.IsMap(): |
| 117 | return equalMap(fd, x.Map(), y.Map()) |
| 118 | case fd.IsList(): |
| 119 | return equalList(fd, x.List(), y.List()) |
| 120 | default: |
| 121 | return equalValue(fd, x, y) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func equalMap(fd protoreflect.FieldDescriptor, x, y protoreflect.Map) bool { |
| 126 | if x.Len() != y.Len() { |
| 127 | return false |
| 128 | } |
| 129 | equal := true |
| 130 | x.Range(func(k protoreflect.MapKey, vx protoreflect.Value) bool { |
| 131 | vy := y.Get(k) |
| 132 | equal = y.Has(k) && equalValue(fd.MapValue(), vx, vy) |
| 133 | return equal |
| 134 | }) |
| 135 | return equal |
| 136 | } |
| 137 | |
| 138 | func equalList(fd protoreflect.FieldDescriptor, x, y protoreflect.List) bool { |
| 139 | if x.Len() != y.Len() { |
| 140 | return false |
| 141 | } |
| 142 | for i := x.Len() - 1; i >= 0; i-- { |
| 143 | if !equalValue(fd, x.Get(i), y.Get(i)) { |
| 144 | return false |
| 145 | } |
| 146 | } |
| 147 | return true |
| 148 | } |
| 149 | |
| 150 | func equalValue(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) bool { |
| 151 | switch fd.Kind() { |
| 152 | case protoreflect.BoolKind: |
| 153 | return x.Bool() == y.Bool() |
| 154 | case protoreflect.EnumKind: |
| 155 | return x.Enum() == y.Enum() |
| 156 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, |
| 157 | protoreflect.Int64Kind, protoreflect.Sint64Kind, |
| 158 | protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind: |
| 159 | return x.Int() == y.Int() |
| 160 | case protoreflect.Uint32Kind, protoreflect.Uint64Kind, |
| 161 | protoreflect.Fixed32Kind, protoreflect.Fixed64Kind: |
| 162 | return x.Uint() == y.Uint() |
| 163 | case protoreflect.FloatKind, protoreflect.DoubleKind: |
| 164 | return x.Float() == y.Float() |
| 165 | case protoreflect.StringKind: |
| 166 | return x.String() == y.String() |
| 167 | case protoreflect.BytesKind: |
| 168 | return bytes.Equal(x.Bytes(), y.Bytes()) |
| 169 | case protoreflect.MessageKind, protoreflect.GroupKind: |
| 170 | return equalMessage(x.Message(), y.Message()) |
| 171 | default: |
| 172 | return x.Interface() == y.Interface() |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func equalUnknown(x, y protoreflect.RawFields) bool { |
| 177 | lenX := len(x) |
| 178 | lenY := len(y) |
| 179 | if lenX != lenY { |
| 180 | return false |
| 181 | } |
| 182 | if lenX == 0 { |
| 183 | return true |
| 184 | } |
| 185 | if bytes.Equal([]byte(x), []byte(y)) { |
| 186 | return true |
| 187 | } |
| 188 | |
| 189 | mx := make(map[protoreflect.FieldNumber]protoreflect.RawFields) |
| 190 | my := make(map[protoreflect.FieldNumber]protoreflect.RawFields) |
| 191 | for len(x) > 0 { |
| 192 | fnum, _, n := protowire.ConsumeField(x) |
| 193 | mx[fnum] = append(mx[fnum], x[:n]...) |
| 194 | x = x[n:] |
| 195 | } |
| 196 | for len(y) > 0 { |
| 197 | fnum, _, n := protowire.ConsumeField(y) |
| 198 | my[fnum] = append(my[fnum], y[:n]...) |
| 199 | y = y[n:] |
| 200 | } |
| 201 | return reflect.DeepEqual(mx, my) |
| 202 | } |
| 203 | |
| 204 | func isAny(m protoreflect.Message) bool { |
| 205 | return string(m.Descriptor().FullName()) == "google.protobuf.Any" |
| 206 | } |