mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-29 02:44:08 -05:00
792b4dba2c
* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
167 lines
4.3 KiB
Go
Vendored
167 lines
4.3 KiB
Go
Vendored
// 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"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
mx := x.ProtoReflect()
|
|
my := y.ProtoReflect()
|
|
if mx.IsValid() != my.IsValid() {
|
|
return false
|
|
}
|
|
return equalMessage(mx, my)
|
|
}
|
|
|
|
// equalMessage compares two messages.
|
|
func equalMessage(mx, my pref.Message) bool {
|
|
if mx.Descriptor() != my.Descriptor() {
|
|
return false
|
|
}
|
|
|
|
nx := 0
|
|
equal := true
|
|
mx.Range(func(fd pref.FieldDescriptor, vx pref.Value) bool {
|
|
nx++
|
|
vy := my.Get(fd)
|
|
equal = my.Has(fd) && equalField(fd, vx, vy)
|
|
return equal
|
|
})
|
|
if !equal {
|
|
return false
|
|
}
|
|
ny := 0
|
|
my.Range(func(fd pref.FieldDescriptor, vx pref.Value) bool {
|
|
ny++
|
|
return true
|
|
})
|
|
if nx != ny {
|
|
return false
|
|
}
|
|
|
|
return equalUnknown(mx.GetUnknown(), my.GetUnknown())
|
|
}
|
|
|
|
// equalField compares two fields.
|
|
func equalField(fd pref.FieldDescriptor, x, y pref.Value) bool {
|
|
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.
|
|
func equalMap(fd pref.FieldDescriptor, x, y pref.Map) bool {
|
|
if x.Len() != y.Len() {
|
|
return false
|
|
}
|
|
equal := true
|
|
x.Range(func(k pref.MapKey, vx pref.Value) bool {
|
|
vy := y.Get(k)
|
|
equal = y.Has(k) && equalValue(fd.MapValue(), vx, vy)
|
|
return equal
|
|
})
|
|
return equal
|
|
}
|
|
|
|
// equalList compares two lists.
|
|
func equalList(fd pref.FieldDescriptor, x, y pref.List) bool {
|
|
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.
|
|
func equalValue(fd pref.FieldDescriptor, x, y pref.Value) bool {
|
|
switch fd.Kind() {
|
|
case pref.BoolKind:
|
|
return x.Bool() == y.Bool()
|
|
case pref.EnumKind:
|
|
return x.Enum() == y.Enum()
|
|
case pref.Int32Kind, pref.Sint32Kind,
|
|
pref.Int64Kind, pref.Sint64Kind,
|
|
pref.Sfixed32Kind, pref.Sfixed64Kind:
|
|
return x.Int() == y.Int()
|
|
case pref.Uint32Kind, pref.Uint64Kind,
|
|
pref.Fixed32Kind, pref.Fixed64Kind:
|
|
return x.Uint() == y.Uint()
|
|
case pref.FloatKind, pref.DoubleKind:
|
|
fx := x.Float()
|
|
fy := y.Float()
|
|
if math.IsNaN(fx) || math.IsNaN(fy) {
|
|
return math.IsNaN(fx) && math.IsNaN(fy)
|
|
}
|
|
return fx == fy
|
|
case pref.StringKind:
|
|
return x.String() == y.String()
|
|
case pref.BytesKind:
|
|
return bytes.Equal(x.Bytes(), y.Bytes())
|
|
case pref.MessageKind, pref.GroupKind:
|
|
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.
|
|
func equalUnknown(x, y pref.RawFields) bool {
|
|
if len(x) != len(y) {
|
|
return false
|
|
}
|
|
if bytes.Equal([]byte(x), []byte(y)) {
|
|
return true
|
|
}
|
|
|
|
mx := make(map[pref.FieldNumber]pref.RawFields)
|
|
my := make(map[pref.FieldNumber]pref.RawFields)
|
|
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)
|
|
}
|