1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
42 // Merge merges the src message into dst.
43 // This assumes that dst and src of the same type and are non-nil.
44 func (a *InternalMessageInfo) Merge(dst, src Message) {
45 mi := atomicLoadMergeInfo(&a.merge)
47 mi = getMergeInfo(reflect.TypeOf(dst).Elem())
48 atomicStoreMergeInfo(&a.merge, mi)
50 mi.merge(toPointer(&dst), toPointer(&src))
53 type mergeInfo struct {
56 initialized int32 // 0: only typ is valid, 1: everything is valid
59 fields []mergeFieldInfo
60 unrecognized field // Offset of XXX_unrecognized
63 type mergeFieldInfo struct {
64 field field // Offset of field, guaranteed to be valid
66 // isPointer reports whether the value in the field is a pointer.
67 // This is true for the following situations:
68 // * Pointer to struct
69 // * Pointer to basic type (proto2 only)
70 // * Slice (first value in slice header is a pointer)
71 // * String (first value in string header is a pointer)
74 // basicWidth reports the width of the field assuming that it is directly
75 // embedded in the struct (as is the case for basic types in proto3).
76 // The possible values are:
79 // 4: int32, uint32, float32
80 // 8: int64, uint64, float64
83 // Where dst and src are pointers to the types being merged.
84 merge func(dst, src pointer)
88 mergeInfoMap = map[reflect.Type]*mergeInfo{}
89 mergeInfoLock sync.Mutex
92 func getMergeInfo(t reflect.Type) *mergeInfo {
94 defer mergeInfoLock.Unlock()
97 mi = &mergeInfo{typ: t}
103 // merge merges src into dst assuming they are both of type *mi.typ.
104 func (mi *mergeInfo) merge(dst, src pointer) {
106 panic("proto: nil destination")
109 return // Nothing to do.
112 if atomic.LoadInt32(&mi.initialized) == 0 {
113 mi.computeMergeInfo()
116 for _, fi := range mi.fields {
117 sfp := src.offset(fi.field)
119 // As an optimization, we can avoid the merge function call cost
120 // if we know for sure that the source will have no effect
121 // by checking if it is the zero value.
123 if fi.isPointer && sfp.getPointer().isNil() { // Could be slice or string
126 if fi.basicWidth > 0 {
128 case fi.basicWidth == 1 && !*sfp.toBool():
130 case fi.basicWidth == 4 && *sfp.toUint32() == 0:
132 case fi.basicWidth == 8 && *sfp.toUint64() == 0:
138 dfp := dst.offset(fi.field)
142 // TODO: Make this faster?
143 out := dst.asPointerTo(mi.typ).Elem()
144 in := src.asPointerTo(mi.typ).Elem()
145 if emIn, err := extendable(in.Addr().Interface()); err == nil {
146 emOut, _ := extendable(out.Addr().Interface())
147 mIn, muIn := emIn.extensionsRead()
149 mOut := emOut.extensionsWrite()
151 mergeExtension(mOut, mIn)
156 if mi.unrecognized.IsValid() {
157 if b := *src.offset(mi.unrecognized).toBytes(); len(b) > 0 {
158 *dst.offset(mi.unrecognized).toBytes() = append([]byte(nil), b...)
163 func (mi *mergeInfo) computeMergeInfo() {
165 defer mi.lock.Unlock()
166 if mi.initialized != 0 {
172 props := GetProperties(t)
173 for i := 0; i < n; i++ {
175 if strings.HasPrefix(f.Name, "XXX_") {
179 mfi := mergeFieldInfo{field: toField(&f)}
182 // As an optimization, we can avoid the merge function call cost
183 // if we know for sure that the source will have no effect
184 // by checking if it is the zero value.
187 case reflect.Ptr, reflect.Slice, reflect.String:
188 // As a special case, we assume slices and strings are pointers
189 // since we know that the first field in the SliceSlice or
190 // StringHeader is a data pointer.
194 case reflect.Int32, reflect.Uint32, reflect.Float32:
196 case reflect.Int64, reflect.Uint64, reflect.Float64:
201 // Unwrap tf to get at its most basic type.
202 var isPointer, isSlice bool
203 if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
207 if tf.Kind() == reflect.Ptr {
211 if isPointer && isSlice && tf.Kind() != reflect.Struct {
212 panic("both pointer and slice for basic type in " + tf.Name())
218 case isSlice: // E.g., []int32
219 mfi.merge = func(dst, src pointer) {
220 // NOTE: toInt32Slice is not defined (see pointer_reflect.go).
222 sfsp := src.toInt32Slice()
224 dfsp := dst.toInt32Slice()
225 *dfsp = append(*dfsp, *sfsp...)
231 sfs := src.getInt32Slice()
233 dfs := dst.getInt32Slice()
234 dfs = append(dfs, sfs...)
238 dst.setInt32Slice(dfs)
241 case isPointer: // E.g., *int32
242 mfi.merge = func(dst, src pointer) {
243 // NOTE: toInt32Ptr is not defined (see pointer_reflect.go).
245 sfpp := src.toInt32Ptr()
247 dfpp := dst.toInt32Ptr()
249 *dfpp = Int32(**sfpp)
255 sfp := src.getInt32Ptr()
257 dfp := dst.getInt32Ptr()
259 dst.setInt32Ptr(*sfp)
265 default: // E.g., int32
266 mfi.merge = func(dst, src pointer) {
267 if v := *src.toInt32(); v != 0 {
274 case isSlice: // E.g., []int64
275 mfi.merge = func(dst, src pointer) {
276 sfsp := src.toInt64Slice()
278 dfsp := dst.toInt64Slice()
279 *dfsp = append(*dfsp, *sfsp...)
285 case isPointer: // E.g., *int64
286 mfi.merge = func(dst, src pointer) {
287 sfpp := src.toInt64Ptr()
289 dfpp := dst.toInt64Ptr()
291 *dfpp = Int64(**sfpp)
297 default: // E.g., int64
298 mfi.merge = func(dst, src pointer) {
299 if v := *src.toInt64(); v != 0 {
306 case isSlice: // E.g., []uint32
307 mfi.merge = func(dst, src pointer) {
308 sfsp := src.toUint32Slice()
310 dfsp := dst.toUint32Slice()
311 *dfsp = append(*dfsp, *sfsp...)
317 case isPointer: // E.g., *uint32
318 mfi.merge = func(dst, src pointer) {
319 sfpp := src.toUint32Ptr()
321 dfpp := dst.toUint32Ptr()
323 *dfpp = Uint32(**sfpp)
329 default: // E.g., uint32
330 mfi.merge = func(dst, src pointer) {
331 if v := *src.toUint32(); v != 0 {
338 case isSlice: // E.g., []uint64
339 mfi.merge = func(dst, src pointer) {
340 sfsp := src.toUint64Slice()
342 dfsp := dst.toUint64Slice()
343 *dfsp = append(*dfsp, *sfsp...)
349 case isPointer: // E.g., *uint64
350 mfi.merge = func(dst, src pointer) {
351 sfpp := src.toUint64Ptr()
353 dfpp := dst.toUint64Ptr()
355 *dfpp = Uint64(**sfpp)
361 default: // E.g., uint64
362 mfi.merge = func(dst, src pointer) {
363 if v := *src.toUint64(); v != 0 {
368 case reflect.Float32:
370 case isSlice: // E.g., []float32
371 mfi.merge = func(dst, src pointer) {
372 sfsp := src.toFloat32Slice()
374 dfsp := dst.toFloat32Slice()
375 *dfsp = append(*dfsp, *sfsp...)
381 case isPointer: // E.g., *float32
382 mfi.merge = func(dst, src pointer) {
383 sfpp := src.toFloat32Ptr()
385 dfpp := dst.toFloat32Ptr()
387 *dfpp = Float32(**sfpp)
393 default: // E.g., float32
394 mfi.merge = func(dst, src pointer) {
395 if v := *src.toFloat32(); v != 0 {
400 case reflect.Float64:
402 case isSlice: // E.g., []float64
403 mfi.merge = func(dst, src pointer) {
404 sfsp := src.toFloat64Slice()
406 dfsp := dst.toFloat64Slice()
407 *dfsp = append(*dfsp, *sfsp...)
413 case isPointer: // E.g., *float64
414 mfi.merge = func(dst, src pointer) {
415 sfpp := src.toFloat64Ptr()
417 dfpp := dst.toFloat64Ptr()
419 *dfpp = Float64(**sfpp)
425 default: // E.g., float64
426 mfi.merge = func(dst, src pointer) {
427 if v := *src.toFloat64(); v != 0 {
434 case isSlice: // E.g., []bool
435 mfi.merge = func(dst, src pointer) {
436 sfsp := src.toBoolSlice()
438 dfsp := dst.toBoolSlice()
439 *dfsp = append(*dfsp, *sfsp...)
445 case isPointer: // E.g., *bool
446 mfi.merge = func(dst, src pointer) {
447 sfpp := src.toBoolPtr()
449 dfpp := dst.toBoolPtr()
457 default: // E.g., bool
458 mfi.merge = func(dst, src pointer) {
459 if v := *src.toBool(); v {
466 case isSlice: // E.g., []string
467 mfi.merge = func(dst, src pointer) {
468 sfsp := src.toStringSlice()
470 dfsp := dst.toStringSlice()
471 *dfsp = append(*dfsp, *sfsp...)
477 case isPointer: // E.g., *string
478 mfi.merge = func(dst, src pointer) {
479 sfpp := src.toStringPtr()
481 dfpp := dst.toStringPtr()
483 *dfpp = String(**sfpp)
489 default: // E.g., string
490 mfi.merge = func(dst, src pointer) {
491 if v := *src.toString(); v != "" {
497 isProto3 := props.Prop[i].proto3
500 panic("bad pointer in byte slice case in " + tf.Name())
501 case tf.Elem().Kind() != reflect.Uint8:
502 panic("bad element kind in byte slice case in " + tf.Name())
503 case isSlice: // E.g., [][]byte
504 mfi.merge = func(dst, src pointer) {
505 sbsp := src.toBytesSlice()
507 dbsp := dst.toBytesSlice()
508 for _, sb := range *sbsp {
510 *dbsp = append(*dbsp, nil)
512 *dbsp = append(*dbsp, append([]byte{}, sb...))
520 default: // E.g., []byte
521 mfi.merge = func(dst, src pointer) {
525 if !isProto3 || len(*sbp) > 0 {
526 *dbp = append([]byte{}, *sbp...)
534 mergeInfo := getMergeInfo(tf)
535 mfi.merge = func(dst, src pointer) {
536 mergeInfo.merge(dst, src)
538 case isSlice: // E.g., []*pb.T
539 mergeInfo := getMergeInfo(tf)
540 mfi.merge = func(dst, src pointer) {
541 sps := src.getPointerSlice()
543 dps := dst.getPointerSlice()
544 for _, sp := range sps {
547 dp = valToPointer(reflect.New(tf))
548 mergeInfo.merge(dp, sp)
550 dps = append(dps, dp)
555 dst.setPointerSlice(dps)
558 default: // E.g., *pb.T
559 mergeInfo := getMergeInfo(tf)
560 mfi.merge = func(dst, src pointer) {
561 sp := src.getPointer()
563 dp := dst.getPointer()
565 dp = valToPointer(reflect.New(tf))
568 mergeInfo.merge(dp, sp)
574 case isPointer || isSlice:
575 panic("bad pointer or slice in map case in " + tf.Name())
576 default: // E.g., map[K]V
577 mfi.merge = func(dst, src pointer) {
578 sm := src.asPointerTo(tf).Elem()
582 dm := dst.asPointerTo(tf).Elem()
584 dm.Set(reflect.MakeMap(tf))
587 switch tf.Elem().Kind() {
588 case reflect.Ptr: // Proto struct (e.g., *T)
589 for _, key := range sm.MapKeys() {
590 val := sm.MapIndex(key)
591 val = reflect.ValueOf(Clone(val.Interface().(Message)))
592 dm.SetMapIndex(key, val)
594 case reflect.Slice: // E.g. Bytes type (e.g., []byte)
595 for _, key := range sm.MapKeys() {
596 val := sm.MapIndex(key)
597 val = reflect.ValueOf(append([]byte{}, val.Bytes()...))
598 dm.SetMapIndex(key, val)
600 default: // Basic type (e.g., string)
601 for _, key := range sm.MapKeys() {
602 val := sm.MapIndex(key)
603 dm.SetMapIndex(key, val)
608 case reflect.Interface:
609 // Must be oneof field.
611 case isPointer || isSlice:
612 panic("bad pointer or slice in interface case in " + tf.Name())
613 default: // E.g., interface{}
614 // TODO: Make this faster?
615 mfi.merge = func(dst, src pointer) {
616 su := src.asPointerTo(tf).Elem()
618 du := dst.asPointerTo(tf).Elem()
619 typ := su.Elem().Type()
620 if du.IsNil() || du.Elem().Type() != typ {
621 du.Set(reflect.New(typ.Elem())) // Initialize interface if empty
623 sv := su.Elem().Elem().Field(0)
624 if sv.Kind() == reflect.Ptr && sv.IsNil() {
627 dv := du.Elem().Elem().Field(0)
628 if dv.Kind() == reflect.Ptr && dv.IsNil() {
629 dv.Set(reflect.New(sv.Type().Elem())) // Initialize proto message if empty
631 switch sv.Type().Kind() {
632 case reflect.Ptr: // Proto struct (e.g., *T)
633 Merge(dv.Interface().(Message), sv.Interface().(Message))
634 case reflect.Slice: // E.g. Bytes type (e.g., []byte)
635 dv.Set(reflect.ValueOf(append([]byte{}, sv.Bytes()...)))
636 default: // Basic type (e.g., string)
643 panic(fmt.Sprintf("merger not found for type:%s", tf))
645 mi.fields = append(mi.fields, mfi)
648 mi.unrecognized = invalidField
649 if f, ok := t.FieldByName("XXX_unrecognized"); ok {
650 if f.Type != reflect.TypeOf([]byte{}) {
651 panic("expected XXX_unrecognized to be of type []byte")
653 mi.unrecognized = toField(&f)
656 atomic.StoreInt32(&mi.initialized, 1)