| 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. |
| 33 | // TODO: MessageSet and RawMessage. |
| 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. |
| 46 | The arguments must both be protocol buffer structs, |
| 47 | or both be pointers to protocol buffer structs. |
| 48 | |
| 49 | Equality is defined in this way: |
| 50 | - Two messages are equal iff they are the same type, |
| 51 | corresponding fields are equal, unknown field sets |
| 52 | are equal, and extensions sets are equal. |
| 53 | - Two set scalar fields are equal iff their values are equal. |
| 54 | - Two repeated fields are equal iff their lengths are the same, |
| 55 | and their corresponding elements are equal. |
| 56 | - Two unset fields are equal. |
| 57 | - Two unknown field sets are equal if their current |
| 58 | encoded state is equal. (TODO) |
| 59 | - Two extension sets are equal iff they have corresponding |
| 60 | elements that are pairwise equal. (TODO) |
| 61 | - Every other combination of things are not equal. |
| 62 | |
| 63 | The return value is undefined if a and b are not protocol buffers. |
| 64 | */ |
| 65 | func Equal(a, b interface{}) bool { |
| 66 | v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b) |
| 67 | if v1.Type() != v2.Type() { |
| 68 | return false |
| 69 | } |
| 70 | if v1.Kind() == reflect.Ptr { |
| 71 | v1, v2 = v1.Elem(), v2.Elem() |
| 72 | } |
| 73 | if v1.Kind() != reflect.Struct { |
| 74 | return false |
| 75 | } |
| 76 | return equalStruct(v1, v2) |
| 77 | } |
| 78 | |
| 79 | // v1 and v2 are known to have the same type. |
| 80 | func equalStruct(v1, v2 reflect.Value) bool { |
| 81 | for i := 0; i < v1.NumField(); i++ { |
| 82 | f := v1.Type().Field(i) |
| 83 | if strings.HasPrefix(f.Name, "XXX_") { |
| 84 | continue |
| 85 | } |
| 86 | f1, f2 := v1.Field(i), v2.Field(i) |
| 87 | if f.Type.Kind() == reflect.Ptr { |
| 88 | if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 { |
| 89 | // both unset |
| 90 | continue |
| 91 | } else if n1 != n2 { |
| 92 | // set/unset mismatch |
| 93 | return false |
| 94 | } |
| 95 | f1, f2 = f1.Elem(), f2.Elem() |
| 96 | } |
| 97 | if !equalAny(f1, f2) { |
| 98 | return false |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // TODO: Deal with XXX_unrecognized and XXX_extensions. |
| 103 | |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | // v1 and v2 are known to have the same type. |
| 108 | func equalAny(v1, v2 reflect.Value) bool { |
| 109 | switch v1.Kind() { |
| 110 | case reflect.Bool: |
| 111 | return v1.Bool() == v2.Bool() |
| 112 | case reflect.Float32, reflect.Float64: |
| 113 | return v1.Float() == v2.Float() |
| 114 | case reflect.Int32, reflect.Int64: |
| 115 | return v1.Int() == v2.Int() |
| 116 | case reflect.Ptr: |
| 117 | return equalAny(v1.Elem(), v2.Elem()) |
| 118 | case reflect.Slice: |
| 119 | if n1, n2 := v1.IsNil(), v2.IsNil(); n1 && n2 { |
| 120 | return true |
| 121 | } else if n1 != n2 { |
| 122 | return false |
| 123 | } |
| 124 | if v1.Len() != v2.Len() { |
| 125 | return false |
| 126 | } |
| 127 | // short circuit: []byte |
| 128 | if v1.Type().Elem().Kind() == reflect.Uint8 { |
| 129 | return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte)) |
| 130 | } |
| 131 | for i := 0; i < v1.Len(); i++ { |
| 132 | if !equalAny(v1.Index(i), v2.Index(i)) { |
| 133 | return false |
| 134 | } |
| 135 | } |
| 136 | return true |
| 137 | case reflect.String: |
| 138 | return v1.Interface().(string) == v2.Interface().(string) |
| 139 | case reflect.Struct: |
| 140 | return equalStruct(v1, v2) |
| 141 | case reflect.Uint32, reflect.Uint64: |
| 142 | return v1.Uint() == v2.Uint() |
| 143 | } |
| 144 | |
| 145 | // unknown type, so not a protocol buffer |
| 146 | log.Printf("proto: don't know how to compare %v", v1) |
| 147 | return false |
| 148 | } |