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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. dstMsg, srcMsg := dst.ProtoReflect(), src.ProtoReflect()
  22. if dstMsg.Descriptor() != srcMsg.Descriptor() {
  23. panic("descriptor mismatch")
  24. }
  25. mergeOptions{}.mergeMessage(dstMsg, srcMsg)
  26. }
  27. // Clone returns a deep copy of m.
  28. // If the top-level message is invalid, it returns an invalid message as well.
  29. func Clone(m Message) Message {
  30. // NOTE: Most usages of Clone assume the following properties:
  31. // t := reflect.TypeOf(m)
  32. // t == reflect.TypeOf(m.ProtoReflect().New().Interface())
  33. // t == reflect.TypeOf(m.ProtoReflect().Type().Zero().Interface())
  34. //
  35. // Embedding protobuf messages breaks this since the parent type will have
  36. // a forwarded ProtoReflect method, but the Interface method will return
  37. // the underlying embedded message type.
  38. if m == nil {
  39. return nil
  40. }
  41. src := m.ProtoReflect()
  42. if !src.IsValid() {
  43. return src.Type().Zero().Interface()
  44. }
  45. dst := src.New()
  46. mergeOptions{}.mergeMessage(dst, src)
  47. return dst.Interface()
  48. }
  49. // mergeOptions provides a namespace for merge functions, and can be
  50. // exported in the future if we add user-visible merge options.
  51. type mergeOptions struct{}
  52. func (o mergeOptions) mergeMessage(dst, src protoreflect.Message) {
  53. methods := protoMethods(dst)
  54. if methods != nil && methods.Merge != nil {
  55. in := protoiface.MergeInput{
  56. Destination: dst,
  57. Source: src,
  58. }
  59. out := methods.Merge(in)
  60. if out.Flags&protoiface.MergeComplete != 0 {
  61. return
  62. }
  63. }
  64. if !dst.IsValid() {
  65. panic("cannot merge into invalid destination message")
  66. }
  67. src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  68. switch {
  69. case fd.IsList():
  70. o.mergeList(dst.Mutable(fd).List(), v.List(), fd)
  71. case fd.IsMap():
  72. o.mergeMap(dst.Mutable(fd).Map(), v.Map(), fd.MapValue())
  73. case fd.Message() != nil:
  74. o.mergeMessage(dst.Mutable(fd).Message(), v.Message())
  75. case fd.Kind() == protoreflect.BytesKind:
  76. dst.Set(fd, o.cloneBytes(v))
  77. default:
  78. dst.Set(fd, v)
  79. }
  80. return true
  81. })
  82. if len(src.GetUnknown()) > 0 {
  83. dst.SetUnknown(append(dst.GetUnknown(), src.GetUnknown()...))
  84. }
  85. }
  86. func (o mergeOptions) mergeList(dst, src protoreflect.List, fd protoreflect.FieldDescriptor) {
  87. // Merge semantics appends to the end of the existing list.
  88. for i, n := 0, src.Len(); i < n; i++ {
  89. switch v := src.Get(i); {
  90. case fd.Message() != nil:
  91. dstv := dst.NewElement()
  92. o.mergeMessage(dstv.Message(), v.Message())
  93. dst.Append(dstv)
  94. case fd.Kind() == protoreflect.BytesKind:
  95. dst.Append(o.cloneBytes(v))
  96. default:
  97. dst.Append(v)
  98. }
  99. }
  100. }
  101. func (o mergeOptions) mergeMap(dst, src protoreflect.Map, fd protoreflect.FieldDescriptor) {
  102. // Merge semantics replaces, rather than merges into existing entries.
  103. src.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  104. switch {
  105. case fd.Message() != nil:
  106. dstv := dst.NewValue()
  107. o.mergeMessage(dstv.Message(), v.Message())
  108. dst.Set(k, dstv)
  109. case fd.Kind() == protoreflect.BytesKind:
  110. dst.Set(k, o.cloneBytes(v))
  111. default:
  112. dst.Set(k, v)
  113. }
  114. return true
  115. })
  116. }
  117. func (o mergeOptions) cloneBytes(v protoreflect.Value) protoreflect.Value {
  118. return protoreflect.ValueOfBytes(append([]byte{}, v.Bytes()...))
  119. }