Takahiro Suzuki | d7bf820 | 2020-12-17 20:21:59 +0900 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2011 The Go Authors. All rights reserved. |
| 4 | // https://github.com/golang/protobuf |
| 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 deep copy and merge. |
| 33 | // TODO: RawMessage. |
| 34 | |
| 35 | package proto |
| 36 | |
| 37 | import ( |
| 38 | "fmt" |
| 39 | "log" |
| 40 | "reflect" |
| 41 | "strings" |
| 42 | ) |
| 43 | |
| 44 | // Clone returns a deep copy of a protocol buffer. |
| 45 | func Clone(src Message) Message { |
| 46 | in := reflect.ValueOf(src) |
| 47 | if in.IsNil() { |
| 48 | return src |
| 49 | } |
| 50 | out := reflect.New(in.Type().Elem()) |
| 51 | dst := out.Interface().(Message) |
| 52 | Merge(dst, src) |
| 53 | return dst |
| 54 | } |
| 55 | |
| 56 | // Merger is the interface representing objects that can merge messages of the same type. |
| 57 | type Merger interface { |
| 58 | // Merge merges src into this message. |
| 59 | // Required and optional fields that are set in src will be set to that value in dst. |
| 60 | // Elements of repeated fields will be appended. |
| 61 | // |
| 62 | // Merge may panic if called with a different argument type than the receiver. |
| 63 | Merge(src Message) |
| 64 | } |
| 65 | |
| 66 | // generatedMerger is the custom merge method that generated protos will have. |
| 67 | // We must add this method since a generate Merge method will conflict with |
| 68 | // many existing protos that have a Merge data field already defined. |
| 69 | type generatedMerger interface { |
| 70 | XXX_Merge(src Message) |
| 71 | } |
| 72 | |
| 73 | // Merge merges src into dst. |
| 74 | // Required and optional fields that are set in src will be set to that value in dst. |
| 75 | // Elements of repeated fields will be appended. |
| 76 | // Merge panics if src and dst are not the same type, or if dst is nil. |
| 77 | func Merge(dst, src Message) { |
| 78 | if m, ok := dst.(Merger); ok { |
| 79 | m.Merge(src) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | in := reflect.ValueOf(src) |
| 84 | out := reflect.ValueOf(dst) |
| 85 | if out.IsNil() { |
| 86 | panic("proto: nil destination") |
| 87 | } |
| 88 | if in.Type() != out.Type() { |
| 89 | panic(fmt.Sprintf("proto.Merge(%T, %T) type mismatch", dst, src)) |
| 90 | } |
| 91 | if in.IsNil() { |
| 92 | return // Merge from nil src is a noop |
| 93 | } |
| 94 | if m, ok := dst.(generatedMerger); ok { |
| 95 | m.XXX_Merge(src) |
| 96 | return |
| 97 | } |
| 98 | mergeStruct(out.Elem(), in.Elem()) |
| 99 | } |
| 100 | |
| 101 | func mergeStruct(out, in reflect.Value) { |
| 102 | sprop := GetProperties(in.Type()) |
| 103 | for i := 0; i < in.NumField(); i++ { |
| 104 | f := in.Type().Field(i) |
| 105 | if strings.HasPrefix(f.Name, "XXX_") { |
| 106 | continue |
| 107 | } |
| 108 | mergeAny(out.Field(i), in.Field(i), false, sprop.Prop[i]) |
| 109 | } |
| 110 | |
| 111 | if emIn, ok := in.Addr().Interface().(extensionsBytes); ok { |
| 112 | emOut := out.Addr().Interface().(extensionsBytes) |
| 113 | bIn := emIn.GetExtensions() |
| 114 | bOut := emOut.GetExtensions() |
| 115 | *bOut = append(*bOut, *bIn...) |
| 116 | } else if emIn, err := extendable(in.Addr().Interface()); err == nil { |
| 117 | emOut, _ := extendable(out.Addr().Interface()) |
| 118 | mIn, muIn := emIn.extensionsRead() |
| 119 | if mIn != nil { |
| 120 | mOut := emOut.extensionsWrite() |
| 121 | muIn.Lock() |
| 122 | mergeExtension(mOut, mIn) |
| 123 | muIn.Unlock() |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | uf := in.FieldByName("XXX_unrecognized") |
| 128 | if !uf.IsValid() { |
| 129 | return |
| 130 | } |
| 131 | uin := uf.Bytes() |
| 132 | if len(uin) > 0 { |
| 133 | out.FieldByName("XXX_unrecognized").SetBytes(append([]byte(nil), uin...)) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // mergeAny performs a merge between two values of the same type. |
| 138 | // viaPtr indicates whether the values were indirected through a pointer (implying proto2). |
| 139 | // prop is set if this is a struct field (it may be nil). |
| 140 | func mergeAny(out, in reflect.Value, viaPtr bool, prop *Properties) { |
| 141 | if in.Type() == protoMessageType { |
| 142 | if !in.IsNil() { |
| 143 | if out.IsNil() { |
| 144 | out.Set(reflect.ValueOf(Clone(in.Interface().(Message)))) |
| 145 | } else { |
| 146 | Merge(out.Interface().(Message), in.Interface().(Message)) |
| 147 | } |
| 148 | } |
| 149 | return |
| 150 | } |
| 151 | switch in.Kind() { |
| 152 | case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64, |
| 153 | reflect.String, reflect.Uint32, reflect.Uint64: |
| 154 | if !viaPtr && isProto3Zero(in) { |
| 155 | return |
| 156 | } |
| 157 | out.Set(in) |
| 158 | case reflect.Interface: |
| 159 | // Probably a oneof field; copy non-nil values. |
| 160 | if in.IsNil() { |
| 161 | return |
| 162 | } |
| 163 | // Allocate destination if it is not set, or set to a different type. |
| 164 | // Otherwise we will merge as normal. |
| 165 | if out.IsNil() || out.Elem().Type() != in.Elem().Type() { |
| 166 | out.Set(reflect.New(in.Elem().Elem().Type())) // interface -> *T -> T -> new(T) |
| 167 | } |
| 168 | mergeAny(out.Elem(), in.Elem(), false, nil) |
| 169 | case reflect.Map: |
| 170 | if in.Len() == 0 { |
| 171 | return |
| 172 | } |
| 173 | if out.IsNil() { |
| 174 | out.Set(reflect.MakeMap(in.Type())) |
| 175 | } |
| 176 | // For maps with value types of *T or []byte we need to deep copy each value. |
| 177 | elemKind := in.Type().Elem().Kind() |
| 178 | for _, key := range in.MapKeys() { |
| 179 | var val reflect.Value |
| 180 | switch elemKind { |
| 181 | case reflect.Ptr: |
| 182 | val = reflect.New(in.Type().Elem().Elem()) |
| 183 | mergeAny(val, in.MapIndex(key), false, nil) |
| 184 | case reflect.Slice: |
| 185 | val = in.MapIndex(key) |
| 186 | val = reflect.ValueOf(append([]byte{}, val.Bytes()...)) |
| 187 | default: |
| 188 | val = in.MapIndex(key) |
| 189 | } |
| 190 | out.SetMapIndex(key, val) |
| 191 | } |
| 192 | case reflect.Ptr: |
| 193 | if in.IsNil() { |
| 194 | return |
| 195 | } |
| 196 | if out.IsNil() { |
| 197 | out.Set(reflect.New(in.Elem().Type())) |
| 198 | } |
| 199 | mergeAny(out.Elem(), in.Elem(), true, nil) |
| 200 | case reflect.Slice: |
| 201 | if in.IsNil() { |
| 202 | return |
| 203 | } |
| 204 | if in.Type().Elem().Kind() == reflect.Uint8 { |
| 205 | // []byte is a scalar bytes field, not a repeated field. |
| 206 | |
| 207 | // Edge case: if this is in a proto3 message, a zero length |
| 208 | // bytes field is considered the zero value, and should not |
| 209 | // be merged. |
| 210 | if prop != nil && prop.proto3 && in.Len() == 0 { |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | // Make a deep copy. |
| 215 | // Append to []byte{} instead of []byte(nil) so that we never end up |
| 216 | // with a nil result. |
| 217 | out.SetBytes(append([]byte{}, in.Bytes()...)) |
| 218 | return |
| 219 | } |
| 220 | n := in.Len() |
| 221 | if out.IsNil() { |
| 222 | out.Set(reflect.MakeSlice(in.Type(), 0, n)) |
| 223 | } |
| 224 | switch in.Type().Elem().Kind() { |
| 225 | case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64, |
| 226 | reflect.String, reflect.Uint32, reflect.Uint64: |
| 227 | out.Set(reflect.AppendSlice(out, in)) |
| 228 | default: |
| 229 | for i := 0; i < n; i++ { |
| 230 | x := reflect.Indirect(reflect.New(in.Type().Elem())) |
| 231 | mergeAny(x, in.Index(i), false, nil) |
| 232 | out.Set(reflect.Append(out, x)) |
| 233 | } |
| 234 | } |
| 235 | case reflect.Struct: |
| 236 | mergeStruct(out, in) |
| 237 | default: |
| 238 | // unknown type, so not a protocol buffer |
| 239 | log.Printf("proto: don't know how to copy %v", in) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func mergeExtension(out, in map[int32]Extension) { |
| 244 | for extNum, eIn := range in { |
| 245 | eOut := Extension{desc: eIn.desc} |
| 246 | if eIn.value != nil { |
| 247 | v := reflect.New(reflect.TypeOf(eIn.value)).Elem() |
| 248 | mergeAny(v, reflect.ValueOf(eIn.value), false, nil) |
| 249 | eOut.value = v.Interface() |
| 250 | } |
| 251 | if eIn.enc != nil { |
| 252 | eOut.enc = make([]byte, len(eIn.enc)) |
| 253 | copy(eOut.enc, eIn.enc) |
| 254 | } |
| 255 | |
| 256 | out[extNum] = eOut |
| 257 | } |
| 258 | } |