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