blob: eb7e0474ef6a864804c235931f57350408e9e2f0 [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")
David Symonds7656e742011-07-22 14:54:17 +100073)
Rob Pikeaaa3a622010-03-20 22:32:34 -070074
75// The fundamental encoders that put bytes on the wire.
76// Those that take integer types all accept uint64 and are
77// therefore of type valueEncoder.
78
David Symonds4fee3b12010-11-11 10:00:13 +110079const maxVarintBytes = 10 // maximum length of a varint
80
Rob Pikeaaa3a622010-03-20 22:32:34 -070081// EncodeVarint returns the varint encoding of x.
82// This is the format for the
83// int32, int64, uint32, uint64, bool, and enum
84// protocol buffer types.
85// Not used by the package itself, but helpful to clients
86// wishing to use the same encoding.
87func EncodeVarint(x uint64) []byte {
David Symonds4fee3b12010-11-11 10:00:13 +110088 var buf [maxVarintBytes]byte
Rob Pikeaaa3a622010-03-20 22:32:34 -070089 var n int
90 for n = 0; x > 127; n++ {
91 buf[n] = 0x80 | uint8(x&0x7F)
92 x >>= 7
93 }
94 buf[n] = uint8(x)
95 n++
96 return buf[0:n]
97}
98
99// EncodeVarint writes a varint-encoded integer to the Buffer.
100// This is the format for the
101// int32, int64, uint32, uint64, bool, and enum
102// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700103func (p *Buffer) EncodeVarint(x uint64) error {
David Symonds4fee3b12010-11-11 10:00:13 +1100104 for x >= 1<<7 {
David Symondsd9da6ba2011-08-30 14:41:30 +1000105 p.buf = append(p.buf, uint8(x&0x7f|0x80))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700106 x >>= 7
107 }
David Symondsd9da6ba2011-08-30 14:41:30 +1000108 p.buf = append(p.buf, uint8(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700109 return nil
110}
111
Damien Neil08794902015-12-04 07:39:00 +1100112// SizeVarint returns the varint encoding size of an integer.
113func SizeVarint(x uint64) int {
114 return sizeVarint(x)
115}
116
David Symonds0bf1ad52013-10-11 09:07:50 +1100117func sizeVarint(x uint64) (n int) {
118 for {
119 n++
120 x >>= 7
121 if x == 0 {
122 break
123 }
124 }
125 return n
126}
127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700128// EncodeFixed64 writes a 64-bit integer to the Buffer.
129// This is the format for the
130// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700131func (p *Buffer) EncodeFixed64(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000132 p.buf = append(p.buf,
133 uint8(x),
134 uint8(x>>8),
135 uint8(x>>16),
136 uint8(x>>24),
137 uint8(x>>32),
138 uint8(x>>40),
139 uint8(x>>48),
140 uint8(x>>56))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700141 return nil
142}
143
David Symonds0bf1ad52013-10-11 09:07:50 +1100144func sizeFixed64(x uint64) int {
145 return 8
146}
147
Rob Pikeaaa3a622010-03-20 22:32:34 -0700148// EncodeFixed32 writes a 32-bit integer to the Buffer.
149// This is the format for the
150// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700151func (p *Buffer) EncodeFixed32(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000152 p.buf = append(p.buf,
153 uint8(x),
154 uint8(x>>8),
155 uint8(x>>16),
156 uint8(x>>24))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700157 return nil
158}
159
David Symonds0bf1ad52013-10-11 09:07:50 +1100160func sizeFixed32(x uint64) int {
161 return 4
162}
163
Rob Pikeaaa3a622010-03-20 22:32:34 -0700164// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
165// to the Buffer.
166// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700167func (p *Buffer) EncodeZigzag64(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700168 // use signed number to get arithmetic right shift.
169 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
170}
171
David Symonds0bf1ad52013-10-11 09:07:50 +1100172func sizeZigzag64(x uint64) int {
173 return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
174}
175
Rob Pikeaaa3a622010-03-20 22:32:34 -0700176// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
177// to the Buffer.
178// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700179func (p *Buffer) EncodeZigzag32(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700180 // use signed number to get arithmetic right shift.
181 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
182}
183
David Symonds0bf1ad52013-10-11 09:07:50 +1100184func sizeZigzag32(x uint64) int {
185 return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
186}
187
Rob Pikeaaa3a622010-03-20 22:32:34 -0700188// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
189// This is the format used for the bytes protocol buffer
190// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700191func (p *Buffer) EncodeRawBytes(b []byte) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700192 p.EncodeVarint(uint64(len(b)))
Rob Pike99fa2b62010-12-02 10:39:42 -0800193 p.buf = append(p.buf, b...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700194 return nil
195}
196
David Symonds0bf1ad52013-10-11 09:07:50 +1100197func sizeRawBytes(b []byte) int {
198 return sizeVarint(uint64(len(b))) +
199 len(b)
200}
201
Rob Pikeaaa3a622010-03-20 22:32:34 -0700202// EncodeStringBytes writes an encoded string to the Buffer.
203// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700204func (p *Buffer) EncodeStringBytes(s string) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700205 p.EncodeVarint(uint64(len(s)))
206 p.buf = append(p.buf, s...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700207 return nil
208}
209
David Symonds0bf1ad52013-10-11 09:07:50 +1100210func sizeStringBytes(s string) int {
211 return sizeVarint(uint64(len(s))) +
212 len(s)
213}
214
Rob Pikeaaa3a622010-03-20 22:32:34 -0700215// Marshaler is the interface representing objects that can marshal themselves.
216type Marshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700217 Marshal() ([]byte, error)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700218}
219
David Symonds9f60f432012-06-14 09:45:25 +1000220// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700221// and encodes it into the wire format, returning the data.
David Symonds9f60f432012-06-14 09:45:25 +1000222func Marshal(pb Message) ([]byte, error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700223 // Can the object marshal itself?
224 if m, ok := pb.(Marshaler); ok {
225 return m.Marshal()
226 }
227 p := NewBuffer(nil)
228 err := p.Marshal(pb)
David Symonds4646c372013-09-09 13:18:58 +1000229 var state errorState
230 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700231 return nil, err
232 }
David Symonds8b253302014-01-14 16:24:19 +1100233 if p.buf == nil && err == nil {
234 // Return a non-nil slice on success.
235 return []byte{}, nil
236 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700237 return p.buf, err
238}
239
David Symonds59b73b32015-08-24 13:22:02 +1000240// EncodeMessage writes the protocol buffer to the Buffer,
241// prefixed by a varint-encoded length.
242func (p *Buffer) EncodeMessage(pb Message) error {
243 t, base, err := getbase(pb)
244 if structPointer_IsNil(base) {
245 return ErrNil
246 }
247 if err == nil {
248 var state errorState
249 err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
250 }
251 return err
252}
253
David Symonds9f60f432012-06-14 09:45:25 +1000254// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700255// and encodes it into the wire format, writing the result to the
256// Buffer.
David Symonds9f60f432012-06-14 09:45:25 +1000257func (p *Buffer) Marshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700258 // Can the object marshal itself?
259 if m, ok := pb.(Marshaler); ok {
260 data, err := m.Marshal()
261 if err != nil {
262 return err
263 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800264 p.buf = append(p.buf, data...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700265 return nil
266 }
267
Russ Coxd4ce3f12012-09-12 10:36:26 +1000268 t, base, err := getbase(pb)
269 if structPointer_IsNil(base) {
David Symondsd4661c52012-08-30 15:17:53 +1000270 return ErrNil
271 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700272 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100273 err = p.enc_struct(GetProperties(t.Elem()), base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700274 }
275
David Symonds9f60f432012-06-14 09:45:25 +1000276 if collectStats {
277 stats.Encode++
278 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700279
280 return err
281}
282
David Symonds0bf1ad52013-10-11 09:07:50 +1100283// Size returns the encoded size of a protocol buffer.
284func Size(pb Message) (n int) {
285 // Can the object marshal itself? If so, Size is slow.
286 // TODO: add Size to Marshaler, or add a Sizer interface.
287 if m, ok := pb.(Marshaler); ok {
288 b, _ := m.Marshal()
289 return len(b)
290 }
291
292 t, base, err := getbase(pb)
293 if structPointer_IsNil(base) {
294 return 0
295 }
296 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100297 n = size_struct(GetProperties(t.Elem()), base)
David Symonds0bf1ad52013-10-11 09:07:50 +1100298 }
299
300 if collectStats {
301 stats.Size++
302 }
303
304 return
305}
306
Rob Pikeaaa3a622010-03-20 22:32:34 -0700307// Individual type encoders.
308
309// Encode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000310func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
311 v := *structPointer_Bool(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700312 if v == nil {
313 return ErrNil
314 }
Rob Pike0f42a272011-10-20 16:03:11 -0700315 x := 0
316 if *v {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700317 x = 1
318 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800319 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700320 p.valEnc(o, uint64(x))
321 return nil
322}
323
David Symondsabd3b412014-11-28 11:43:44 +1100324func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
325 v := *structPointer_BoolVal(base, p.field)
326 if !v {
327 return ErrNil
328 }
329 o.buf = append(o.buf, p.tagcode...)
330 p.valEnc(o, 1)
331 return nil
332}
333
David Symonds0bf1ad52013-10-11 09:07:50 +1100334func size_bool(p *Properties, base structPointer) int {
335 v := *structPointer_Bool(base, p.field)
336 if v == nil {
337 return 0
338 }
339 return len(p.tagcode) + 1 // each bool takes exactly one byte
340}
341
David Symondsabd3b412014-11-28 11:43:44 +1100342func size_proto3_bool(p *Properties, base structPointer) int {
343 v := *structPointer_BoolVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000344 if !v && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100345 return 0
346 }
347 return len(p.tagcode) + 1 // each bool takes exactly one byte
348}
349
Rob Pikeaaa3a622010-03-20 22:32:34 -0700350// Encode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000351func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
352 v := structPointer_Word32(base, p.field)
353 if word32_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700354 return ErrNil
355 }
David Symondsf054e842014-07-22 14:06:27 +1000356 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
Rob Pike99fa2b62010-12-02 10:39:42 -0800357 o.buf = append(o.buf, p.tagcode...)
David Symondsc31645c2013-06-22 17:57:36 +1000358 p.valEnc(o, uint64(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700359 return nil
360}
361
David Symondsabd3b412014-11-28 11:43:44 +1100362func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
363 v := structPointer_Word32Val(base, p.field)
364 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
365 if x == 0 {
366 return ErrNil
367 }
368 o.buf = append(o.buf, p.tagcode...)
369 p.valEnc(o, uint64(x))
370 return nil
371}
372
David Symonds0bf1ad52013-10-11 09:07:50 +1100373func size_int32(p *Properties, base structPointer) (n int) {
374 v := structPointer_Word32(base, p.field)
375 if word32_IsNil(v) {
376 return 0
377 }
David Symondsf054e842014-07-22 14:06:27 +1000378 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
379 n += len(p.tagcode)
380 n += p.valSize(uint64(x))
381 return
382}
383
David Symondsabd3b412014-11-28 11:43:44 +1100384func size_proto3_int32(p *Properties, base structPointer) (n int) {
385 v := structPointer_Word32Val(base, p.field)
386 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
David Symonds535a1042015-09-11 08:27:00 +1000387 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100388 return 0
389 }
390 n += len(p.tagcode)
391 n += p.valSize(uint64(x))
392 return
393}
394
David Symondsf054e842014-07-22 14:06:27 +1000395// Encode a uint32.
396// Exactly the same as int32, except for no sign extension.
397func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
398 v := structPointer_Word32(base, p.field)
399 if word32_IsNil(v) {
400 return ErrNil
401 }
402 x := word32_Get(v)
403 o.buf = append(o.buf, p.tagcode...)
404 p.valEnc(o, uint64(x))
405 return nil
406}
407
David Symondsabd3b412014-11-28 11:43:44 +1100408func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
409 v := structPointer_Word32Val(base, p.field)
410 x := word32Val_Get(v)
411 if x == 0 {
412 return ErrNil
413 }
414 o.buf = append(o.buf, p.tagcode...)
415 p.valEnc(o, uint64(x))
416 return nil
417}
418
David Symondsf054e842014-07-22 14:06:27 +1000419func size_uint32(p *Properties, base structPointer) (n int) {
420 v := structPointer_Word32(base, p.field)
421 if word32_IsNil(v) {
422 return 0
423 }
David Symonds0bf1ad52013-10-11 09:07:50 +1100424 x := word32_Get(v)
425 n += len(p.tagcode)
426 n += p.valSize(uint64(x))
427 return
428}
429
David Symondsabd3b412014-11-28 11:43:44 +1100430func size_proto3_uint32(p *Properties, base structPointer) (n int) {
431 v := structPointer_Word32Val(base, p.field)
432 x := word32Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000433 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100434 return 0
435 }
436 n += len(p.tagcode)
437 n += p.valSize(uint64(x))
438 return
439}
440
Rob Pikeaaa3a622010-03-20 22:32:34 -0700441// Encode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000442func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
443 v := structPointer_Word64(base, p.field)
444 if word64_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700445 return ErrNil
446 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000447 x := word64_Get(v)
Rob Pike99fa2b62010-12-02 10:39:42 -0800448 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000449 p.valEnc(o, x)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700450 return nil
451}
452
David Symondsabd3b412014-11-28 11:43:44 +1100453func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
454 v := structPointer_Word64Val(base, p.field)
455 x := word64Val_Get(v)
456 if x == 0 {
457 return ErrNil
458 }
459 o.buf = append(o.buf, p.tagcode...)
460 p.valEnc(o, x)
461 return nil
462}
463
David Symonds0bf1ad52013-10-11 09:07:50 +1100464func size_int64(p *Properties, base structPointer) (n int) {
465 v := structPointer_Word64(base, p.field)
466 if word64_IsNil(v) {
467 return 0
468 }
469 x := word64_Get(v)
470 n += len(p.tagcode)
471 n += p.valSize(x)
472 return
473}
474
David Symondsabd3b412014-11-28 11:43:44 +1100475func size_proto3_int64(p *Properties, base structPointer) (n int) {
476 v := structPointer_Word64Val(base, p.field)
477 x := word64Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000478 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100479 return 0
480 }
481 n += len(p.tagcode)
482 n += p.valSize(x)
483 return
484}
485
Rob Pikeaaa3a622010-03-20 22:32:34 -0700486// Encode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000487func (o *Buffer) enc_string(p *Properties, base structPointer) error {
488 v := *structPointer_String(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700489 if v == nil {
490 return ErrNil
491 }
492 x := *v
Rob Pike99fa2b62010-12-02 10:39:42 -0800493 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700494 o.EncodeStringBytes(x)
495 return nil
496}
497
David Symondsabd3b412014-11-28 11:43:44 +1100498func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
499 v := *structPointer_StringVal(base, p.field)
500 if v == "" {
501 return ErrNil
502 }
503 o.buf = append(o.buf, p.tagcode...)
504 o.EncodeStringBytes(v)
505 return nil
506}
507
David Symonds0bf1ad52013-10-11 09:07:50 +1100508func size_string(p *Properties, base structPointer) (n int) {
509 v := *structPointer_String(base, p.field)
510 if v == nil {
511 return 0
512 }
513 x := *v
514 n += len(p.tagcode)
515 n += sizeStringBytes(x)
516 return
517}
518
David Symondsabd3b412014-11-28 11:43:44 +1100519func size_proto3_string(p *Properties, base structPointer) (n int) {
520 v := *structPointer_StringVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000521 if v == "" && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100522 return 0
523 }
524 n += len(p.tagcode)
525 n += sizeStringBytes(v)
526 return
527}
528
Rob Pike97e934d2011-04-11 12:52:49 -0700529// All protocol buffer fields are nillable, but be careful.
530func isNil(v reflect.Value) bool {
531 switch v.Kind() {
David Symonds007ed9d2012-07-24 10:59:36 +1000532 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
Rob Pike97e934d2011-04-11 12:52:49 -0700533 return v.IsNil()
534 }
535 return false
536}
537
Rob Pikeaaa3a622010-03-20 22:32:34 -0700538// Encode a message struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000539func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000540 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000541 structp := structPointer_GetStructPointer(base, p.field)
542 if structPointer_IsNil(structp) {
David Symondsa80b2822012-03-14 14:31:25 +1100543 return ErrNil
544 }
545
Rob Pikeaaa3a622010-03-20 22:32:34 -0700546 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100547 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000548 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700549 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000550 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700551 return err
552 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800553 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700554 o.EncodeRawBytes(data)
David Symonds68c687d2015-07-27 19:06:00 +1000555 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700556 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700557
Rob Pike99fa2b62010-12-02 10:39:42 -0800558 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100559 return o.enc_len_struct(p.sprop, structp, &state)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700560}
561
David Symonds0bf1ad52013-10-11 09:07:50 +1100562func size_struct_message(p *Properties, base structPointer) int {
563 structp := structPointer_GetStructPointer(base, p.field)
564 if structPointer_IsNil(structp) {
565 return 0
566 }
567
568 // Can the object marshal itself?
569 if p.isMarshaler {
570 m := structPointer_Interface(structp, p.stype).(Marshaler)
571 data, _ := m.Marshal()
572 n0 := len(p.tagcode)
573 n1 := sizeRawBytes(data)
574 return n0 + n1
575 }
576
577 n0 := len(p.tagcode)
David Symondsba7896c2014-11-20 15:29:05 +1100578 n1 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +1100579 n2 := sizeVarint(uint64(n1)) // size of encoded length
580 return n0 + n1 + n2
581}
582
Rob Pikeaaa3a622010-03-20 22:32:34 -0700583// Encode a group struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000584func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000585 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000586 b := structPointer_GetStructPointer(base, p.field)
587 if structPointer_IsNil(b) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700588 return ErrNil
589 }
590
591 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100592 err := o.enc_struct(p.sprop, b)
David Symonds4646c372013-09-09 13:18:58 +1000593 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700594 return err
595 }
596 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
David Symonds4646c372013-09-09 13:18:58 +1000597 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700598}
599
David Symonds0bf1ad52013-10-11 09:07:50 +1100600func size_struct_group(p *Properties, base structPointer) (n int) {
601 b := structPointer_GetStructPointer(base, p.field)
602 if structPointer_IsNil(b) {
603 return 0
604 }
605
606 n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100607 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +1100608 n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
609 return
610}
611
Rob Pikeaaa3a622010-03-20 22:32:34 -0700612// Encode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000613func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
614 s := *structPointer_BoolSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700615 l := len(s)
616 if l == 0 {
617 return ErrNil
618 }
619 for _, x := range s {
Rob Pike99fa2b62010-12-02 10:39:42 -0800620 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000621 v := uint64(0)
622 if x {
623 v = 1
Rob Pikeaaa3a622010-03-20 22:32:34 -0700624 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000625 p.valEnc(o, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700626 }
627 return nil
628}
629
David Symonds0bf1ad52013-10-11 09:07:50 +1100630func size_slice_bool(p *Properties, base structPointer) int {
631 s := *structPointer_BoolSlice(base, p.field)
632 l := len(s)
633 if l == 0 {
634 return 0
635 }
636 return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
637}
638
David Symonds5b7775e2010-12-01 10:09:04 +1100639// Encode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000640func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
641 s := *structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100642 l := len(s)
643 if l == 0 {
644 return ErrNil
645 }
646 o.buf = append(o.buf, p.tagcode...)
647 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
648 for _, x := range s {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000649 v := uint64(0)
650 if x {
651 v = 1
David Symonds5b7775e2010-12-01 10:09:04 +1100652 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000653 p.valEnc(o, v)
David Symonds5b7775e2010-12-01 10:09:04 +1100654 }
655 return nil
656}
657
David Symonds0bf1ad52013-10-11 09:07:50 +1100658func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
659 s := *structPointer_BoolSlice(base, p.field)
660 l := len(s)
661 if l == 0 {
662 return 0
663 }
664 n += len(p.tagcode)
665 n += sizeVarint(uint64(l))
666 n += l // each bool takes exactly one byte
667 return
668}
669
Rob Pikeaaa3a622010-03-20 22:32:34 -0700670// Encode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000671func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
672 s := *structPointer_Bytes(base, p.field)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800673 if s == nil {
674 return ErrNil
675 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800676 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700677 o.EncodeRawBytes(s)
678 return nil
679}
680
David Symondsabd3b412014-11-28 11:43:44 +1100681func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
682 s := *structPointer_Bytes(base, p.field)
683 if len(s) == 0 {
684 return ErrNil
685 }
686 o.buf = append(o.buf, p.tagcode...)
687 o.EncodeRawBytes(s)
688 return nil
689}
690
David Symonds0bf1ad52013-10-11 09:07:50 +1100691func size_slice_byte(p *Properties, base structPointer) (n int) {
692 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000693 if s == nil && !p.oneof {
David Symonds0bf1ad52013-10-11 09:07:50 +1100694 return 0
695 }
696 n += len(p.tagcode)
697 n += sizeRawBytes(s)
698 return
699}
700
David Symondsabd3b412014-11-28 11:43:44 +1100701func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
702 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000703 if len(s) == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100704 return 0
705 }
706 n += len(p.tagcode)
707 n += sizeRawBytes(s)
708 return
709}
710
Rob Pikeaaa3a622010-03-20 22:32:34 -0700711// Encode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000712func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
713 s := structPointer_Word32Slice(base, p.field)
714 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700715 if l == 0 {
716 return ErrNil
717 }
718 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800719 o.buf = append(o.buf, p.tagcode...)
David Symonds0ec36a22014-08-12 13:21:46 +1000720 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
Rob Pikeaaa3a622010-03-20 22:32:34 -0700721 p.valEnc(o, uint64(x))
722 }
723 return nil
724}
725
David Symonds0bf1ad52013-10-11 09:07:50 +1100726func size_slice_int32(p *Properties, base structPointer) (n int) {
727 s := structPointer_Word32Slice(base, p.field)
728 l := s.Len()
729 if l == 0 {
730 return 0
731 }
732 for i := 0; i < l; i++ {
733 n += len(p.tagcode)
David Symonds0ec36a22014-08-12 13:21:46 +1000734 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
David Symonds0bf1ad52013-10-11 09:07:50 +1100735 n += p.valSize(uint64(x))
736 }
737 return
738}
739
David Symonds5b7775e2010-12-01 10:09:04 +1100740// Encode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000741func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
742 s := structPointer_Word32Slice(base, p.field)
743 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100744 if l == 0 {
745 return ErrNil
746 }
747 // TODO: Reuse a Buffer.
748 buf := NewBuffer(nil)
749 for i := 0; i < l; i++ {
David Symonds0ec36a22014-08-12 13:21:46 +1000750 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
751 p.valEnc(buf, uint64(x))
752 }
753
754 o.buf = append(o.buf, p.tagcode...)
755 o.EncodeVarint(uint64(len(buf.buf)))
756 o.buf = append(o.buf, buf.buf...)
757 return nil
758}
759
760func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
761 s := structPointer_Word32Slice(base, p.field)
762 l := s.Len()
763 if l == 0 {
764 return 0
765 }
766 var bufSize int
767 for i := 0; i < l; i++ {
768 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
769 bufSize += p.valSize(uint64(x))
770 }
771
772 n += len(p.tagcode)
773 n += sizeVarint(uint64(bufSize))
774 n += bufSize
775 return
776}
777
778// Encode a slice of uint32s ([]uint32).
779// Exactly the same as int32, except for no sign extension.
780func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
781 s := structPointer_Word32Slice(base, p.field)
782 l := s.Len()
783 if l == 0 {
784 return ErrNil
785 }
786 for i := 0; i < l; i++ {
787 o.buf = append(o.buf, p.tagcode...)
788 x := s.Index(i)
789 p.valEnc(o, uint64(x))
790 }
791 return nil
792}
793
794func size_slice_uint32(p *Properties, base structPointer) (n int) {
795 s := structPointer_Word32Slice(base, p.field)
796 l := s.Len()
797 if l == 0 {
798 return 0
799 }
800 for i := 0; i < l; i++ {
801 n += len(p.tagcode)
802 x := s.Index(i)
803 n += p.valSize(uint64(x))
804 }
805 return
806}
807
808// Encode a slice of uint32s ([]uint32) in packed format.
809// Exactly the same as int32, except for no sign extension.
810func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
811 s := structPointer_Word32Slice(base, p.field)
812 l := s.Len()
813 if l == 0 {
814 return ErrNil
815 }
816 // TODO: Reuse a Buffer.
817 buf := NewBuffer(nil)
818 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000819 p.valEnc(buf, uint64(s.Index(i)))
David Symonds5b7775e2010-12-01 10:09:04 +1100820 }
821
822 o.buf = append(o.buf, p.tagcode...)
823 o.EncodeVarint(uint64(len(buf.buf)))
824 o.buf = append(o.buf, buf.buf...)
825 return nil
826}
827
David Symonds0ec36a22014-08-12 13:21:46 +1000828func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +1100829 s := structPointer_Word32Slice(base, p.field)
830 l := s.Len()
831 if l == 0 {
832 return 0
833 }
834 var bufSize int
835 for i := 0; i < l; i++ {
836 bufSize += p.valSize(uint64(s.Index(i)))
837 }
838
839 n += len(p.tagcode)
840 n += sizeVarint(uint64(bufSize))
841 n += bufSize
842 return
843}
844
Rob Pikeaaa3a622010-03-20 22:32:34 -0700845// Encode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000846func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
847 s := structPointer_Word64Slice(base, p.field)
848 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700849 if l == 0 {
850 return ErrNil
851 }
852 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800853 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000854 p.valEnc(o, s.Index(i))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700855 }
856 return nil
857}
858
David Symonds0bf1ad52013-10-11 09:07:50 +1100859func size_slice_int64(p *Properties, base structPointer) (n int) {
860 s := structPointer_Word64Slice(base, p.field)
861 l := s.Len()
862 if l == 0 {
863 return 0
864 }
865 for i := 0; i < l; i++ {
866 n += len(p.tagcode)
867 n += p.valSize(s.Index(i))
868 }
869 return
870}
871
David Symonds5b7775e2010-12-01 10:09:04 +1100872// Encode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000873func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
874 s := structPointer_Word64Slice(base, p.field)
875 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100876 if l == 0 {
877 return ErrNil
878 }
879 // TODO: Reuse a Buffer.
880 buf := NewBuffer(nil)
881 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000882 p.valEnc(buf, s.Index(i))
David Symonds5b7775e2010-12-01 10:09:04 +1100883 }
884
885 o.buf = append(o.buf, p.tagcode...)
886 o.EncodeVarint(uint64(len(buf.buf)))
887 o.buf = append(o.buf, buf.buf...)
888 return nil
889}
890
David Symonds0bf1ad52013-10-11 09:07:50 +1100891func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
892 s := structPointer_Word64Slice(base, p.field)
893 l := s.Len()
894 if l == 0 {
895 return 0
896 }
897 var bufSize int
898 for i := 0; i < l; i++ {
899 bufSize += p.valSize(s.Index(i))
900 }
901
902 n += len(p.tagcode)
903 n += sizeVarint(uint64(bufSize))
904 n += bufSize
905 return
906}
907
Rob Pikeaaa3a622010-03-20 22:32:34 -0700908// Encode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000909func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
910 ss := *structPointer_BytesSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700911 l := len(ss)
912 if l == 0 {
913 return ErrNil
914 }
915 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800916 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100917 o.EncodeRawBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700918 }
919 return nil
920}
921
David Symonds0bf1ad52013-10-11 09:07:50 +1100922func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
923 ss := *structPointer_BytesSlice(base, p.field)
924 l := len(ss)
925 if l == 0 {
926 return 0
927 }
928 n += l * len(p.tagcode)
929 for i := 0; i < l; i++ {
930 n += sizeRawBytes(ss[i])
931 }
932 return
933}
934
Rob Pikeaaa3a622010-03-20 22:32:34 -0700935// Encode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000936func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
937 ss := *structPointer_StringSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700938 l := len(ss)
939 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800940 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100941 o.EncodeStringBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700942 }
943 return nil
944}
945
David Symonds0bf1ad52013-10-11 09:07:50 +1100946func size_slice_string(p *Properties, base structPointer) (n int) {
947 ss := *structPointer_StringSlice(base, p.field)
948 l := len(ss)
949 n += l * len(p.tagcode)
950 for i := 0; i < l; i++ {
951 n += sizeStringBytes(ss[i])
952 }
953 return
954}
955
Rob Pikeaaa3a622010-03-20 22:32:34 -0700956// Encode a slice of message structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000957func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000958 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000959 s := structPointer_StructPointerSlice(base, p.field)
960 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700961
962 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000963 structp := s.Index(i)
964 if structPointer_IsNil(structp) {
David Symondsf62db482015-02-23 12:37:00 +1100965 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700966 }
967
968 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100969 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000970 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700971 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000972 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700973 return err
974 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800975 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700976 o.EncodeRawBytes(data)
977 continue
978 }
979
David Symonds1d8ba132014-01-13 16:01:15 +1100980 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100981 err := o.enc_len_struct(p.sprop, structp, &state)
David Symonds4646c372013-09-09 13:18:58 +1000982 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700983 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +1100984 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700985 }
986 return err
987 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700988 }
David Symonds4646c372013-09-09 13:18:58 +1000989 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700990}
991
David Symonds0bf1ad52013-10-11 09:07:50 +1100992func size_slice_struct_message(p *Properties, base structPointer) (n int) {
993 s := structPointer_StructPointerSlice(base, p.field)
994 l := s.Len()
995 n += l * len(p.tagcode)
996 for i := 0; i < l; i++ {
997 structp := s.Index(i)
998 if structPointer_IsNil(structp) {
999 return // return the size up to this point
1000 }
1001
1002 // Can the object marshal itself?
1003 if p.isMarshaler {
1004 m := structPointer_Interface(structp, p.stype).(Marshaler)
1005 data, _ := m.Marshal()
1006 n += len(p.tagcode)
1007 n += sizeRawBytes(data)
1008 continue
1009 }
1010
David Symondsba7896c2014-11-20 15:29:05 +11001011 n0 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +11001012 n1 := sizeVarint(uint64(n0)) // size of encoded length
1013 n += n0 + n1
1014 }
1015 return
1016}
1017
Rob Pikeaaa3a622010-03-20 22:32:34 -07001018// Encode a slice of group structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +10001019func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001020 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +10001021 s := structPointer_StructPointerSlice(base, p.field)
1022 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001023
1024 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +10001025 b := s.Index(i)
1026 if structPointer_IsNil(b) {
David Symondsf62db482015-02-23 12:37:00 +11001027 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001028 }
1029
1030 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
1031
David Symondsba7896c2014-11-20 15:29:05 +11001032 err := o.enc_struct(p.sprop, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001033
David Symonds4646c372013-09-09 13:18:58 +10001034 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001035 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +11001036 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001037 }
1038 return err
1039 }
1040
1041 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
1042 }
David Symonds4646c372013-09-09 13:18:58 +10001043 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -07001044}
1045
David Symonds0bf1ad52013-10-11 09:07:50 +11001046func size_slice_struct_group(p *Properties, base structPointer) (n int) {
1047 s := structPointer_StructPointerSlice(base, p.field)
1048 l := s.Len()
1049
1050 n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
1051 n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
1052 for i := 0; i < l; i++ {
1053 b := s.Index(i)
1054 if structPointer_IsNil(b) {
1055 return // return size up to this point
1056 }
1057
David Symondsba7896c2014-11-20 15:29:05 +11001058 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +11001059 }
1060 return
1061}
1062
Rob Pikeaaa3a622010-03-20 22:32:34 -07001063// Encode an extension map.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001064func (o *Buffer) enc_map(p *Properties, base structPointer) error {
1065 v := *structPointer_ExtMap(base, p.field)
David Symonds1d72f7a2011-08-19 18:28:52 +10001066 if err := encodeExtensionMap(v); err != nil {
1067 return err
1068 }
David Symondsdf583ae2012-12-06 14:06:53 +11001069 // Fast-path for common cases: zero or one extensions.
1070 if len(v) <= 1 {
1071 for _, e := range v {
1072 o.buf = append(o.buf, e.enc...)
1073 }
1074 return nil
1075 }
1076
1077 // Sort keys to provide a deterministic encoding.
1078 keys := make([]int, 0, len(v))
1079 for k := range v {
1080 keys = append(keys, int(k))
1081 }
1082 sort.Ints(keys)
1083
1084 for _, k := range keys {
1085 o.buf = append(o.buf, v[int32(k)].enc...)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001086 }
1087 return nil
1088}
1089
David Symonds0bf1ad52013-10-11 09:07:50 +11001090func size_map(p *Properties, base structPointer) int {
1091 v := *structPointer_ExtMap(base, p.field)
1092 return sizeExtensionMap(v)
1093}
1094
David Symonds3ea3e052014-12-22 16:15:28 +11001095// Encode a map field.
1096func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
1097 var state errorState // XXX: or do we need to plumb this through?
1098
1099 /*
1100 A map defined as
1101 map<key_type, value_type> map_field = N;
1102 is encoded in the same way as
1103 message MapFieldEntry {
1104 key_type key = 1;
1105 value_type value = 2;
1106 }
1107 repeated MapFieldEntry map_field = N;
1108 */
1109
David Symonds0f7a9ca2015-07-20 16:00:00 +10001110 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001111 if v.Len() == 0 {
1112 return nil
1113 }
1114
1115 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1116
1117 enc := func() error {
1118 if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
1119 return err
1120 }
1121 if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil {
1122 return err
1123 }
1124 return nil
1125 }
1126
David Symondsefcaa342015-10-28 16:14:47 +11001127 // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
1128 for _, key := range v.MapKeys() {
David Symonds3ea3e052014-12-22 16:15:28 +11001129 val := v.MapIndex(key)
1130
David Symondscab84a32015-05-19 09:29:00 +10001131 // The only illegal map entry values are nil message pointers.
1132 if val.Kind() == reflect.Ptr && val.IsNil() {
1133 return errors.New("proto: map has nil element")
1134 }
1135
David Symonds3ea3e052014-12-22 16:15:28 +11001136 keycopy.Set(key)
1137 valcopy.Set(val)
1138
1139 o.buf = append(o.buf, p.tagcode...)
1140 if err := o.enc_len_thing(enc, &state); err != nil {
1141 return err
1142 }
1143 }
1144 return nil
1145}
1146
1147func size_new_map(p *Properties, base structPointer) int {
David Symonds0f7a9ca2015-07-20 16:00:00 +10001148 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001149
1150 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1151
1152 n := 0
1153 for _, key := range v.MapKeys() {
1154 val := v.MapIndex(key)
1155 keycopy.Set(key)
1156 valcopy.Set(val)
1157
David Symondsa8323e22015-03-24 10:18:00 +11001158 // Tag codes for key and val are the responsibility of the sub-sizer.
1159 keysize := p.mkeyprop.size(p.mkeyprop, keybase)
1160 valsize := p.mvalprop.size(p.mvalprop, valbase)
1161 entry := keysize + valsize
1162 // Add on tag code and length of map entry itself.
1163 n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
David Symonds3ea3e052014-12-22 16:15:28 +11001164 }
1165 return n
1166}
1167
1168// mapEncodeScratch returns a new reflect.Value matching the map's value type,
1169// and a structPointer suitable for passing to an encoder or sizer.
1170func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
1171 // Prepare addressable doubly-indirect placeholders for the key and value types.
1172 // This is needed because the element-type encoders expect **T, but the map iteration produces T.
1173
1174 keycopy = reflect.New(mapType.Key()).Elem() // addressable K
1175 keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
1176 keyptr.Set(keycopy.Addr()) //
1177 keybase = toStructPointer(keyptr.Addr()) // **K
1178
1179 // Value types are more varied and require special handling.
1180 switch mapType.Elem().Kind() {
1181 case reflect.Slice:
1182 // []byte
1183 var dummy []byte
1184 valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
1185 valbase = toStructPointer(valcopy.Addr())
1186 case reflect.Ptr:
1187 // message; the generated field type is map[K]*Msg (so V is *Msg),
1188 // so we only need one level of indirection.
1189 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1190 valbase = toStructPointer(valcopy.Addr())
1191 default:
1192 // everything else
1193 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1194 valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
1195 valptr.Set(valcopy.Addr()) //
1196 valbase = toStructPointer(valptr.Addr()) // **V
1197 }
1198 return
1199}
1200
Rob Pikeaaa3a622010-03-20 22:32:34 -07001201// Encode a struct.
David Symondsba7896c2014-11-20 15:29:05 +11001202func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001203 var state errorState
David Symondsd15e81b2011-10-03 14:31:12 -07001204 // Encode fields in tag order so that decoders may use optimizations
1205 // that depend on the ordering.
David Symonds380d2d02014-11-24 10:50:43 +11001206 // https://developers.google.com/protocol-buffers/docs/encoding#order
David Symondsd15e81b2011-10-03 14:31:12 -07001207 for _, i := range prop.order {
1208 p := prop.Prop[i]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001209 if p.enc != nil {
1210 err := p.enc(o, p, base)
David Symonds4a2eeb52013-09-25 11:54:08 +10001211 if err != nil {
1212 if err == ErrNil {
1213 if p.Required && state.err == nil {
David Symondse583a5f2013-09-27 10:02:37 +10001214 state.err = &RequiredNotSetError{p.Name}
David Symonds4a2eeb52013-09-25 11:54:08 +10001215 }
David Symondsf62db482015-02-23 12:37:00 +11001216 } else if err == errRepeatedHasNil {
1217 // Give more context to nil values in repeated fields.
1218 return errors.New("repeated field " + p.OrigName + " has nil element")
David Symonds4a2eeb52013-09-25 11:54:08 +10001219 } else if !state.shouldContinue(err, p) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001220 return err
1221 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001222 }
1223 }
1224 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001225
David Symonds59b73b32015-08-24 13:22:02 +10001226 // Do oneof fields.
1227 if prop.oneofMarshaler != nil {
1228 m := structPointer_Interface(base, prop.stype).(Message)
Bryan Mills8d92cf52016-03-19 05:57:00 +11001229 if err := prop.oneofMarshaler(m, o); err == ErrNil {
1230 return errOneofHasNil
1231 } else if err != nil {
David Symonds59b73b32015-08-24 13:22:02 +10001232 return err
1233 }
1234 }
1235
David Symonds10c93ba2012-08-04 16:38:08 +10001236 // Add unrecognized fields at the end.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001237 if prop.unrecField.IsValid() {
1238 v := *structPointer_Bytes(base, prop.unrecField)
1239 if len(v) > 0 {
1240 o.buf = append(o.buf, v...)
1241 }
David Symonds10c93ba2012-08-04 16:38:08 +10001242 }
1243
David Symonds4646c372013-09-09 13:18:58 +10001244 return state.err
1245}
1246
David Symondsba7896c2014-11-20 15:29:05 +11001247func size_struct(prop *StructProperties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001248 for _, i := range prop.order {
1249 p := prop.Prop[i]
1250 if p.size != nil {
1251 n += p.size(p, base)
1252 }
1253 }
1254
1255 // Add unrecognized fields at the end.
1256 if prop.unrecField.IsValid() {
1257 v := *structPointer_Bytes(base, prop.unrecField)
1258 n += len(v)
1259 }
1260
David Symonds59b73b32015-08-24 13:22:02 +10001261 // Factor in any oneof fields.
Damien Neil08794902015-12-04 07:39:00 +11001262 if prop.oneofSizer != nil {
1263 m := structPointer_Interface(base, prop.stype).(Message)
1264 n += prop.oneofSizer(m)
David Symonds59b73b32015-08-24 13:22:02 +10001265 }
1266
David Symonds0bf1ad52013-10-11 09:07:50 +11001267 return
1268}
1269
David Symonds1d8ba132014-01-13 16:01:15 +11001270var zeroes [20]byte // longer than any conceivable sizeVarint
1271
1272// Encode a struct, preceded by its encoded length (as a varint).
David Symondsba7896c2014-11-20 15:29:05 +11001273func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
David Symonds3ea3e052014-12-22 16:15:28 +11001274 return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
1275}
1276
1277// Encode something, preceded by its encoded length (as a varint).
1278func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
David Symonds1d8ba132014-01-13 16:01:15 +11001279 iLen := len(o.buf)
1280 o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
1281 iMsg := len(o.buf)
David Symonds3ea3e052014-12-22 16:15:28 +11001282 err := enc()
David Symonds1d8ba132014-01-13 16:01:15 +11001283 if err != nil && !state.shouldContinue(err, nil) {
1284 return err
1285 }
1286 lMsg := len(o.buf) - iMsg
1287 lLen := sizeVarint(uint64(lMsg))
1288 switch x := lLen - (iMsg - iLen); {
1289 case x > 0: // actual length is x bytes larger than the space we reserved
1290 // Move msg x bytes right.
1291 o.buf = append(o.buf, zeroes[:x]...)
1292 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1293 case x < 0: // actual length is x bytes smaller than the space we reserved
1294 // Move msg x bytes left.
1295 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1296 o.buf = o.buf[:len(o.buf)+x] // x is negative
1297 }
1298 // Encode the length in the reserved space.
1299 o.buf = o.buf[:iLen]
1300 o.EncodeVarint(uint64(lMsg))
1301 o.buf = o.buf[:len(o.buf)+lMsg]
1302 return state.err
1303}
1304
David Symonds4646c372013-09-09 13:18:58 +10001305// errorState maintains the first error that occurs and updates that error
1306// with additional context.
1307type errorState struct {
1308 err error
1309}
1310
1311// shouldContinue reports whether encoding should continue upon encountering the
David Symondse583a5f2013-09-27 10:02:37 +10001312// given error. If the error is RequiredNotSetError, shouldContinue returns true
David Symonds4646c372013-09-09 13:18:58 +10001313// and, if this is the first appearance of that error, remembers it for future
1314// reporting.
1315//
1316// If prop is not nil, it may update any error with additional context about the
1317// field with the error.
1318func (s *errorState) shouldContinue(err error, prop *Properties) bool {
1319 // Ignore unset required fields.
David Symondse583a5f2013-09-27 10:02:37 +10001320 reqNotSet, ok := err.(*RequiredNotSetError)
David Symonds4646c372013-09-09 13:18:58 +10001321 if !ok {
1322 return false
1323 }
1324 if s.err == nil {
1325 if prop != nil {
David Symondse583a5f2013-09-27 10:02:37 +10001326 err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
David Symonds4646c372013-09-09 13:18:58 +10001327 }
1328 s.err = err
1329 }
1330 return true
Rob Pikeaaa3a622010-03-20 22:32:34 -07001331}