blob: 401c1143c88389d334037615ffd8e75fec009af8 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2010 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
Rob Pikeaaa3a622010-03-20 22:32:34 -07005//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto
33
34/*
35 * Routines for encoding data into the wire format for protocol buffers.
36 */
37
38import (
Rob Pikea17fdd92011-11-02 12:43:05 -070039 "errors"
David Symonds4646c372013-09-09 13:18:58 +100040 "fmt"
Rob Pikeaaa3a622010-03-20 22:32:34 -070041 "reflect"
David Symondsdf583ae2012-12-06 14:06:53 +110042 "sort"
Rob Pikeaaa3a622010-03-20 22:32:34 -070043)
44
David Symondse583a5f2013-09-27 10:02:37 +100045// RequiredNotSetError is the error returned if Marshal is called with
Rob Pikeaaa3a622010-03-20 22:32:34 -070046// a protocol buffer struct whose required fields have not
Rob Pikec6d8e4a2010-07-28 15:34:32 -070047// all been initialized. It is also the error returned if Unmarshal is
48// called with an encoded protocol buffer that does not include all the
49// required fields.
David Symonds4646c372013-09-09 13:18:58 +100050//
David Symondse583a5f2013-09-27 10:02:37 +100051// When printed, RequiredNotSetError reports the first unset required field in a
David Symonds4646c372013-09-09 13:18:58 +100052// message. If the field cannot be precisely determined, it is reported as
53// "{Unknown}".
David Symondse583a5f2013-09-27 10:02:37 +100054type RequiredNotSetError struct {
David Symonds4646c372013-09-09 13:18:58 +100055 field string
David Symonds5b7775e2010-12-01 10:09:04 +110056}
57
David Symondse583a5f2013-09-27 10:02:37 +100058func (e *RequiredNotSetError) Error() string {
David Symonds4646c372013-09-09 13:18:58 +100059 return fmt.Sprintf("proto: required field %q not set", e.field)
David Symonds5b7775e2010-12-01 10:09:04 +110060}
Rob Pikeaaa3a622010-03-20 22:32:34 -070061
David Symonds7656e742011-07-22 14:54:17 +100062var (
David Symondsf62db482015-02-23 12:37:00 +110063 // errRepeatedHasNil is the error returned if Marshal is called with
David Symonds7656e742011-07-22 14:54:17 +100064 // a struct with a repeated field containing a nil element.
David Symondsf62db482015-02-23 12:37:00 +110065 errRepeatedHasNil = errors.New("proto: repeated field has nil element")
Rob Pikeaaa3a622010-03-20 22:32:34 -070066
Bryan Mills8d92cf52016-03-19 05:57:00 +110067 // errOneofHasNil is the error returned if Marshal is called with
68 // a struct with a oneof field containing a nil element.
69 errOneofHasNil = errors.New("proto: oneof field has nil value")
70
David Symonds7656e742011-07-22 14:54:17 +100071 // ErrNil is the error returned if Marshal is called with nil.
Rob Pikea17fdd92011-11-02 12:43:05 -070072 ErrNil = errors.New("proto: Marshal called with nil")
Ross Lightcd85f192016-05-19 15:50:37 -040073
74 // ErrTooLarge is the error returned if Marshal is called with a
75 // message that encodes to >2GB.
76 ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
David Symonds7656e742011-07-22 14:54:17 +100077)
Rob Pikeaaa3a622010-03-20 22:32:34 -070078
79// The fundamental encoders that put bytes on the wire.
80// Those that take integer types all accept uint64 and are
81// therefore of type valueEncoder.
82
David Symonds4fee3b12010-11-11 10:00:13 +110083const maxVarintBytes = 10 // maximum length of a varint
84
Ross Lightcd85f192016-05-19 15:50:37 -040085// maxMarshalSize is the largest allowed size of an encoded protobuf,
86// since C++ and Java use signed int32s for the size.
87const maxMarshalSize = 1<<31 - 1
88
Rob Pikeaaa3a622010-03-20 22:32:34 -070089// EncodeVarint returns the varint encoding of x.
90// This is the format for the
91// int32, int64, uint32, uint64, bool, and enum
92// protocol buffer types.
93// Not used by the package itself, but helpful to clients
94// wishing to use the same encoding.
95func EncodeVarint(x uint64) []byte {
David Symonds4fee3b12010-11-11 10:00:13 +110096 var buf [maxVarintBytes]byte
Rob Pikeaaa3a622010-03-20 22:32:34 -070097 var n int
98 for n = 0; x > 127; n++ {
99 buf[n] = 0x80 | uint8(x&0x7F)
100 x >>= 7
101 }
102 buf[n] = uint8(x)
103 n++
104 return buf[0:n]
105}
106
107// EncodeVarint writes a varint-encoded integer to the Buffer.
108// This is the format for the
109// int32, int64, uint32, uint64, bool, and enum
110// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700111func (p *Buffer) EncodeVarint(x uint64) error {
David Symonds4fee3b12010-11-11 10:00:13 +1100112 for x >= 1<<7 {
David Symondsd9da6ba2011-08-30 14:41:30 +1000113 p.buf = append(p.buf, uint8(x&0x7f|0x80))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700114 x >>= 7
115 }
David Symondsd9da6ba2011-08-30 14:41:30 +1000116 p.buf = append(p.buf, uint8(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700117 return nil
118}
119
Damien Neil08794902015-12-04 07:39:00 +1100120// SizeVarint returns the varint encoding size of an integer.
121func SizeVarint(x uint64) int {
122 return sizeVarint(x)
123}
124
David Symonds0bf1ad52013-10-11 09:07:50 +1100125func sizeVarint(x uint64) (n int) {
126 for {
127 n++
128 x >>= 7
129 if x == 0 {
130 break
131 }
132 }
133 return n
134}
135
Rob Pikeaaa3a622010-03-20 22:32:34 -0700136// EncodeFixed64 writes a 64-bit integer to the Buffer.
137// This is the format for the
138// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700139func (p *Buffer) EncodeFixed64(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000140 p.buf = append(p.buf,
141 uint8(x),
142 uint8(x>>8),
143 uint8(x>>16),
144 uint8(x>>24),
145 uint8(x>>32),
146 uint8(x>>40),
147 uint8(x>>48),
148 uint8(x>>56))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700149 return nil
150}
151
David Symonds0bf1ad52013-10-11 09:07:50 +1100152func sizeFixed64(x uint64) int {
153 return 8
154}
155
Rob Pikeaaa3a622010-03-20 22:32:34 -0700156// EncodeFixed32 writes a 32-bit integer to the Buffer.
157// This is the format for the
158// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700159func (p *Buffer) EncodeFixed32(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000160 p.buf = append(p.buf,
161 uint8(x),
162 uint8(x>>8),
163 uint8(x>>16),
164 uint8(x>>24))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700165 return nil
166}
167
David Symonds0bf1ad52013-10-11 09:07:50 +1100168func sizeFixed32(x uint64) int {
169 return 4
170}
171
Rob Pikeaaa3a622010-03-20 22:32:34 -0700172// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
173// to the Buffer.
174// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700175func (p *Buffer) EncodeZigzag64(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700176 // use signed number to get arithmetic right shift.
177 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
178}
179
David Symonds0bf1ad52013-10-11 09:07:50 +1100180func sizeZigzag64(x uint64) int {
181 return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
182}
183
Rob Pikeaaa3a622010-03-20 22:32:34 -0700184// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
185// to the Buffer.
186// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700187func (p *Buffer) EncodeZigzag32(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700188 // use signed number to get arithmetic right shift.
189 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
190}
191
David Symonds0bf1ad52013-10-11 09:07:50 +1100192func sizeZigzag32(x uint64) int {
193 return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
194}
195
Rob Pikeaaa3a622010-03-20 22:32:34 -0700196// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
197// This is the format used for the bytes protocol buffer
198// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700199func (p *Buffer) EncodeRawBytes(b []byte) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700200 p.EncodeVarint(uint64(len(b)))
Rob Pike99fa2b62010-12-02 10:39:42 -0800201 p.buf = append(p.buf, b...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700202 return nil
203}
204
David Symonds0bf1ad52013-10-11 09:07:50 +1100205func sizeRawBytes(b []byte) int {
206 return sizeVarint(uint64(len(b))) +
207 len(b)
208}
209
Rob Pikeaaa3a622010-03-20 22:32:34 -0700210// EncodeStringBytes writes an encoded string to the Buffer.
211// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700212func (p *Buffer) EncodeStringBytes(s string) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700213 p.EncodeVarint(uint64(len(s)))
214 p.buf = append(p.buf, s...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700215 return nil
216}
217
David Symonds0bf1ad52013-10-11 09:07:50 +1100218func sizeStringBytes(s string) int {
219 return sizeVarint(uint64(len(s))) +
220 len(s)
221}
222
Rob Pikeaaa3a622010-03-20 22:32:34 -0700223// Marshaler is the interface representing objects that can marshal themselves.
224type Marshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700225 Marshal() ([]byte, error)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700226}
227
David Symonds9f60f432012-06-14 09:45:25 +1000228// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700229// and encodes it into the wire format, returning the data.
David Symonds9f60f432012-06-14 09:45:25 +1000230func Marshal(pb Message) ([]byte, error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700231 // Can the object marshal itself?
232 if m, ok := pb.(Marshaler); ok {
233 return m.Marshal()
234 }
235 p := NewBuffer(nil)
236 err := p.Marshal(pb)
David Symonds4646c372013-09-09 13:18:58 +1000237 var state errorState
238 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700239 return nil, err
240 }
David Symonds8b253302014-01-14 16:24:19 +1100241 if p.buf == nil && err == nil {
242 // Return a non-nil slice on success.
243 return []byte{}, nil
244 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700245 return p.buf, err
246}
247
David Symonds59b73b32015-08-24 13:22:02 +1000248// EncodeMessage writes the protocol buffer to the Buffer,
249// prefixed by a varint-encoded length.
250func (p *Buffer) EncodeMessage(pb Message) error {
251 t, base, err := getbase(pb)
252 if structPointer_IsNil(base) {
253 return ErrNil
254 }
255 if err == nil {
256 var state errorState
257 err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
258 }
259 return err
260}
261
David Symonds9f60f432012-06-14 09:45:25 +1000262// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700263// and encodes it into the wire format, writing the result to the
264// Buffer.
David Symonds9f60f432012-06-14 09:45:25 +1000265func (p *Buffer) Marshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700266 // Can the object marshal itself?
267 if m, ok := pb.(Marshaler); ok {
268 data, err := m.Marshal()
269 if err != nil {
270 return err
271 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800272 p.buf = append(p.buf, data...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700273 return nil
274 }
275
Russ Coxd4ce3f12012-09-12 10:36:26 +1000276 t, base, err := getbase(pb)
277 if structPointer_IsNil(base) {
David Symondsd4661c52012-08-30 15:17:53 +1000278 return ErrNil
279 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700280 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100281 err = p.enc_struct(GetProperties(t.Elem()), base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700282 }
283
David Symonds9f60f432012-06-14 09:45:25 +1000284 if collectStats {
285 stats.Encode++
286 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700287
Ross Lightcd85f192016-05-19 15:50:37 -0400288 if len(p.buf) > maxMarshalSize {
289 return ErrTooLarge
290 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700291 return err
292}
293
David Symonds0bf1ad52013-10-11 09:07:50 +1100294// Size returns the encoded size of a protocol buffer.
295func Size(pb Message) (n int) {
296 // Can the object marshal itself? If so, Size is slow.
297 // TODO: add Size to Marshaler, or add a Sizer interface.
298 if m, ok := pb.(Marshaler); ok {
299 b, _ := m.Marshal()
300 return len(b)
301 }
302
303 t, base, err := getbase(pb)
304 if structPointer_IsNil(base) {
305 return 0
306 }
307 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100308 n = size_struct(GetProperties(t.Elem()), base)
David Symonds0bf1ad52013-10-11 09:07:50 +1100309 }
310
311 if collectStats {
312 stats.Size++
313 }
314
315 return
316}
317
Rob Pikeaaa3a622010-03-20 22:32:34 -0700318// Individual type encoders.
319
320// Encode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000321func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
322 v := *structPointer_Bool(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700323 if v == nil {
324 return ErrNil
325 }
Rob Pike0f42a272011-10-20 16:03:11 -0700326 x := 0
327 if *v {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700328 x = 1
329 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800330 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700331 p.valEnc(o, uint64(x))
332 return nil
333}
334
David Symondsabd3b412014-11-28 11:43:44 +1100335func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
336 v := *structPointer_BoolVal(base, p.field)
337 if !v {
338 return ErrNil
339 }
340 o.buf = append(o.buf, p.tagcode...)
341 p.valEnc(o, 1)
342 return nil
343}
344
David Symonds0bf1ad52013-10-11 09:07:50 +1100345func size_bool(p *Properties, base structPointer) int {
346 v := *structPointer_Bool(base, p.field)
347 if v == nil {
348 return 0
349 }
350 return len(p.tagcode) + 1 // each bool takes exactly one byte
351}
352
David Symondsabd3b412014-11-28 11:43:44 +1100353func size_proto3_bool(p *Properties, base structPointer) int {
354 v := *structPointer_BoolVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000355 if !v && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100356 return 0
357 }
358 return len(p.tagcode) + 1 // each bool takes exactly one byte
359}
360
Rob Pikeaaa3a622010-03-20 22:32:34 -0700361// Encode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000362func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
363 v := structPointer_Word32(base, p.field)
364 if word32_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700365 return ErrNil
366 }
David Symondsf054e842014-07-22 14:06:27 +1000367 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
Rob Pike99fa2b62010-12-02 10:39:42 -0800368 o.buf = append(o.buf, p.tagcode...)
David Symondsc31645c2013-06-22 17:57:36 +1000369 p.valEnc(o, uint64(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700370 return nil
371}
372
David Symondsabd3b412014-11-28 11:43:44 +1100373func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
374 v := structPointer_Word32Val(base, p.field)
375 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
376 if x == 0 {
377 return ErrNil
378 }
379 o.buf = append(o.buf, p.tagcode...)
380 p.valEnc(o, uint64(x))
381 return nil
382}
383
David Symonds0bf1ad52013-10-11 09:07:50 +1100384func size_int32(p *Properties, base structPointer) (n int) {
385 v := structPointer_Word32(base, p.field)
386 if word32_IsNil(v) {
387 return 0
388 }
David Symondsf054e842014-07-22 14:06:27 +1000389 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
390 n += len(p.tagcode)
391 n += p.valSize(uint64(x))
392 return
393}
394
David Symondsabd3b412014-11-28 11:43:44 +1100395func size_proto3_int32(p *Properties, base structPointer) (n int) {
396 v := structPointer_Word32Val(base, p.field)
397 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
David Symonds535a1042015-09-11 08:27:00 +1000398 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100399 return 0
400 }
401 n += len(p.tagcode)
402 n += p.valSize(uint64(x))
403 return
404}
405
David Symondsf054e842014-07-22 14:06:27 +1000406// Encode a uint32.
407// Exactly the same as int32, except for no sign extension.
408func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
409 v := structPointer_Word32(base, p.field)
410 if word32_IsNil(v) {
411 return ErrNil
412 }
413 x := word32_Get(v)
414 o.buf = append(o.buf, p.tagcode...)
415 p.valEnc(o, uint64(x))
416 return nil
417}
418
David Symondsabd3b412014-11-28 11:43:44 +1100419func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
420 v := structPointer_Word32Val(base, p.field)
421 x := word32Val_Get(v)
422 if x == 0 {
423 return ErrNil
424 }
425 o.buf = append(o.buf, p.tagcode...)
426 p.valEnc(o, uint64(x))
427 return nil
428}
429
David Symondsf054e842014-07-22 14:06:27 +1000430func size_uint32(p *Properties, base structPointer) (n int) {
431 v := structPointer_Word32(base, p.field)
432 if word32_IsNil(v) {
433 return 0
434 }
David Symonds0bf1ad52013-10-11 09:07:50 +1100435 x := word32_Get(v)
436 n += len(p.tagcode)
437 n += p.valSize(uint64(x))
438 return
439}
440
David Symondsabd3b412014-11-28 11:43:44 +1100441func size_proto3_uint32(p *Properties, base structPointer) (n int) {
442 v := structPointer_Word32Val(base, p.field)
443 x := word32Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000444 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100445 return 0
446 }
447 n += len(p.tagcode)
448 n += p.valSize(uint64(x))
449 return
450}
451
Rob Pikeaaa3a622010-03-20 22:32:34 -0700452// Encode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000453func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
454 v := structPointer_Word64(base, p.field)
455 if word64_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700456 return ErrNil
457 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000458 x := word64_Get(v)
Rob Pike99fa2b62010-12-02 10:39:42 -0800459 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000460 p.valEnc(o, x)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700461 return nil
462}
463
David Symondsabd3b412014-11-28 11:43:44 +1100464func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
465 v := structPointer_Word64Val(base, p.field)
466 x := word64Val_Get(v)
467 if x == 0 {
468 return ErrNil
469 }
470 o.buf = append(o.buf, p.tagcode...)
471 p.valEnc(o, x)
472 return nil
473}
474
David Symonds0bf1ad52013-10-11 09:07:50 +1100475func size_int64(p *Properties, base structPointer) (n int) {
476 v := structPointer_Word64(base, p.field)
477 if word64_IsNil(v) {
478 return 0
479 }
480 x := word64_Get(v)
481 n += len(p.tagcode)
482 n += p.valSize(x)
483 return
484}
485
David Symondsabd3b412014-11-28 11:43:44 +1100486func size_proto3_int64(p *Properties, base structPointer) (n int) {
487 v := structPointer_Word64Val(base, p.field)
488 x := word64Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000489 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100490 return 0
491 }
492 n += len(p.tagcode)
493 n += p.valSize(x)
494 return
495}
496
Rob Pikeaaa3a622010-03-20 22:32:34 -0700497// Encode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000498func (o *Buffer) enc_string(p *Properties, base structPointer) error {
499 v := *structPointer_String(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700500 if v == nil {
501 return ErrNil
502 }
503 x := *v
Rob Pike99fa2b62010-12-02 10:39:42 -0800504 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700505 o.EncodeStringBytes(x)
506 return nil
507}
508
David Symondsabd3b412014-11-28 11:43:44 +1100509func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
510 v := *structPointer_StringVal(base, p.field)
511 if v == "" {
512 return ErrNil
513 }
514 o.buf = append(o.buf, p.tagcode...)
515 o.EncodeStringBytes(v)
516 return nil
517}
518
David Symonds0bf1ad52013-10-11 09:07:50 +1100519func size_string(p *Properties, base structPointer) (n int) {
520 v := *structPointer_String(base, p.field)
521 if v == nil {
522 return 0
523 }
524 x := *v
525 n += len(p.tagcode)
526 n += sizeStringBytes(x)
527 return
528}
529
David Symondsabd3b412014-11-28 11:43:44 +1100530func size_proto3_string(p *Properties, base structPointer) (n int) {
531 v := *structPointer_StringVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000532 if v == "" && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100533 return 0
534 }
535 n += len(p.tagcode)
536 n += sizeStringBytes(v)
537 return
538}
539
Rob Pike97e934d2011-04-11 12:52:49 -0700540// All protocol buffer fields are nillable, but be careful.
541func isNil(v reflect.Value) bool {
542 switch v.Kind() {
David Symonds007ed9d2012-07-24 10:59:36 +1000543 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
Rob Pike97e934d2011-04-11 12:52:49 -0700544 return v.IsNil()
545 }
546 return false
547}
548
Rob Pikeaaa3a622010-03-20 22:32:34 -0700549// Encode a message struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000550func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000551 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000552 structp := structPointer_GetStructPointer(base, p.field)
553 if structPointer_IsNil(structp) {
David Symondsa80b2822012-03-14 14:31:25 +1100554 return ErrNil
555 }
556
Rob Pikeaaa3a622010-03-20 22:32:34 -0700557 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100558 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000559 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700560 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000561 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700562 return err
563 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800564 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700565 o.EncodeRawBytes(data)
David Symonds68c687d2015-07-27 19:06:00 +1000566 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700567 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700568
Rob Pike99fa2b62010-12-02 10:39:42 -0800569 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100570 return o.enc_len_struct(p.sprop, structp, &state)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700571}
572
David Symonds0bf1ad52013-10-11 09:07:50 +1100573func size_struct_message(p *Properties, base structPointer) int {
574 structp := structPointer_GetStructPointer(base, p.field)
575 if structPointer_IsNil(structp) {
576 return 0
577 }
578
579 // Can the object marshal itself?
580 if p.isMarshaler {
581 m := structPointer_Interface(structp, p.stype).(Marshaler)
582 data, _ := m.Marshal()
583 n0 := len(p.tagcode)
584 n1 := sizeRawBytes(data)
585 return n0 + n1
586 }
587
588 n0 := len(p.tagcode)
David Symondsba7896c2014-11-20 15:29:05 +1100589 n1 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +1100590 n2 := sizeVarint(uint64(n1)) // size of encoded length
591 return n0 + n1 + n2
592}
593
Rob Pikeaaa3a622010-03-20 22:32:34 -0700594// Encode a group struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000595func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000596 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000597 b := structPointer_GetStructPointer(base, p.field)
598 if structPointer_IsNil(b) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700599 return ErrNil
600 }
601
602 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100603 err := o.enc_struct(p.sprop, b)
David Symonds4646c372013-09-09 13:18:58 +1000604 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700605 return err
606 }
607 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
David Symonds4646c372013-09-09 13:18:58 +1000608 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700609}
610
David Symonds0bf1ad52013-10-11 09:07:50 +1100611func size_struct_group(p *Properties, base structPointer) (n int) {
612 b := structPointer_GetStructPointer(base, p.field)
613 if structPointer_IsNil(b) {
614 return 0
615 }
616
617 n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100618 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +1100619 n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
620 return
621}
622
Rob Pikeaaa3a622010-03-20 22:32:34 -0700623// Encode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000624func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
625 s := *structPointer_BoolSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700626 l := len(s)
627 if l == 0 {
628 return ErrNil
629 }
630 for _, x := range s {
Rob Pike99fa2b62010-12-02 10:39:42 -0800631 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000632 v := uint64(0)
633 if x {
634 v = 1
Rob Pikeaaa3a622010-03-20 22:32:34 -0700635 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000636 p.valEnc(o, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700637 }
638 return nil
639}
640
David Symonds0bf1ad52013-10-11 09:07:50 +1100641func size_slice_bool(p *Properties, base structPointer) int {
642 s := *structPointer_BoolSlice(base, p.field)
643 l := len(s)
644 if l == 0 {
645 return 0
646 }
647 return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
648}
649
David Symonds5b7775e2010-12-01 10:09:04 +1100650// Encode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000651func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
652 s := *structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100653 l := len(s)
654 if l == 0 {
655 return ErrNil
656 }
657 o.buf = append(o.buf, p.tagcode...)
658 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
659 for _, x := range s {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000660 v := uint64(0)
661 if x {
662 v = 1
David Symonds5b7775e2010-12-01 10:09:04 +1100663 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000664 p.valEnc(o, v)
David Symonds5b7775e2010-12-01 10:09:04 +1100665 }
666 return nil
667}
668
David Symonds0bf1ad52013-10-11 09:07:50 +1100669func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
670 s := *structPointer_BoolSlice(base, p.field)
671 l := len(s)
672 if l == 0 {
673 return 0
674 }
675 n += len(p.tagcode)
676 n += sizeVarint(uint64(l))
677 n += l // each bool takes exactly one byte
678 return
679}
680
Rob Pikeaaa3a622010-03-20 22:32:34 -0700681// Encode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000682func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
683 s := *structPointer_Bytes(base, p.field)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800684 if s == nil {
685 return ErrNil
686 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800687 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700688 o.EncodeRawBytes(s)
689 return nil
690}
691
David Symondsabd3b412014-11-28 11:43:44 +1100692func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
693 s := *structPointer_Bytes(base, p.field)
694 if len(s) == 0 {
695 return ErrNil
696 }
697 o.buf = append(o.buf, p.tagcode...)
698 o.EncodeRawBytes(s)
699 return nil
700}
701
David Symonds0bf1ad52013-10-11 09:07:50 +1100702func size_slice_byte(p *Properties, base structPointer) (n int) {
703 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000704 if s == nil && !p.oneof {
David Symonds0bf1ad52013-10-11 09:07:50 +1100705 return 0
706 }
707 n += len(p.tagcode)
708 n += sizeRawBytes(s)
709 return
710}
711
David Symondsabd3b412014-11-28 11:43:44 +1100712func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
713 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000714 if len(s) == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100715 return 0
716 }
717 n += len(p.tagcode)
718 n += sizeRawBytes(s)
719 return
720}
721
Rob Pikeaaa3a622010-03-20 22:32:34 -0700722// Encode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000723func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
724 s := structPointer_Word32Slice(base, p.field)
725 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700726 if l == 0 {
727 return ErrNil
728 }
729 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800730 o.buf = append(o.buf, p.tagcode...)
David Symonds0ec36a22014-08-12 13:21:46 +1000731 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
Rob Pikeaaa3a622010-03-20 22:32:34 -0700732 p.valEnc(o, uint64(x))
733 }
734 return nil
735}
736
David Symonds0bf1ad52013-10-11 09:07:50 +1100737func size_slice_int32(p *Properties, base structPointer) (n int) {
738 s := structPointer_Word32Slice(base, p.field)
739 l := s.Len()
740 if l == 0 {
741 return 0
742 }
743 for i := 0; i < l; i++ {
744 n += len(p.tagcode)
David Symonds0ec36a22014-08-12 13:21:46 +1000745 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
David Symonds0bf1ad52013-10-11 09:07:50 +1100746 n += p.valSize(uint64(x))
747 }
748 return
749}
750
David Symonds5b7775e2010-12-01 10:09:04 +1100751// Encode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000752func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
753 s := structPointer_Word32Slice(base, p.field)
754 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100755 if l == 0 {
756 return ErrNil
757 }
758 // TODO: Reuse a Buffer.
759 buf := NewBuffer(nil)
760 for i := 0; i < l; i++ {
David Symonds0ec36a22014-08-12 13:21:46 +1000761 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
762 p.valEnc(buf, uint64(x))
763 }
764
765 o.buf = append(o.buf, p.tagcode...)
766 o.EncodeVarint(uint64(len(buf.buf)))
767 o.buf = append(o.buf, buf.buf...)
768 return nil
769}
770
771func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
772 s := structPointer_Word32Slice(base, p.field)
773 l := s.Len()
774 if l == 0 {
775 return 0
776 }
777 var bufSize int
778 for i := 0; i < l; i++ {
779 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
780 bufSize += p.valSize(uint64(x))
781 }
782
783 n += len(p.tagcode)
784 n += sizeVarint(uint64(bufSize))
785 n += bufSize
786 return
787}
788
789// Encode a slice of uint32s ([]uint32).
790// Exactly the same as int32, except for no sign extension.
791func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
792 s := structPointer_Word32Slice(base, p.field)
793 l := s.Len()
794 if l == 0 {
795 return ErrNil
796 }
797 for i := 0; i < l; i++ {
798 o.buf = append(o.buf, p.tagcode...)
799 x := s.Index(i)
800 p.valEnc(o, uint64(x))
801 }
802 return nil
803}
804
805func size_slice_uint32(p *Properties, base structPointer) (n int) {
806 s := structPointer_Word32Slice(base, p.field)
807 l := s.Len()
808 if l == 0 {
809 return 0
810 }
811 for i := 0; i < l; i++ {
812 n += len(p.tagcode)
813 x := s.Index(i)
814 n += p.valSize(uint64(x))
815 }
816 return
817}
818
819// Encode a slice of uint32s ([]uint32) in packed format.
820// Exactly the same as int32, except for no sign extension.
821func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
822 s := structPointer_Word32Slice(base, p.field)
823 l := s.Len()
824 if l == 0 {
825 return ErrNil
826 }
827 // TODO: Reuse a Buffer.
828 buf := NewBuffer(nil)
829 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000830 p.valEnc(buf, uint64(s.Index(i)))
David Symonds5b7775e2010-12-01 10:09:04 +1100831 }
832
833 o.buf = append(o.buf, p.tagcode...)
834 o.EncodeVarint(uint64(len(buf.buf)))
835 o.buf = append(o.buf, buf.buf...)
836 return nil
837}
838
David Symonds0ec36a22014-08-12 13:21:46 +1000839func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +1100840 s := structPointer_Word32Slice(base, p.field)
841 l := s.Len()
842 if l == 0 {
843 return 0
844 }
845 var bufSize int
846 for i := 0; i < l; i++ {
847 bufSize += p.valSize(uint64(s.Index(i)))
848 }
849
850 n += len(p.tagcode)
851 n += sizeVarint(uint64(bufSize))
852 n += bufSize
853 return
854}
855
Rob Pikeaaa3a622010-03-20 22:32:34 -0700856// Encode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000857func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
858 s := structPointer_Word64Slice(base, p.field)
859 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700860 if l == 0 {
861 return ErrNil
862 }
863 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800864 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000865 p.valEnc(o, s.Index(i))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700866 }
867 return nil
868}
869
David Symonds0bf1ad52013-10-11 09:07:50 +1100870func size_slice_int64(p *Properties, base structPointer) (n int) {
871 s := structPointer_Word64Slice(base, p.field)
872 l := s.Len()
873 if l == 0 {
874 return 0
875 }
876 for i := 0; i < l; i++ {
877 n += len(p.tagcode)
878 n += p.valSize(s.Index(i))
879 }
880 return
881}
882
David Symonds5b7775e2010-12-01 10:09:04 +1100883// Encode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000884func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
885 s := structPointer_Word64Slice(base, p.field)
886 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100887 if l == 0 {
888 return ErrNil
889 }
890 // TODO: Reuse a Buffer.
891 buf := NewBuffer(nil)
892 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000893 p.valEnc(buf, s.Index(i))
David Symonds5b7775e2010-12-01 10:09:04 +1100894 }
895
896 o.buf = append(o.buf, p.tagcode...)
897 o.EncodeVarint(uint64(len(buf.buf)))
898 o.buf = append(o.buf, buf.buf...)
899 return nil
900}
901
David Symonds0bf1ad52013-10-11 09:07:50 +1100902func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
903 s := structPointer_Word64Slice(base, p.field)
904 l := s.Len()
905 if l == 0 {
906 return 0
907 }
908 var bufSize int
909 for i := 0; i < l; i++ {
910 bufSize += p.valSize(s.Index(i))
911 }
912
913 n += len(p.tagcode)
914 n += sizeVarint(uint64(bufSize))
915 n += bufSize
916 return
917}
918
Rob Pikeaaa3a622010-03-20 22:32:34 -0700919// Encode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000920func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
921 ss := *structPointer_BytesSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700922 l := len(ss)
923 if l == 0 {
924 return ErrNil
925 }
926 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800927 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100928 o.EncodeRawBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700929 }
930 return nil
931}
932
David Symonds0bf1ad52013-10-11 09:07:50 +1100933func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
934 ss := *structPointer_BytesSlice(base, p.field)
935 l := len(ss)
936 if l == 0 {
937 return 0
938 }
939 n += l * len(p.tagcode)
940 for i := 0; i < l; i++ {
941 n += sizeRawBytes(ss[i])
942 }
943 return
944}
945
Rob Pikeaaa3a622010-03-20 22:32:34 -0700946// Encode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000947func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
948 ss := *structPointer_StringSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700949 l := len(ss)
950 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800951 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100952 o.EncodeStringBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700953 }
954 return nil
955}
956
David Symonds0bf1ad52013-10-11 09:07:50 +1100957func size_slice_string(p *Properties, base structPointer) (n int) {
958 ss := *structPointer_StringSlice(base, p.field)
959 l := len(ss)
960 n += l * len(p.tagcode)
961 for i := 0; i < l; i++ {
962 n += sizeStringBytes(ss[i])
963 }
964 return
965}
966
Rob Pikeaaa3a622010-03-20 22:32:34 -0700967// Encode a slice of message structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000968func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000969 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000970 s := structPointer_StructPointerSlice(base, p.field)
971 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700972
973 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000974 structp := s.Index(i)
975 if structPointer_IsNil(structp) {
David Symondsf62db482015-02-23 12:37:00 +1100976 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700977 }
978
979 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100980 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000981 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700982 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000983 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700984 return err
985 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800986 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700987 o.EncodeRawBytes(data)
988 continue
989 }
990
David Symonds1d8ba132014-01-13 16:01:15 +1100991 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100992 err := o.enc_len_struct(p.sprop, structp, &state)
David Symonds4646c372013-09-09 13:18:58 +1000993 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700994 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +1100995 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700996 }
997 return err
998 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700999 }
David Symonds4646c372013-09-09 13:18:58 +10001000 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -07001001}
1002
David Symonds0bf1ad52013-10-11 09:07:50 +11001003func size_slice_struct_message(p *Properties, base structPointer) (n int) {
1004 s := structPointer_StructPointerSlice(base, p.field)
1005 l := s.Len()
1006 n += l * len(p.tagcode)
1007 for i := 0; i < l; i++ {
1008 structp := s.Index(i)
1009 if structPointer_IsNil(structp) {
1010 return // return the size up to this point
1011 }
1012
1013 // Can the object marshal itself?
1014 if p.isMarshaler {
1015 m := structPointer_Interface(structp, p.stype).(Marshaler)
1016 data, _ := m.Marshal()
1017 n += len(p.tagcode)
1018 n += sizeRawBytes(data)
1019 continue
1020 }
1021
David Symondsba7896c2014-11-20 15:29:05 +11001022 n0 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +11001023 n1 := sizeVarint(uint64(n0)) // size of encoded length
1024 n += n0 + n1
1025 }
1026 return
1027}
1028
Rob Pikeaaa3a622010-03-20 22:32:34 -07001029// Encode a slice of group structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +10001030func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001031 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +10001032 s := structPointer_StructPointerSlice(base, p.field)
1033 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001034
1035 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +10001036 b := s.Index(i)
1037 if structPointer_IsNil(b) {
David Symondsf62db482015-02-23 12:37:00 +11001038 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001039 }
1040
1041 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
1042
David Symondsba7896c2014-11-20 15:29:05 +11001043 err := o.enc_struct(p.sprop, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001044
David Symonds4646c372013-09-09 13:18:58 +10001045 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001046 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +11001047 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001048 }
1049 return err
1050 }
1051
1052 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
1053 }
David Symonds4646c372013-09-09 13:18:58 +10001054 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -07001055}
1056
David Symonds0bf1ad52013-10-11 09:07:50 +11001057func size_slice_struct_group(p *Properties, base structPointer) (n int) {
1058 s := structPointer_StructPointerSlice(base, p.field)
1059 l := s.Len()
1060
1061 n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
1062 n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
1063 for i := 0; i < l; i++ {
1064 b := s.Index(i)
1065 if structPointer_IsNil(b) {
1066 return // return size up to this point
1067 }
1068
David Symondsba7896c2014-11-20 15:29:05 +11001069 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +11001070 }
1071 return
1072}
1073
Rob Pikeaaa3a622010-03-20 22:32:34 -07001074// Encode an extension map.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001075func (o *Buffer) enc_map(p *Properties, base structPointer) error {
1076 v := *structPointer_ExtMap(base, p.field)
David Symonds1d72f7a2011-08-19 18:28:52 +10001077 if err := encodeExtensionMap(v); err != nil {
1078 return err
1079 }
David Symondsdf583ae2012-12-06 14:06:53 +11001080 // Fast-path for common cases: zero or one extensions.
1081 if len(v) <= 1 {
1082 for _, e := range v {
1083 o.buf = append(o.buf, e.enc...)
1084 }
1085 return nil
1086 }
1087
1088 // Sort keys to provide a deterministic encoding.
1089 keys := make([]int, 0, len(v))
1090 for k := range v {
1091 keys = append(keys, int(k))
1092 }
1093 sort.Ints(keys)
1094
1095 for _, k := range keys {
1096 o.buf = append(o.buf, v[int32(k)].enc...)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001097 }
1098 return nil
1099}
1100
David Symonds0bf1ad52013-10-11 09:07:50 +11001101func size_map(p *Properties, base structPointer) int {
1102 v := *structPointer_ExtMap(base, p.field)
1103 return sizeExtensionMap(v)
1104}
1105
David Symonds3ea3e052014-12-22 16:15:28 +11001106// Encode a map field.
1107func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
1108 var state errorState // XXX: or do we need to plumb this through?
1109
1110 /*
1111 A map defined as
1112 map<key_type, value_type> map_field = N;
1113 is encoded in the same way as
1114 message MapFieldEntry {
1115 key_type key = 1;
1116 value_type value = 2;
1117 }
1118 repeated MapFieldEntry map_field = N;
1119 */
1120
David Symonds0f7a9ca2015-07-20 16:00:00 +10001121 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001122 if v.Len() == 0 {
1123 return nil
1124 }
1125
1126 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1127
1128 enc := func() error {
1129 if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
1130 return err
1131 }
1132 if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil {
1133 return err
1134 }
1135 return nil
1136 }
1137
David Symondsefcaa342015-10-28 16:14:47 +11001138 // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
1139 for _, key := range v.MapKeys() {
David Symonds3ea3e052014-12-22 16:15:28 +11001140 val := v.MapIndex(key)
1141
David Symondscab84a32015-05-19 09:29:00 +10001142 // The only illegal map entry values are nil message pointers.
1143 if val.Kind() == reflect.Ptr && val.IsNil() {
1144 return errors.New("proto: map has nil element")
1145 }
1146
David Symonds3ea3e052014-12-22 16:15:28 +11001147 keycopy.Set(key)
1148 valcopy.Set(val)
1149
1150 o.buf = append(o.buf, p.tagcode...)
1151 if err := o.enc_len_thing(enc, &state); err != nil {
1152 return err
1153 }
1154 }
1155 return nil
1156}
1157
1158func size_new_map(p *Properties, base structPointer) int {
David Symonds0f7a9ca2015-07-20 16:00:00 +10001159 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001160
1161 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1162
1163 n := 0
1164 for _, key := range v.MapKeys() {
1165 val := v.MapIndex(key)
1166 keycopy.Set(key)
1167 valcopy.Set(val)
1168
David Symondsa8323e22015-03-24 10:18:00 +11001169 // Tag codes for key and val are the responsibility of the sub-sizer.
1170 keysize := p.mkeyprop.size(p.mkeyprop, keybase)
1171 valsize := p.mvalprop.size(p.mvalprop, valbase)
1172 entry := keysize + valsize
1173 // Add on tag code and length of map entry itself.
1174 n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
David Symonds3ea3e052014-12-22 16:15:28 +11001175 }
1176 return n
1177}
1178
1179// mapEncodeScratch returns a new reflect.Value matching the map's value type,
1180// and a structPointer suitable for passing to an encoder or sizer.
1181func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
1182 // Prepare addressable doubly-indirect placeholders for the key and value types.
1183 // This is needed because the element-type encoders expect **T, but the map iteration produces T.
1184
1185 keycopy = reflect.New(mapType.Key()).Elem() // addressable K
1186 keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
1187 keyptr.Set(keycopy.Addr()) //
1188 keybase = toStructPointer(keyptr.Addr()) // **K
1189
1190 // Value types are more varied and require special handling.
1191 switch mapType.Elem().Kind() {
1192 case reflect.Slice:
1193 // []byte
1194 var dummy []byte
1195 valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
1196 valbase = toStructPointer(valcopy.Addr())
1197 case reflect.Ptr:
1198 // message; the generated field type is map[K]*Msg (so V is *Msg),
1199 // so we only need one level of indirection.
1200 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1201 valbase = toStructPointer(valcopy.Addr())
1202 default:
1203 // everything else
1204 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1205 valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
1206 valptr.Set(valcopy.Addr()) //
1207 valbase = toStructPointer(valptr.Addr()) // **V
1208 }
1209 return
1210}
1211
Rob Pikeaaa3a622010-03-20 22:32:34 -07001212// Encode a struct.
David Symondsba7896c2014-11-20 15:29:05 +11001213func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001214 var state errorState
David Symondsd15e81b2011-10-03 14:31:12 -07001215 // Encode fields in tag order so that decoders may use optimizations
1216 // that depend on the ordering.
David Symonds380d2d02014-11-24 10:50:43 +11001217 // https://developers.google.com/protocol-buffers/docs/encoding#order
David Symondsd15e81b2011-10-03 14:31:12 -07001218 for _, i := range prop.order {
1219 p := prop.Prop[i]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001220 if p.enc != nil {
1221 err := p.enc(o, p, base)
David Symonds4a2eeb52013-09-25 11:54:08 +10001222 if err != nil {
1223 if err == ErrNil {
1224 if p.Required && state.err == nil {
David Symondse583a5f2013-09-27 10:02:37 +10001225 state.err = &RequiredNotSetError{p.Name}
David Symonds4a2eeb52013-09-25 11:54:08 +10001226 }
David Symondsf62db482015-02-23 12:37:00 +11001227 } else if err == errRepeatedHasNil {
1228 // Give more context to nil values in repeated fields.
1229 return errors.New("repeated field " + p.OrigName + " has nil element")
David Symonds4a2eeb52013-09-25 11:54:08 +10001230 } else if !state.shouldContinue(err, p) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001231 return err
1232 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001233 }
Ross Lightcd85f192016-05-19 15:50:37 -04001234 if len(o.buf) > maxMarshalSize {
1235 return ErrTooLarge
1236 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001237 }
1238 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001239
David Symonds59b73b32015-08-24 13:22:02 +10001240 // Do oneof fields.
1241 if prop.oneofMarshaler != nil {
1242 m := structPointer_Interface(base, prop.stype).(Message)
Bryan Mills8d92cf52016-03-19 05:57:00 +11001243 if err := prop.oneofMarshaler(m, o); err == ErrNil {
1244 return errOneofHasNil
1245 } else if err != nil {
David Symonds59b73b32015-08-24 13:22:02 +10001246 return err
1247 }
1248 }
1249
David Symonds10c93ba2012-08-04 16:38:08 +10001250 // Add unrecognized fields at the end.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001251 if prop.unrecField.IsValid() {
1252 v := *structPointer_Bytes(base, prop.unrecField)
Ross Lightcd85f192016-05-19 15:50:37 -04001253 if len(o.buf)+len(v) > maxMarshalSize {
1254 return ErrTooLarge
1255 }
Russ Coxd4ce3f12012-09-12 10:36:26 +10001256 if len(v) > 0 {
1257 o.buf = append(o.buf, v...)
1258 }
David Symonds10c93ba2012-08-04 16:38:08 +10001259 }
1260
David Symonds4646c372013-09-09 13:18:58 +10001261 return state.err
1262}
1263
David Symondsba7896c2014-11-20 15:29:05 +11001264func size_struct(prop *StructProperties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001265 for _, i := range prop.order {
1266 p := prop.Prop[i]
1267 if p.size != nil {
1268 n += p.size(p, base)
1269 }
1270 }
1271
1272 // Add unrecognized fields at the end.
1273 if prop.unrecField.IsValid() {
1274 v := *structPointer_Bytes(base, prop.unrecField)
1275 n += len(v)
1276 }
1277
David Symonds59b73b32015-08-24 13:22:02 +10001278 // Factor in any oneof fields.
Damien Neil08794902015-12-04 07:39:00 +11001279 if prop.oneofSizer != nil {
1280 m := structPointer_Interface(base, prop.stype).(Message)
1281 n += prop.oneofSizer(m)
David Symonds59b73b32015-08-24 13:22:02 +10001282 }
1283
David Symonds0bf1ad52013-10-11 09:07:50 +11001284 return
1285}
1286
David Symonds1d8ba132014-01-13 16:01:15 +11001287var zeroes [20]byte // longer than any conceivable sizeVarint
1288
1289// Encode a struct, preceded by its encoded length (as a varint).
David Symondsba7896c2014-11-20 15:29:05 +11001290func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
David Symonds3ea3e052014-12-22 16:15:28 +11001291 return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
1292}
1293
1294// Encode something, preceded by its encoded length (as a varint).
1295func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
David Symonds1d8ba132014-01-13 16:01:15 +11001296 iLen := len(o.buf)
1297 o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
1298 iMsg := len(o.buf)
David Symonds3ea3e052014-12-22 16:15:28 +11001299 err := enc()
David Symonds1d8ba132014-01-13 16:01:15 +11001300 if err != nil && !state.shouldContinue(err, nil) {
1301 return err
1302 }
1303 lMsg := len(o.buf) - iMsg
1304 lLen := sizeVarint(uint64(lMsg))
1305 switch x := lLen - (iMsg - iLen); {
1306 case x > 0: // actual length is x bytes larger than the space we reserved
1307 // Move msg x bytes right.
1308 o.buf = append(o.buf, zeroes[:x]...)
1309 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1310 case x < 0: // actual length is x bytes smaller than the space we reserved
1311 // Move msg x bytes left.
1312 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1313 o.buf = o.buf[:len(o.buf)+x] // x is negative
1314 }
1315 // Encode the length in the reserved space.
1316 o.buf = o.buf[:iLen]
1317 o.EncodeVarint(uint64(lMsg))
1318 o.buf = o.buf[:len(o.buf)+lMsg]
1319 return state.err
1320}
1321
David Symonds4646c372013-09-09 13:18:58 +10001322// errorState maintains the first error that occurs and updates that error
1323// with additional context.
1324type errorState struct {
1325 err error
1326}
1327
1328// shouldContinue reports whether encoding should continue upon encountering the
David Symondse583a5f2013-09-27 10:02:37 +10001329// given error. If the error is RequiredNotSetError, shouldContinue returns true
David Symonds4646c372013-09-09 13:18:58 +10001330// and, if this is the first appearance of that error, remembers it for future
1331// reporting.
1332//
1333// If prop is not nil, it may update any error with additional context about the
1334// field with the error.
1335func (s *errorState) shouldContinue(err error, prop *Properties) bool {
1336 // Ignore unset required fields.
David Symondse583a5f2013-09-27 10:02:37 +10001337 reqNotSet, ok := err.(*RequiredNotSetError)
David Symonds4646c372013-09-09 13:18:58 +10001338 if !ok {
1339 return false
1340 }
1341 if s.err == nil {
1342 if prop != nil {
David Symondse583a5f2013-09-27 10:02:37 +10001343 err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
David Symonds4646c372013-09-09 13:18:58 +10001344 }
1345 s.err = err
1346 }
1347 return true
Rob Pikeaaa3a622010-03-20 22:32:34 -07001348}