You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

merge.go 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 (
  6. "google.golang.org/protobuf/reflect/protoreflect"
  7. "google.golang.org/protobuf/runtime/protoiface"
  8. )
  9. // Merge merges src into dst, which must be a message with the same descriptor.
  10. //
  11. // Populated scalar fields in src are copied to dst, while populated
  12. // singular messages in src are merged into dst by recursively calling Merge.
  13. // The elements of every list field in src is appended to the corresponded
  14. // list fields in dst. The entries of every map field in src is copied into
  15. // the corresponding map field in dst, possibly replacing existing entries.
  16. // The unknown fields of src are appended to the unknown fields of dst.
  17. //
  18. // It is semantically equivalent to unmarshaling the encoded form of src
  19. // into dst with the UnmarshalOptions.Merge option specified.
  20. func Merge(dst, src Message) {
  21. // TODO: Should nil src be treated as semantically equivalent to a
  22. // untyped, read-only, empty message? What about a nil dst?
  23. dstMsg, srcMsg := dst.ProtoReflect(), src.ProtoReflect()
  24. if dstMsg.Descriptor() != srcMsg.Descriptor() {
  25. panic("descriptor mismatch")
  26. }
  27. mergeOptions{}.mergeMessage(dstMsg, srcMsg)
  28. }
  29. // Clone returns a deep copy of m.
  30. // If the top-level message is invalid, it returns an invalid message as well.
  31. func Clone(m Message) Message {
  32. // NOTE: Most usages of Clone assume the following properties:
  33. // t := reflect.TypeOf(m)
  34. // t == reflect.TypeOf(m.ProtoReflect().New().Interface())
  35. // t == reflect.TypeOf(m.ProtoReflect().Type().Zero().Interface())
  36. //
  37. // Embedding protobuf messages breaks this since the parent type will have
  38. // a forwarded ProtoReflect method, but the Interface method will return
  39. // the underlying embedded message type.
  40. if m == nil {
  41. return nil
  42. }
  43. src := m.ProtoReflect()
  44. if !src.IsValid() {
  45. return src.Type().Zero().Interface()
  46. }
  47. dst := src.New()
  48. mergeOptions{}.mergeMessage(dst, src)
  49. return dst.Interface()
  50. }
  51. // mergeOptions provides a namespace for merge functions, and can be
  52. // exported in the future if we add user-visible merge options.
  53. type mergeOptions struct{}
  54. func (o mergeOptions) mergeMessage(dst, src protoreflect.Message) {
  55. methods := protoMethods(dst)
  56. if methods != nil && methods.Merge != nil {
  57. in := protoiface.MergeInput{
  58. Destination: dst,
  59. Source: src,
  60. }
  61. out := methods.Merge(in)
  62. if out.Flags&protoiface.MergeComplete != 0 {
  63. return
  64. }
  65. }
  66. if !dst.IsValid() {
  67. panic("cannot merge into invalid destination message")
  68. }
  69. src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  70. switch {
  71. case fd.IsList():
  72. o.mergeList(dst.Mutable(fd).List(), v.List(), fd)
  73. case fd.IsMap():
  74. o.mergeMap(dst.Mutable(fd).Map(), v.Map(), fd.MapValue())
  75. case fd.Message() != nil:
  76. o.mergeMessage(dst.Mutable(fd).Message(), v.Message())
  77. case fd.Kind() == protoreflect.BytesKind:
  78. dst.Set(fd, o.cloneBytes(v))
  79. default:
  80. dst.Set(fd, v)
  81. }
  82. return true
  83. })
  84. if len(src.GetUnknown()) > 0 {
  85. dst.SetUnknown(append(dst.GetUnknown(), src.GetUnknown()...))
  86. }
  87. }
  88. func (o mergeOptions) mergeList(dst, src protoreflect.List, fd protoreflect.FieldDescriptor) {
  89. // Merge semantics appends to the end of the existing list.
  90. for i, n := 0, src.Len(); i < n; i++ {
  91. switch v := src.Get(i); {
  92. case fd.Message() != nil:
  93. dstv := dst.NewElement()
  94. o.mergeMessage(dstv.Message(), v.Message())
  95. dst.Append(dstv)
  96. case fd.Kind() == protoreflect.BytesKind:
  97. dst.Append(o.cloneBytes(v))
  98. default:
  99. dst.Append(v)
  100. }
  101. }
  102. }
  103. func (o mergeOptions) mergeMap(dst, src protoreflect.Map, fd protoreflect.FieldDescriptor) {
  104. // Merge semantics replaces, rather than merges into existing entries.
  105. src.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  106. switch {
  107. case fd.Message() != nil:
  108. dstv := dst.NewValue()
  109. o.mergeMessage(dstv.Message(), v.Message())
  110. dst.Set(k, dstv)
  111. case fd.Kind() == protoreflect.BytesKind:
  112. dst.Set(k, o.cloneBytes(v))
  113. default:
  114. dst.Set(k, v)
  115. }
  116. return true
  117. })
  118. }
  119. func (o mergeOptions) cloneBytes(v protoreflect.Value) protoreflect.Value {
  120. return protoreflect.ValueOfBytes(append([]byte{}, v.Bytes()...))
  121. }