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