go-bot
322 строки · 9.9 Кб
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"google.golang.org/protobuf/encoding/protowire"
9"google.golang.org/protobuf/internal/encoding/messageset"
10"google.golang.org/protobuf/internal/order"
11"google.golang.org/protobuf/internal/pragma"
12"google.golang.org/protobuf/reflect/protoreflect"
13"google.golang.org/protobuf/runtime/protoiface"
14)
15
16// MarshalOptions configures the marshaler.
17//
18// Example usage:
19//
20// b, err := MarshalOptions{Deterministic: true}.Marshal(m)
21type MarshalOptions struct {
22pragma.NoUnkeyedLiterals
23
24// AllowPartial allows messages that have missing required fields to marshal
25// without returning an error. If AllowPartial is false (the default),
26// Marshal will return an error if there are any missing required fields.
27AllowPartial bool
28
29// Deterministic controls whether the same message will always be
30// serialized to the same bytes within the same binary.
31//
32// Setting this option guarantees that repeated serialization of
33// the same message will return the same bytes, and that different
34// processes of the same binary (which may be executing on different
35// machines) will serialize equal messages to the same bytes.
36// It has no effect on the resulting size of the encoded message compared
37// to a non-deterministic marshal.
38//
39// Note that the deterministic serialization is NOT canonical across
40// languages. It is not guaranteed to remain stable over time. It is
41// unstable across different builds with schema changes due to unknown
42// fields. Users who need canonical serialization (e.g., persistent
43// storage in a canonical form, fingerprinting, etc.) must define
44// their own canonicalization specification and implement their own
45// serializer rather than relying on this API.
46//
47// If deterministic serialization is requested, map entries will be
48// sorted by keys in lexographical order. This is an implementation
49// detail and subject to change.
50Deterministic bool
51
52// UseCachedSize indicates that the result of a previous Size call
53// may be reused.
54//
55// Setting this option asserts that:
56//
57// 1. Size has previously been called on this message with identical
58// options (except for UseCachedSize itself).
59//
60// 2. The message and all its submessages have not changed in any
61// way since the Size call.
62//
63// If either of these invariants is violated,
64// the results are undefined and may include panics or corrupted output.
65//
66// Implementations MAY take this option into account to provide
67// better performance, but there is no guarantee that they will do so.
68// There is absolutely no guarantee that Size followed by Marshal with
69// UseCachedSize set will perform equivalently to Marshal alone.
70UseCachedSize bool
71}
72
73// Marshal returns the wire-format encoding of m.
74func Marshal(m Message) ([]byte, error) {
75// Treat nil message interface as an empty message; nothing to output.
76if m == nil {
77return nil, nil
78}
79
80out, err := MarshalOptions{}.marshal(nil, m.ProtoReflect())
81if len(out.Buf) == 0 && err == nil {
82out.Buf = emptyBytesForMessage(m)
83}
84return out.Buf, err
85}
86
87// Marshal returns the wire-format encoding of m.
88func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
89// Treat nil message interface as an empty message; nothing to output.
90if m == nil {
91return nil, nil
92}
93
94out, err := o.marshal(nil, m.ProtoReflect())
95if len(out.Buf) == 0 && err == nil {
96out.Buf = emptyBytesForMessage(m)
97}
98return out.Buf, err
99}
100
101// emptyBytesForMessage returns a nil buffer if and only if m is invalid,
102// otherwise it returns a non-nil empty buffer.
103//
104// This is to assist the edge-case where user-code does the following:
105//
106// m1.OptionalBytes, _ = proto.Marshal(m2)
107//
108// where they expect the proto2 "optional_bytes" field to be populated
109// if any only if m2 is a valid message.
110func emptyBytesForMessage(m Message) []byte {
111if m == nil || !m.ProtoReflect().IsValid() {
112return nil
113}
114return emptyBuf[:]
115}
116
117// MarshalAppend appends the wire-format encoding of m to b,
118// returning the result.
119func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
120// Treat nil message interface as an empty message; nothing to append.
121if m == nil {
122return b, nil
123}
124
125out, err := o.marshal(b, m.ProtoReflect())
126return out.Buf, err
127}
128
129// MarshalState returns the wire-format encoding of a message.
130//
131// This method permits fine-grained control over the marshaler.
132// Most users should use Marshal instead.
133func (o MarshalOptions) MarshalState(in protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
134return o.marshal(in.Buf, in.Message)
135}
136
137// marshal is a centralized function that all marshal operations go through.
138// For profiling purposes, avoid changing the name of this function or
139// introducing other code paths for marshal that do not go through this.
140func (o MarshalOptions) marshal(b []byte, m protoreflect.Message) (out protoiface.MarshalOutput, err error) {
141allowPartial := o.AllowPartial
142o.AllowPartial = true
143if methods := protoMethods(m); methods != nil && methods.Marshal != nil &&
144!(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
145in := protoiface.MarshalInput{
146Message: m,
147Buf: b,
148}
149if o.Deterministic {
150in.Flags |= protoiface.MarshalDeterministic
151}
152if o.UseCachedSize {
153in.Flags |= protoiface.MarshalUseCachedSize
154}
155if methods.Size != nil {
156sout := methods.Size(protoiface.SizeInput{
157Message: m,
158Flags: in.Flags,
159})
160if cap(b) < len(b)+sout.Size {
161in.Buf = make([]byte, len(b), growcap(cap(b), len(b)+sout.Size))
162copy(in.Buf, b)
163}
164in.Flags |= protoiface.MarshalUseCachedSize
165}
166out, err = methods.Marshal(in)
167} else {
168out.Buf, err = o.marshalMessageSlow(b, m)
169}
170if err != nil {
171return out, err
172}
173if allowPartial {
174return out, nil
175}
176return out, checkInitialized(m)
177}
178
179func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
180out, err := o.marshal(b, m)
181return out.Buf, err
182}
183
184// growcap scales up the capacity of a slice.
185//
186// Given a slice with a current capacity of oldcap and a desired
187// capacity of wantcap, growcap returns a new capacity >= wantcap.
188//
189// The algorithm is mostly identical to the one used by append as of Go 1.14.
190func growcap(oldcap, wantcap int) (newcap int) {
191if wantcap > oldcap*2 {
192newcap = wantcap
193} else if oldcap < 1024 {
194// The Go 1.14 runtime takes this case when len(s) < 1024,
195// not when cap(s) < 1024. The difference doesn't seem
196// significant here.
197newcap = oldcap * 2
198} else {
199newcap = oldcap
200for 0 < newcap && newcap < wantcap {
201newcap += newcap / 4
202}
203if newcap <= 0 {
204newcap = wantcap
205}
206}
207return newcap
208}
209
210func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
211if messageset.IsMessageSet(m.Descriptor()) {
212return o.marshalMessageSet(b, m)
213}
214fieldOrder := order.AnyFieldOrder
215if o.Deterministic {
216// TODO: This should use a more natural ordering like NumberFieldOrder,
217// but doing so breaks golden tests that make invalid assumption about
218// output stability of this implementation.
219fieldOrder = order.LegacyFieldOrder
220}
221var err error
222order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
223b, err = o.marshalField(b, fd, v)
224return err == nil
225})
226if err != nil {
227return b, err
228}
229b = append(b, m.GetUnknown()...)
230return b, nil
231}
232
233func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
234switch {
235case fd.IsList():
236return o.marshalList(b, fd, value.List())
237case fd.IsMap():
238return o.marshalMap(b, fd, value.Map())
239default:
240b = protowire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
241return o.marshalSingular(b, fd, value)
242}
243}
244
245func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
246if fd.IsPacked() && list.Len() > 0 {
247b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
248b, pos := appendSpeculativeLength(b)
249for i, llen := 0, list.Len(); i < llen; i++ {
250var err error
251b, err = o.marshalSingular(b, fd, list.Get(i))
252if err != nil {
253return b, err
254}
255}
256b = finishSpeculativeLength(b, pos)
257return b, nil
258}
259
260kind := fd.Kind()
261for i, llen := 0, list.Len(); i < llen; i++ {
262var err error
263b = protowire.AppendTag(b, fd.Number(), wireTypes[kind])
264b, err = o.marshalSingular(b, fd, list.Get(i))
265if err != nil {
266return b, err
267}
268}
269return b, nil
270}
271
272func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
273keyf := fd.MapKey()
274valf := fd.MapValue()
275keyOrder := order.AnyKeyOrder
276if o.Deterministic {
277keyOrder = order.GenericKeyOrder
278}
279var err error
280order.RangeEntries(mapv, keyOrder, func(key protoreflect.MapKey, value protoreflect.Value) bool {
281b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
282var pos int
283b, pos = appendSpeculativeLength(b)
284
285b, err = o.marshalField(b, keyf, key.Value())
286if err != nil {
287return false
288}
289b, err = o.marshalField(b, valf, value)
290if err != nil {
291return false
292}
293b = finishSpeculativeLength(b, pos)
294return true
295})
296return b, err
297}
298
299// When encoding length-prefixed fields, we speculatively set aside some number of bytes
300// for the length, encode the data, and then encode the length (shifting the data if necessary
301// to make room).
302const speculativeLength = 1
303
304func appendSpeculativeLength(b []byte) ([]byte, int) {
305pos := len(b)
306b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
307return b, pos
308}
309
310func finishSpeculativeLength(b []byte, pos int) []byte {
311mlen := len(b) - pos - speculativeLength
312msiz := protowire.SizeVarint(uint64(mlen))
313if msiz != speculativeLength {
314for i := 0; i < msiz-speculativeLength; i++ {
315b = append(b, 0)
316}
317copy(b[pos+msiz:], b[pos+speculativeLength:])
318b = b[:pos+msiz+mlen]
319}
320protowire.AppendVarint(b[:pos], uint64(mlen))
321return b
322}
323