Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
Joe Tsai | 6bd33b6 | 2019-07-15 13:08:00 -0700 | [diff] [blame] | 9 | "math" |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 10 | "reflect" |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 11 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 16 | // Equal reports whether two messages are equal. |
Joe Tsai | 6bd33b6 | 2019-07-15 13:08:00 -0700 | [diff] [blame] | 17 | // If two messages marshal to the same bytes under deterministic serialization, |
| 18 | // then Equal is guaranteed to report true. |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 19 | // |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 20 | // Two messages are equal if they belong to the same message descriptor, |
| 21 | // have the same set of populated known and extension field values, |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 22 | // and the same set of unknown fields values. If either of the top-level |
| 23 | // messages are invalid, then Equal reports true only if both are invalid. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 24 | // |
| 25 | // Scalar values are compared with the equivalent of the == operator in Go, |
Joe Tsai | 6bd33b6 | 2019-07-15 13:08:00 -0700 | [diff] [blame] | 26 | // except bytes values which are compared using bytes.Equal and |
| 27 | // floating point values which specially treat NaNs as equal. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 28 | // Message values are compared by recursively calling Equal. |
| 29 | // Lists are equal if each element value is also equal. |
| 30 | // Maps are equal if they have the same set of keys, where the pair of values |
| 31 | // for each key is also equal. |
| 32 | func Equal(x, y Message) bool { |
Joe Tsai | f2c4ddc | 2019-09-19 21:28:52 -0700 | [diff] [blame] | 33 | if x == nil || y == nil { |
| 34 | return x == nil && y == nil |
| 35 | } |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 36 | mx := x.ProtoReflect() |
| 37 | my := y.ProtoReflect() |
Damien Neil | 2ad3f24 | 2020-01-06 15:12:19 -0800 | [diff] [blame] | 38 | if mx.IsValid() != my.IsValid() { |
| 39 | return false |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 40 | } |
| 41 | return equalMessage(mx, my) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // equalMessage compares two messages. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 45 | func equalMessage(mx, my pref.Message) bool { |
| 46 | if mx.Descriptor() != my.Descriptor() { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 47 | return false |
| 48 | } |
| 49 | |
Damien Neil | a994082 | 2019-06-24 12:58:17 -0700 | [diff] [blame] | 50 | nx := 0 |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 51 | equal := true |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 52 | mx.Range(func(fd pref.FieldDescriptor, vx pref.Value) bool { |
Damien Neil | a994082 | 2019-06-24 12:58:17 -0700 | [diff] [blame] | 53 | nx++ |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 54 | vy := my.Get(fd) |
| 55 | equal = my.Has(fd) && equalField(fd, vx, vy) |
| 56 | return equal |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 57 | }) |
| 58 | if !equal { |
| 59 | return false |
| 60 | } |
Damien Neil | a994082 | 2019-06-24 12:58:17 -0700 | [diff] [blame] | 61 | ny := 0 |
| 62 | my.Range(func(fd pref.FieldDescriptor, vx pref.Value) bool { |
| 63 | ny++ |
| 64 | return true |
| 65 | }) |
| 66 | if nx != ny { |
| 67 | return false |
| 68 | } |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 69 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 70 | return equalUnknown(mx.GetUnknown(), my.GetUnknown()) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 73 | // equalField compares two fields. |
| 74 | func equalField(fd pref.FieldDescriptor, x, y pref.Value) bool { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 75 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 76 | case fd.IsList(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 77 | return equalList(fd, x.List(), y.List()) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 78 | case fd.IsMap(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 79 | return equalMap(fd, x.Map(), y.Map()) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 80 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 81 | return equalValue(fd, x, y) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 85 | // equalMap compares two maps. |
| 86 | func equalMap(fd pref.FieldDescriptor, x, y pref.Map) bool { |
| 87 | if x.Len() != y.Len() { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 88 | return false |
| 89 | } |
| 90 | equal := true |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 91 | x.Range(func(k pref.MapKey, vx pref.Value) bool { |
| 92 | vy := y.Get(k) |
| 93 | equal = y.Has(k) && equalValue(fd.MapValue(), vx, vy) |
| 94 | return equal |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 95 | }) |
| 96 | return equal |
| 97 | } |
| 98 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 99 | // equalList compares two lists. |
| 100 | func equalList(fd pref.FieldDescriptor, x, y pref.List) bool { |
| 101 | if x.Len() != y.Len() { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 102 | return false |
| 103 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 104 | for i := x.Len() - 1; i >= 0; i-- { |
| 105 | if !equalValue(fd, x.Get(i), y.Get(i)) { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 106 | return false |
| 107 | } |
| 108 | } |
| 109 | return true |
| 110 | } |
| 111 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 112 | // equalValue compares two singular values. |
| 113 | func equalValue(fd pref.FieldDescriptor, x, y pref.Value) bool { |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 114 | switch { |
| 115 | case fd.Message() != nil: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 116 | return equalMessage(x.Message(), y.Message()) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 117 | case fd.Kind() == pref.BytesKind: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 118 | return bytes.Equal(x.Bytes(), y.Bytes()) |
Joe Tsai | 6bd33b6 | 2019-07-15 13:08:00 -0700 | [diff] [blame] | 119 | case fd.Kind() == pref.FloatKind, fd.Kind() == pref.DoubleKind: |
| 120 | fx := x.Float() |
| 121 | fy := y.Float() |
| 122 | if math.IsNaN(fx) || math.IsNaN(fy) { |
| 123 | return math.IsNaN(fx) && math.IsNaN(fy) |
| 124 | } |
| 125 | return fx == fy |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 126 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 127 | return x.Interface() == y.Interface() |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 130 | |
| 131 | // equalUnknown compares unknown fields by direct comparison on the raw bytes |
| 132 | // of each individual field number. |
| 133 | func equalUnknown(x, y pref.RawFields) bool { |
| 134 | if len(x) != len(y) { |
| 135 | return false |
| 136 | } |
| 137 | if bytes.Equal([]byte(x), []byte(y)) { |
| 138 | return true |
| 139 | } |
| 140 | |
| 141 | mx := make(map[pref.FieldNumber]pref.RawFields) |
| 142 | my := make(map[pref.FieldNumber]pref.RawFields) |
| 143 | for len(x) > 0 { |
| 144 | fnum, _, n := wire.ConsumeField(x) |
| 145 | mx[fnum] = append(mx[fnum], x[:n]...) |
| 146 | x = x[n:] |
| 147 | } |
| 148 | for len(y) > 0 { |
| 149 | fnum, _, n := wire.ConsumeField(y) |
| 150 | my[fnum] = append(my[fnum], y[:n]...) |
| 151 | y = y[n:] |
| 152 | } |
| 153 | return reflect.DeepEqual(mx, my) |
| 154 | } |