2021-08-12 22:03:24 +03:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/encoding/protowire"
|
2022-09-28 20:30:40 +03:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2021-08-12 22:03:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Equal reports whether two messages are equal.
|
|
|
|
// If two messages marshal to the same bytes under deterministic serialization,
|
|
|
|
// then Equal is guaranteed to report true.
|
|
|
|
//
|
|
|
|
// Two messages are equal if they belong to the same message descriptor,
|
|
|
|
// have the same set of populated known and extension field values,
|
|
|
|
// and the same set of unknown fields values. If either of the top-level
|
|
|
|
// messages are invalid, then Equal reports true only if both are invalid.
|
|
|
|
//
|
|
|
|
// Scalar values are compared with the equivalent of the == operator in Go,
|
|
|
|
// except bytes values which are compared using bytes.Equal and
|
|
|
|
// floating point values which specially treat NaNs as equal.
|
|
|
|
// Message values are compared by recursively calling Equal.
|
|
|
|
// Lists are equal if each element value is also equal.
|
|
|
|
// Maps are equal if they have the same set of keys, where the pair of values
|
|
|
|
// for each key is also equal.
|
|
|
|
func Equal(x, y Message) bool {
|
|
|
|
if x == nil || y == nil {
|
|
|
|
return x == nil && y == nil
|
|
|
|
}
|
2022-09-28 20:30:40 +03:00
|
|
|
if reflect.TypeOf(x).Kind() == reflect.Ptr && x == y {
|
|
|
|
// Avoid an expensive comparison if both inputs are identical pointers.
|
|
|
|
return true
|
|
|
|
}
|
2021-08-12 22:03:24 +03:00
|
|
|
mx := x.ProtoReflect()
|
|
|
|
my := y.ProtoReflect()
|
|
|
|
if mx.IsValid() != my.IsValid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return equalMessage(mx, my)
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalMessage compares two messages.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalMessage(mx, my protoreflect.Message) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
if mx.Descriptor() != my.Descriptor() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nx := 0
|
|
|
|
equal := true
|
2022-09-28 20:30:40 +03:00
|
|
|
mx.Range(func(fd protoreflect.FieldDescriptor, vx protoreflect.Value) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
nx++
|
|
|
|
vy := my.Get(fd)
|
|
|
|
equal = my.Has(fd) && equalField(fd, vx, vy)
|
|
|
|
return equal
|
|
|
|
})
|
|
|
|
if !equal {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ny := 0
|
2022-09-28 20:30:40 +03:00
|
|
|
my.Range(func(fd protoreflect.FieldDescriptor, vx protoreflect.Value) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
ny++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if nx != ny {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return equalUnknown(mx.GetUnknown(), my.GetUnknown())
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalField compares two fields.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalField(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
switch {
|
|
|
|
case fd.IsList():
|
|
|
|
return equalList(fd, x.List(), y.List())
|
|
|
|
case fd.IsMap():
|
|
|
|
return equalMap(fd, x.Map(), y.Map())
|
|
|
|
default:
|
|
|
|
return equalValue(fd, x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalMap compares two maps.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalMap(fd protoreflect.FieldDescriptor, x, y protoreflect.Map) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
if x.Len() != y.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
equal := true
|
2022-09-28 20:30:40 +03:00
|
|
|
x.Range(func(k protoreflect.MapKey, vx protoreflect.Value) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
vy := y.Get(k)
|
|
|
|
equal = y.Has(k) && equalValue(fd.MapValue(), vx, vy)
|
|
|
|
return equal
|
|
|
|
})
|
|
|
|
return equal
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalList compares two lists.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalList(fd protoreflect.FieldDescriptor, x, y protoreflect.List) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
if x.Len() != y.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := x.Len() - 1; i >= 0; i-- {
|
|
|
|
if !equalValue(fd, x.Get(i), y.Get(i)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalValue compares two singular values.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalValue(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
switch fd.Kind() {
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.BoolKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return x.Bool() == y.Bool()
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.EnumKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return x.Enum() == y.Enum()
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.Int32Kind, protoreflect.Sint32Kind,
|
|
|
|
protoreflect.Int64Kind, protoreflect.Sint64Kind,
|
|
|
|
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return x.Int() == y.Int()
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
|
|
|
|
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return x.Uint() == y.Uint()
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.FloatKind, protoreflect.DoubleKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
fx := x.Float()
|
|
|
|
fy := y.Float()
|
|
|
|
if math.IsNaN(fx) || math.IsNaN(fy) {
|
|
|
|
return math.IsNaN(fx) && math.IsNaN(fy)
|
|
|
|
}
|
|
|
|
return fx == fy
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.StringKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return x.String() == y.String()
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.BytesKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return bytes.Equal(x.Bytes(), y.Bytes())
|
2022-09-28 20:30:40 +03:00
|
|
|
case protoreflect.MessageKind, protoreflect.GroupKind:
|
2021-08-12 22:03:24 +03:00
|
|
|
return equalMessage(x.Message(), y.Message())
|
|
|
|
default:
|
|
|
|
return x.Interface() == y.Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalUnknown compares unknown fields by direct comparison on the raw bytes
|
|
|
|
// of each individual field number.
|
2022-09-28 20:30:40 +03:00
|
|
|
func equalUnknown(x, y protoreflect.RawFields) bool {
|
2021-08-12 22:03:24 +03:00
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bytes.Equal([]byte(x), []byte(y)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-28 20:30:40 +03:00
|
|
|
mx := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
|
|
|
|
my := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
|
2021-08-12 22:03:24 +03:00
|
|
|
for len(x) > 0 {
|
|
|
|
fnum, _, n := protowire.ConsumeField(x)
|
|
|
|
mx[fnum] = append(mx[fnum], x[:n]...)
|
|
|
|
x = x[n:]
|
|
|
|
}
|
|
|
|
for len(y) > 0 {
|
|
|
|
fnum, _, n := protowire.ConsumeField(y)
|
|
|
|
my[fnum] = append(my[fnum], y[:n]...)
|
|
|
|
y = y[n:]
|
|
|
|
}
|
|
|
|
return reflect.DeepEqual(mx, my)
|
|
|
|
}
|