| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2011 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | // Protocol buffer comparison. |
| David Symonds | e9e7aaf | 2012-03-23 13:12:33 +1100 | [diff] [blame] | 33 | // TODO: MessageSet. |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 34 | |
| 35 | package proto |
| 36 | |
| 37 | import ( |
| 38 | "bytes" |
| 39 | "log" |
| 40 | "reflect" |
| 41 | "strings" |
| 42 | ) |
| 43 | |
| 44 | /* |
| 45 | Equal returns true iff protocol buffers a and b are equal. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 46 | The arguments must both be pointers to protocol buffer structs. |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 47 | |
| 48 | Equality is defined in this way: |
| 49 | - Two messages are equal iff they are the same type, |
| 50 | corresponding fields are equal, unknown field sets |
| 51 | are equal, and extensions sets are equal. |
| 52 | - Two set scalar fields are equal iff their values are equal. |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 53 | If the fields are of a floating-point type, remember that |
| 54 | NaN != x for all x, including NaN. |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 55 | - Two repeated fields are equal iff their lengths are the same, |
| David Symonds | 35abe74 | 2012-02-13 09:55:16 +1100 | [diff] [blame] | 56 | and their corresponding elements are equal (a "bytes" field, |
| 57 | although represented by []byte, is not a repeated field) |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 58 | - Two unset fields are equal. |
| 59 | - Two unknown field sets are equal if their current |
| 60 | encoded state is equal. (TODO) |
| 61 | - Two extension sets are equal iff they have corresponding |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 62 | elements that are pairwise equal. |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 63 | - Every other combination of things are not equal. |
| 64 | |
| 65 | The return value is undefined if a and b are not protocol buffers. |
| 66 | */ |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 67 | func Equal(a, b Message) bool { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 68 | if a == nil || b == nil { |
| 69 | return a == b |
| 70 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 71 | v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b) |
| 72 | if v1.Type() != v2.Type() { |
| 73 | return false |
| 74 | } |
| 75 | if v1.Kind() == reflect.Ptr { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 76 | if v1.IsNil() { |
| 77 | return v2.IsNil() |
| 78 | } |
| 79 | if v2.IsNil() { |
| 80 | return false |
| 81 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 82 | v1, v2 = v1.Elem(), v2.Elem() |
| 83 | } |
| 84 | if v1.Kind() != reflect.Struct { |
| 85 | return false |
| 86 | } |
| 87 | return equalStruct(v1, v2) |
| 88 | } |
| 89 | |
| 90 | // v1 and v2 are known to have the same type. |
| 91 | func equalStruct(v1, v2 reflect.Value) bool { |
| 92 | for i := 0; i < v1.NumField(); i++ { |
| 93 | f := v1.Type().Field(i) |
| 94 | if strings.HasPrefix(f.Name, "XXX_") { |
| 95 | continue |
| 96 | } |
| 97 | f1, f2 := v1.Field(i), v2.Field(i) |
| 98 | if f.Type.Kind() == reflect.Ptr { |
| 99 | if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 { |
| 100 | // both unset |
| 101 | continue |
| 102 | } else if n1 != n2 { |
| 103 | // set/unset mismatch |
| 104 | return false |
| 105 | } |
| David Symonds | e9e7aaf | 2012-03-23 13:12:33 +1100 | [diff] [blame] | 106 | b1, ok := f1.Interface().(raw) |
| 107 | if ok { |
| 108 | b2 := f2.Interface().(raw) |
| 109 | // RawMessage |
| 110 | if !bytes.Equal(b1.Bytes(), b2.Bytes()) { |
| 111 | return false |
| 112 | } |
| 113 | continue |
| 114 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 115 | f1, f2 = f1.Elem(), f2.Elem() |
| 116 | } |
| 117 | if !equalAny(f1, f2) { |
| 118 | return false |
| 119 | } |
| 120 | } |
| 121 | |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 122 | if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() { |
| 123 | em2 := v2.FieldByName("XXX_extensions") |
| 124 | if !equalExtensions(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) { |
| 125 | return false |
| 126 | } |
| 127 | } |
| 128 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame^] | 129 | u1 := v1.FieldByName("XXX_unrecognized").Bytes() |
| 130 | u2 := v2.FieldByName("XXX_unrecognized").Bytes() |
| 131 | if !bytes.Equal(u1, u2) { |
| 132 | return false |
| 133 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 134 | |
| 135 | return true |
| 136 | } |
| 137 | |
| 138 | // v1 and v2 are known to have the same type. |
| 139 | func equalAny(v1, v2 reflect.Value) bool { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 140 | if v1.Type() == protoMessageType { |
| 141 | m1, _ := v1.Interface().(Message) |
| 142 | m2, _ := v2.Interface().(Message) |
| 143 | return Equal(m1, m2) |
| 144 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 145 | switch v1.Kind() { |
| 146 | case reflect.Bool: |
| 147 | return v1.Bool() == v2.Bool() |
| 148 | case reflect.Float32, reflect.Float64: |
| 149 | return v1.Float() == v2.Float() |
| 150 | case reflect.Int32, reflect.Int64: |
| 151 | return v1.Int() == v2.Int() |
| 152 | case reflect.Ptr: |
| 153 | return equalAny(v1.Elem(), v2.Elem()) |
| 154 | case reflect.Slice: |
| David Symonds | 35abe74 | 2012-02-13 09:55:16 +1100 | [diff] [blame] | 155 | if v1.Type().Elem().Kind() == reflect.Uint8 { |
| 156 | // short circuit: []byte |
| 157 | if v1.IsNil() != v2.IsNil() { |
| 158 | return false |
| 159 | } |
| 160 | return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte)) |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 161 | } |
| David Symonds | 35abe74 | 2012-02-13 09:55:16 +1100 | [diff] [blame] | 162 | |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 163 | if v1.Len() != v2.Len() { |
| 164 | return false |
| 165 | } |
| David Symonds | 7655121 | 2011-08-22 15:59:24 +1000 | [diff] [blame] | 166 | for i := 0; i < v1.Len(); i++ { |
| 167 | if !equalAny(v1.Index(i), v2.Index(i)) { |
| 168 | return false |
| 169 | } |
| 170 | } |
| 171 | return true |
| 172 | case reflect.String: |
| 173 | return v1.Interface().(string) == v2.Interface().(string) |
| 174 | case reflect.Struct: |
| 175 | return equalStruct(v1, v2) |
| 176 | case reflect.Uint32, reflect.Uint64: |
| 177 | return v1.Uint() == v2.Uint() |
| 178 | } |
| 179 | |
| 180 | // unknown type, so not a protocol buffer |
| 181 | log.Printf("proto: don't know how to compare %v", v1) |
| 182 | return false |
| 183 | } |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 184 | |
| 185 | // base is the struct type that the extensions are based on. |
| 186 | // em1 and em2 are extension maps. |
| 187 | func equalExtensions(base reflect.Type, em1, em2 map[int32]Extension) bool { |
| 188 | if len(em1) != len(em2) { |
| 189 | return false |
| 190 | } |
| 191 | |
| 192 | for extNum, e1 := range em1 { |
| 193 | e2, ok := em2[extNum] |
| 194 | if !ok { |
| 195 | return false |
| 196 | } |
| 197 | |
| 198 | m1, m2 := e1.value, e2.value |
| 199 | |
| 200 | if m1 != nil && m2 != nil { |
| 201 | // Both are unencoded. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 202 | if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) { |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 203 | return false |
| 204 | } |
| 205 | continue |
| 206 | } |
| 207 | |
| 208 | // At least one is encoded. To do a semantically correct comparison |
| 209 | // we need to unmarshal them first. |
| 210 | var desc *ExtensionDesc |
| 211 | if m := extensionMaps[base]; m != nil { |
| 212 | desc = m[extNum] |
| 213 | } |
| 214 | if desc == nil { |
| 215 | log.Printf("proto: don't know how to compare extension %d of %v", extNum, base) |
| 216 | continue |
| 217 | } |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 218 | var err error |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 219 | if m1 == nil { |
| 220 | m1, err = decodeExtension(e1.enc, desc) |
| 221 | } |
| 222 | if m2 == nil && err == nil { |
| 223 | m2, err = decodeExtension(e2.enc, desc) |
| 224 | } |
| 225 | if err != nil { |
| 226 | // The encoded form is invalid. |
| 227 | log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err) |
| 228 | return false |
| 229 | } |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 230 | if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) { |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 231 | return false |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return true |
| 236 | } |