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