...
1
2
3
4
5 package proto
6
7 import (
8 "fmt"
9
10 "google.golang.org/protobuf/reflect/protoreflect"
11 "google.golang.org/protobuf/runtime/protoiface"
12 )
13
14
15
16
17
18
19
20
21
22
23
24
25 func Merge(dst, src Message) {
26
27
28
29 dstMsg, srcMsg := dst.ProtoReflect(), src.ProtoReflect()
30 if dstMsg.Descriptor() != srcMsg.Descriptor() {
31 if got, want := dstMsg.Descriptor().FullName(), srcMsg.Descriptor().FullName(); got != want {
32 panic(fmt.Sprintf("descriptor mismatch: %v != %v", got, want))
33 }
34 panic("descriptor mismatch")
35 }
36 mergeOptions{}.mergeMessage(dstMsg, srcMsg)
37 }
38
39
40
41 func Clone(m Message) Message {
42
43
44
45
46
47
48
49
50 if m == nil {
51 return nil
52 }
53 src := m.ProtoReflect()
54 if !src.IsValid() {
55 return src.Type().Zero().Interface()
56 }
57 dst := src.New()
58 mergeOptions{}.mergeMessage(dst, src)
59 return dst.Interface()
60 }
61
62
63
64 type mergeOptions struct{}
65
66 func (o mergeOptions) mergeMessage(dst, src protoreflect.Message) {
67 methods := protoMethods(dst)
68 if methods != nil && methods.Merge != nil {
69 in := protoiface.MergeInput{
70 Destination: dst,
71 Source: src,
72 }
73 out := methods.Merge(in)
74 if out.Flags&protoiface.MergeComplete != 0 {
75 return
76 }
77 }
78
79 if !dst.IsValid() {
80 panic(fmt.Sprintf("cannot merge into invalid %v message", dst.Descriptor().FullName()))
81 }
82
83 src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
84 switch {
85 case fd.IsList():
86 o.mergeList(dst.Mutable(fd).List(), v.List(), fd)
87 case fd.IsMap():
88 o.mergeMap(dst.Mutable(fd).Map(), v.Map(), fd.MapValue())
89 case fd.Message() != nil:
90 o.mergeMessage(dst.Mutable(fd).Message(), v.Message())
91 case fd.Kind() == protoreflect.BytesKind:
92 dst.Set(fd, o.cloneBytes(v))
93 default:
94 dst.Set(fd, v)
95 }
96 return true
97 })
98
99 if len(src.GetUnknown()) > 0 {
100 dst.SetUnknown(append(dst.GetUnknown(), src.GetUnknown()...))
101 }
102 }
103
104 func (o mergeOptions) mergeList(dst, src protoreflect.List, fd protoreflect.FieldDescriptor) {
105
106 for i, n := 0, src.Len(); i < n; i++ {
107 switch v := src.Get(i); {
108 case fd.Message() != nil:
109 dstv := dst.NewElement()
110 o.mergeMessage(dstv.Message(), v.Message())
111 dst.Append(dstv)
112 case fd.Kind() == protoreflect.BytesKind:
113 dst.Append(o.cloneBytes(v))
114 default:
115 dst.Append(v)
116 }
117 }
118 }
119
120 func (o mergeOptions) mergeMap(dst, src protoreflect.Map, fd protoreflect.FieldDescriptor) {
121
122 src.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
123 switch {
124 case fd.Message() != nil:
125 dstv := dst.NewValue()
126 o.mergeMessage(dstv.Message(), v.Message())
127 dst.Set(k, dstv)
128 case fd.Kind() == protoreflect.BytesKind:
129 dst.Set(k, o.cloneBytes(v))
130 default:
131 dst.Set(k, v)
132 }
133 return true
134 })
135 }
136
137 func (o mergeOptions) cloneBytes(v protoreflect.Value) protoreflect.Value {
138 return protoreflect.ValueOfBytes(append([]byte{}, v.Bytes()...))
139 }
140
View as plain text