merge.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2020 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. package impl
  5. import (
  6. "fmt"
  7. "reflect"
  8. "google.golang.org/protobuf/proto"
  9. "google.golang.org/protobuf/reflect/protoreflect"
  10. "google.golang.org/protobuf/runtime/protoiface"
  11. )
  12. type mergeOptions struct{}
  13. func (o mergeOptions) Merge(dst, src proto.Message) {
  14. proto.Merge(dst, src)
  15. }
  16. // merge is protoreflect.Methods.Merge.
  17. func (mi *MessageInfo) merge(in protoiface.MergeInput) protoiface.MergeOutput {
  18. dp, ok := mi.getPointer(in.Destination)
  19. if !ok {
  20. return protoiface.MergeOutput{}
  21. }
  22. sp, ok := mi.getPointer(in.Source)
  23. if !ok {
  24. return protoiface.MergeOutput{}
  25. }
  26. mi.mergePointer(dp, sp, mergeOptions{})
  27. return protoiface.MergeOutput{Flags: protoiface.MergeComplete}
  28. }
  29. func (mi *MessageInfo) mergePointer(dst, src pointer, opts mergeOptions) {
  30. mi.init()
  31. if dst.IsNil() {
  32. panic(fmt.Sprintf("invalid value: merging into nil message"))
  33. }
  34. if src.IsNil() {
  35. return
  36. }
  37. for _, f := range mi.orderedCoderFields {
  38. if f.funcs.merge == nil {
  39. continue
  40. }
  41. sfptr := src.Apply(f.offset)
  42. if f.isPointer && sfptr.Elem().IsNil() {
  43. continue
  44. }
  45. f.funcs.merge(dst.Apply(f.offset), sfptr, f, opts)
  46. }
  47. if mi.extensionOffset.IsValid() {
  48. sext := src.Apply(mi.extensionOffset).Extensions()
  49. dext := dst.Apply(mi.extensionOffset).Extensions()
  50. if *dext == nil {
  51. *dext = make(map[int32]ExtensionField)
  52. }
  53. for num, sx := range *sext {
  54. xt := sx.Type()
  55. xi := getExtensionFieldInfo(xt)
  56. if xi.funcs.merge == nil {
  57. continue
  58. }
  59. dx := (*dext)[num]
  60. var dv protoreflect.Value
  61. if dx.Type() == sx.Type() {
  62. dv = dx.Value()
  63. }
  64. if !dv.IsValid() && xi.unmarshalNeedsValue {
  65. dv = xt.New()
  66. }
  67. dv = xi.funcs.merge(dv, sx.Value(), opts)
  68. dx.Set(sx.Type(), dv)
  69. (*dext)[num] = dx
  70. }
  71. }
  72. if mi.unknownOffset.IsValid() {
  73. su := mi.getUnknownBytes(src)
  74. if su != nil && len(*su) > 0 {
  75. du := mi.mutableUnknownBytes(dst)
  76. *du = append(*du, *su...)
  77. }
  78. }
  79. }
  80. func mergeScalarValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  81. return src
  82. }
  83. func mergeBytesValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  84. return protoreflect.ValueOfBytes(append(emptyBuf[:], src.Bytes()...))
  85. }
  86. func mergeListValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  87. dstl := dst.List()
  88. srcl := src.List()
  89. for i, llen := 0, srcl.Len(); i < llen; i++ {
  90. dstl.Append(srcl.Get(i))
  91. }
  92. return dst
  93. }
  94. func mergeBytesListValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  95. dstl := dst.List()
  96. srcl := src.List()
  97. for i, llen := 0, srcl.Len(); i < llen; i++ {
  98. sb := srcl.Get(i).Bytes()
  99. db := append(emptyBuf[:], sb...)
  100. dstl.Append(protoreflect.ValueOfBytes(db))
  101. }
  102. return dst
  103. }
  104. func mergeMessageListValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  105. dstl := dst.List()
  106. srcl := src.List()
  107. for i, llen := 0, srcl.Len(); i < llen; i++ {
  108. sm := srcl.Get(i).Message()
  109. dm := proto.Clone(sm.Interface()).ProtoReflect()
  110. dstl.Append(protoreflect.ValueOfMessage(dm))
  111. }
  112. return dst
  113. }
  114. func mergeMessageValue(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value {
  115. opts.Merge(dst.Message().Interface(), src.Message().Interface())
  116. return dst
  117. }
  118. func mergeMessage(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
  119. if f.mi != nil {
  120. if dst.Elem().IsNil() {
  121. dst.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
  122. }
  123. f.mi.mergePointer(dst.Elem(), src.Elem(), opts)
  124. } else {
  125. dm := dst.AsValueOf(f.ft).Elem()
  126. sm := src.AsValueOf(f.ft).Elem()
  127. if dm.IsNil() {
  128. dm.Set(reflect.New(f.ft.Elem()))
  129. }
  130. opts.Merge(asMessage(dm), asMessage(sm))
  131. }
  132. }
  133. func mergeMessageSlice(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
  134. for _, sp := range src.PointerSlice() {
  135. dm := reflect.New(f.ft.Elem().Elem())
  136. if f.mi != nil {
  137. f.mi.mergePointer(pointerOfValue(dm), sp, opts)
  138. } else {
  139. opts.Merge(asMessage(dm), asMessage(sp.AsValueOf(f.ft.Elem().Elem())))
  140. }
  141. dst.AppendPointerSlice(pointerOfValue(dm))
  142. }
  143. }
  144. func mergeBytes(dst, src pointer, _ *coderFieldInfo, _ mergeOptions) {
  145. *dst.Bytes() = append(emptyBuf[:], *src.Bytes()...)
  146. }
  147. func mergeBytesNoZero(dst, src pointer, _ *coderFieldInfo, _ mergeOptions) {
  148. v := *src.Bytes()
  149. if len(v) > 0 {
  150. *dst.Bytes() = append(emptyBuf[:], v...)
  151. }
  152. }
  153. func mergeBytesSlice(dst, src pointer, _ *coderFieldInfo, _ mergeOptions) {
  154. ds := dst.BytesSlice()
  155. for _, v := range *src.BytesSlice() {
  156. *ds = append(*ds, append(emptyBuf[:], v...))
  157. }
  158. }