merge.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 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 proto
  5. import "google.golang.org/protobuf/reflect/protoreflect"
  6. // Merge merges src into dst, which must be messages with the same descriptor.
  7. //
  8. // Populated scalar fields in src are copied to dst, while populated
  9. // singular messages in src are merged into dst by recursively calling Merge.
  10. // The elements of every list field in src is appended to the corresponded
  11. // list fields in dst. The entries of every map field in src is copied into
  12. // the corresponding map field in dst, possibly replacing existing entries.
  13. // The unknown fields of src are appended to the unknown fields of dst.
  14. func Merge(dst, src Message) {
  15. mergeMessage(dst.ProtoReflect(), src.ProtoReflect())
  16. }
  17. func mergeMessage(dst, src protoreflect.Message) {
  18. if dst.Descriptor() != src.Descriptor() {
  19. panic("descriptor mismatch")
  20. }
  21. src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  22. switch {
  23. case fd.IsList():
  24. mergeList(dst.Mutable(fd).List(), v.List(), fd)
  25. case fd.IsMap():
  26. mergeMap(dst.Mutable(fd).Map(), v.Map(), fd.MapValue())
  27. case fd.Message() != nil:
  28. mergeMessage(dst.Mutable(fd).Message(), v.Message())
  29. case fd.Kind() == protoreflect.BytesKind:
  30. dst.Set(fd, cloneBytes(v))
  31. default:
  32. dst.Set(fd, v)
  33. }
  34. return true
  35. })
  36. if len(src.GetUnknown()) > 0 {
  37. dst.SetUnknown(append(dst.GetUnknown(), src.GetUnknown()...))
  38. }
  39. }
  40. func mergeList(dst, src protoreflect.List, fd protoreflect.FieldDescriptor) {
  41. for i, n := 0, src.Len(); i < n; i++ {
  42. switch v := src.Get(i); {
  43. case fd.Message() != nil:
  44. dstv := dst.NewElement()
  45. mergeMessage(dstv.Message(), v.Message())
  46. dst.Append(dstv)
  47. case fd.Kind() == protoreflect.BytesKind:
  48. dst.Append(cloneBytes(v))
  49. default:
  50. dst.Append(v)
  51. }
  52. }
  53. }
  54. func mergeMap(dst, src protoreflect.Map, fd protoreflect.FieldDescriptor) {
  55. src.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  56. switch {
  57. case fd.Message() != nil:
  58. dstv := dst.NewValue()
  59. mergeMessage(dstv.Message(), v.Message())
  60. dst.Set(k, dstv) // may replace existing entry
  61. case fd.Kind() == protoreflect.BytesKind:
  62. dst.Set(k, cloneBytes(v))
  63. default:
  64. dst.Set(k, v)
  65. }
  66. return true
  67. })
  68. }
  69. func cloneBytes(v protoreflect.Value) protoreflect.Value {
  70. return protoreflect.ValueOfBytes(append([]byte{}, v.Bytes()...))
  71. }