blob: 231b07401a383f46a2dfa5f28d2a1ac32ff7af95 [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
David Symonds7656e742011-07-22 14:54:17 +100067 // ErrNil is the error returned if Marshal is called with nil.
Rob Pikea17fdd92011-11-02 12:43:05 -070068 ErrNil = errors.New("proto: Marshal called with nil")
David Symonds7656e742011-07-22 14:54:17 +100069)
Rob Pikeaaa3a622010-03-20 22:32:34 -070070
71// The fundamental encoders that put bytes on the wire.
72// Those that take integer types all accept uint64 and are
73// therefore of type valueEncoder.
74
David Symonds4fee3b12010-11-11 10:00:13 +110075const maxVarintBytes = 10 // maximum length of a varint
76
Rob Pikeaaa3a622010-03-20 22:32:34 -070077// EncodeVarint returns the varint encoding of x.
78// This is the format for the
79// int32, int64, uint32, uint64, bool, and enum
80// protocol buffer types.
81// Not used by the package itself, but helpful to clients
82// wishing to use the same encoding.
83func EncodeVarint(x uint64) []byte {
David Symonds4fee3b12010-11-11 10:00:13 +110084 var buf [maxVarintBytes]byte
Rob Pikeaaa3a622010-03-20 22:32:34 -070085 var n int
86 for n = 0; x > 127; n++ {
87 buf[n] = 0x80 | uint8(x&0x7F)
88 x >>= 7
89 }
90 buf[n] = uint8(x)
91 n++
92 return buf[0:n]
93}
94
95// EncodeVarint writes a varint-encoded integer to the Buffer.
96// This is the format for the
97// int32, int64, uint32, uint64, bool, and enum
98// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070099func (p *Buffer) EncodeVarint(x uint64) error {
David Symonds4fee3b12010-11-11 10:00:13 +1100100 for x >= 1<<7 {
David Symondsd9da6ba2011-08-30 14:41:30 +1000101 p.buf = append(p.buf, uint8(x&0x7f|0x80))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700102 x >>= 7
103 }
David Symondsd9da6ba2011-08-30 14:41:30 +1000104 p.buf = append(p.buf, uint8(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700105 return nil
106}
107
Damien Neil08794902015-12-04 07:39:00 +1100108// SizeVarint returns the varint encoding size of an integer.
109func SizeVarint(x uint64) int {
110 return sizeVarint(x)
111}
112
David Symonds0bf1ad52013-10-11 09:07:50 +1100113func sizeVarint(x uint64) (n int) {
114 for {
115 n++
116 x >>= 7
117 if x == 0 {
118 break
119 }
120 }
121 return n
122}
123
Rob Pikeaaa3a622010-03-20 22:32:34 -0700124// EncodeFixed64 writes a 64-bit integer to the Buffer.
125// This is the format for the
126// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700127func (p *Buffer) EncodeFixed64(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000128 p.buf = append(p.buf,
129 uint8(x),
130 uint8(x>>8),
131 uint8(x>>16),
132 uint8(x>>24),
133 uint8(x>>32),
134 uint8(x>>40),
135 uint8(x>>48),
136 uint8(x>>56))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700137 return nil
138}
139
David Symonds0bf1ad52013-10-11 09:07:50 +1100140func sizeFixed64(x uint64) int {
141 return 8
142}
143
Rob Pikeaaa3a622010-03-20 22:32:34 -0700144// EncodeFixed32 writes a 32-bit integer to the Buffer.
145// This is the format for the
146// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700147func (p *Buffer) EncodeFixed32(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000148 p.buf = append(p.buf,
149 uint8(x),
150 uint8(x>>8),
151 uint8(x>>16),
152 uint8(x>>24))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700153 return nil
154}
155
David Symonds0bf1ad52013-10-11 09:07:50 +1100156func sizeFixed32(x uint64) int {
157 return 4
158}
159
Rob Pikeaaa3a622010-03-20 22:32:34 -0700160// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
161// to the Buffer.
162// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700163func (p *Buffer) EncodeZigzag64(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700164 // use signed number to get arithmetic right shift.
165 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
166}
167
David Symonds0bf1ad52013-10-11 09:07:50 +1100168func sizeZigzag64(x uint64) int {
169 return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
170}
171
Rob Pikeaaa3a622010-03-20 22:32:34 -0700172// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
173// to the Buffer.
174// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700175func (p *Buffer) EncodeZigzag32(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700176 // use signed number to get arithmetic right shift.
177 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
178}
179
David Symonds0bf1ad52013-10-11 09:07:50 +1100180func sizeZigzag32(x uint64) int {
181 return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
182}
183
Rob Pikeaaa3a622010-03-20 22:32:34 -0700184// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
185// This is the format used for the bytes protocol buffer
186// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700187func (p *Buffer) EncodeRawBytes(b []byte) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700188 p.EncodeVarint(uint64(len(b)))
Rob Pike99fa2b62010-12-02 10:39:42 -0800189 p.buf = append(p.buf, b...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700190 return nil
191}
192
David Symonds0bf1ad52013-10-11 09:07:50 +1100193func sizeRawBytes(b []byte) int {
194 return sizeVarint(uint64(len(b))) +
195 len(b)
196}
197
Rob Pikeaaa3a622010-03-20 22:32:34 -0700198// EncodeStringBytes writes an encoded string to the Buffer.
199// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700200func (p *Buffer) EncodeStringBytes(s string) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700201 p.EncodeVarint(uint64(len(s)))
202 p.buf = append(p.buf, s...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700203 return nil
204}
205
David Symonds0bf1ad52013-10-11 09:07:50 +1100206func sizeStringBytes(s string) int {
207 return sizeVarint(uint64(len(s))) +
208 len(s)
209}
210
Rob Pikeaaa3a622010-03-20 22:32:34 -0700211// Marshaler is the interface representing objects that can marshal themselves.
212type Marshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700213 Marshal() ([]byte, error)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700214}
215
David Symonds9f60f432012-06-14 09:45:25 +1000216// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700217// and encodes it into the wire format, returning the data.
David Symonds9f60f432012-06-14 09:45:25 +1000218func Marshal(pb Message) ([]byte, error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700219 // Can the object marshal itself?
220 if m, ok := pb.(Marshaler); ok {
221 return m.Marshal()
222 }
223 p := NewBuffer(nil)
224 err := p.Marshal(pb)
David Symonds4646c372013-09-09 13:18:58 +1000225 var state errorState
226 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700227 return nil, err
228 }
David Symonds8b253302014-01-14 16:24:19 +1100229 if p.buf == nil && err == nil {
230 // Return a non-nil slice on success.
231 return []byte{}, nil
232 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700233 return p.buf, err
234}
235
David Symonds59b73b32015-08-24 13:22:02 +1000236// EncodeMessage writes the protocol buffer to the Buffer,
237// prefixed by a varint-encoded length.
238func (p *Buffer) EncodeMessage(pb Message) error {
239 t, base, err := getbase(pb)
240 if structPointer_IsNil(base) {
241 return ErrNil
242 }
243 if err == nil {
244 var state errorState
245 err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
246 }
247 return err
248}
249
David Symonds9f60f432012-06-14 09:45:25 +1000250// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700251// and encodes it into the wire format, writing the result to the
252// Buffer.
David Symonds9f60f432012-06-14 09:45:25 +1000253func (p *Buffer) Marshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700254 // Can the object marshal itself?
255 if m, ok := pb.(Marshaler); ok {
256 data, err := m.Marshal()
257 if err != nil {
258 return err
259 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800260 p.buf = append(p.buf, data...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700261 return nil
262 }
263
Russ Coxd4ce3f12012-09-12 10:36:26 +1000264 t, base, err := getbase(pb)
265 if structPointer_IsNil(base) {
David Symondsd4661c52012-08-30 15:17:53 +1000266 return ErrNil
267 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700268 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100269 err = p.enc_struct(GetProperties(t.Elem()), base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700270 }
271
David Symonds9f60f432012-06-14 09:45:25 +1000272 if collectStats {
273 stats.Encode++
274 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700275
276 return err
277}
278
David Symonds0bf1ad52013-10-11 09:07:50 +1100279// Size returns the encoded size of a protocol buffer.
280func Size(pb Message) (n int) {
281 // Can the object marshal itself? If so, Size is slow.
282 // TODO: add Size to Marshaler, or add a Sizer interface.
283 if m, ok := pb.(Marshaler); ok {
284 b, _ := m.Marshal()
285 return len(b)
286 }
287
288 t, base, err := getbase(pb)
289 if structPointer_IsNil(base) {
290 return 0
291 }
292 if err == nil {
David Symondsba7896c2014-11-20 15:29:05 +1100293 n = size_struct(GetProperties(t.Elem()), base)
David Symonds0bf1ad52013-10-11 09:07:50 +1100294 }
295
296 if collectStats {
297 stats.Size++
298 }
299
300 return
301}
302
Rob Pikeaaa3a622010-03-20 22:32:34 -0700303// Individual type encoders.
304
305// Encode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000306func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
307 v := *structPointer_Bool(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700308 if v == nil {
309 return ErrNil
310 }
Rob Pike0f42a272011-10-20 16:03:11 -0700311 x := 0
312 if *v {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700313 x = 1
314 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800315 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700316 p.valEnc(o, uint64(x))
317 return nil
318}
319
David Symondsabd3b412014-11-28 11:43:44 +1100320func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
321 v := *structPointer_BoolVal(base, p.field)
322 if !v {
323 return ErrNil
324 }
325 o.buf = append(o.buf, p.tagcode...)
326 p.valEnc(o, 1)
327 return nil
328}
329
David Symonds0bf1ad52013-10-11 09:07:50 +1100330func size_bool(p *Properties, base structPointer) int {
331 v := *structPointer_Bool(base, p.field)
332 if v == nil {
333 return 0
334 }
335 return len(p.tagcode) + 1 // each bool takes exactly one byte
336}
337
David Symondsabd3b412014-11-28 11:43:44 +1100338func size_proto3_bool(p *Properties, base structPointer) int {
339 v := *structPointer_BoolVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000340 if !v && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100341 return 0
342 }
343 return len(p.tagcode) + 1 // each bool takes exactly one byte
344}
345
Rob Pikeaaa3a622010-03-20 22:32:34 -0700346// Encode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000347func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
348 v := structPointer_Word32(base, p.field)
349 if word32_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700350 return ErrNil
351 }
David Symondsf054e842014-07-22 14:06:27 +1000352 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
Rob Pike99fa2b62010-12-02 10:39:42 -0800353 o.buf = append(o.buf, p.tagcode...)
David Symondsc31645c2013-06-22 17:57:36 +1000354 p.valEnc(o, uint64(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700355 return nil
356}
357
David Symondsabd3b412014-11-28 11:43:44 +1100358func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
359 v := structPointer_Word32Val(base, p.field)
360 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
361 if x == 0 {
362 return ErrNil
363 }
364 o.buf = append(o.buf, p.tagcode...)
365 p.valEnc(o, uint64(x))
366 return nil
367}
368
David Symonds0bf1ad52013-10-11 09:07:50 +1100369func size_int32(p *Properties, base structPointer) (n int) {
370 v := structPointer_Word32(base, p.field)
371 if word32_IsNil(v) {
372 return 0
373 }
David Symondsf054e842014-07-22 14:06:27 +1000374 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
375 n += len(p.tagcode)
376 n += p.valSize(uint64(x))
377 return
378}
379
David Symondsabd3b412014-11-28 11:43:44 +1100380func size_proto3_int32(p *Properties, base structPointer) (n int) {
381 v := structPointer_Word32Val(base, p.field)
382 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
David Symonds535a1042015-09-11 08:27:00 +1000383 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100384 return 0
385 }
386 n += len(p.tagcode)
387 n += p.valSize(uint64(x))
388 return
389}
390
David Symondsf054e842014-07-22 14:06:27 +1000391// Encode a uint32.
392// Exactly the same as int32, except for no sign extension.
393func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
394 v := structPointer_Word32(base, p.field)
395 if word32_IsNil(v) {
396 return ErrNil
397 }
398 x := word32_Get(v)
399 o.buf = append(o.buf, p.tagcode...)
400 p.valEnc(o, uint64(x))
401 return nil
402}
403
David Symondsabd3b412014-11-28 11:43:44 +1100404func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
405 v := structPointer_Word32Val(base, p.field)
406 x := word32Val_Get(v)
407 if x == 0 {
408 return ErrNil
409 }
410 o.buf = append(o.buf, p.tagcode...)
411 p.valEnc(o, uint64(x))
412 return nil
413}
414
David Symondsf054e842014-07-22 14:06:27 +1000415func size_uint32(p *Properties, base structPointer) (n int) {
416 v := structPointer_Word32(base, p.field)
417 if word32_IsNil(v) {
418 return 0
419 }
David Symonds0bf1ad52013-10-11 09:07:50 +1100420 x := word32_Get(v)
421 n += len(p.tagcode)
422 n += p.valSize(uint64(x))
423 return
424}
425
David Symondsabd3b412014-11-28 11:43:44 +1100426func size_proto3_uint32(p *Properties, base structPointer) (n int) {
427 v := structPointer_Word32Val(base, p.field)
428 x := word32Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000429 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100430 return 0
431 }
432 n += len(p.tagcode)
433 n += p.valSize(uint64(x))
434 return
435}
436
Rob Pikeaaa3a622010-03-20 22:32:34 -0700437// Encode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000438func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
439 v := structPointer_Word64(base, p.field)
440 if word64_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700441 return ErrNil
442 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000443 x := word64_Get(v)
Rob Pike99fa2b62010-12-02 10:39:42 -0800444 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000445 p.valEnc(o, x)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700446 return nil
447}
448
David Symondsabd3b412014-11-28 11:43:44 +1100449func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
450 v := structPointer_Word64Val(base, p.field)
451 x := word64Val_Get(v)
452 if x == 0 {
453 return ErrNil
454 }
455 o.buf = append(o.buf, p.tagcode...)
456 p.valEnc(o, x)
457 return nil
458}
459
David Symonds0bf1ad52013-10-11 09:07:50 +1100460func size_int64(p *Properties, base structPointer) (n int) {
461 v := structPointer_Word64(base, p.field)
462 if word64_IsNil(v) {
463 return 0
464 }
465 x := word64_Get(v)
466 n += len(p.tagcode)
467 n += p.valSize(x)
468 return
469}
470
David Symondsabd3b412014-11-28 11:43:44 +1100471func size_proto3_int64(p *Properties, base structPointer) (n int) {
472 v := structPointer_Word64Val(base, p.field)
473 x := word64Val_Get(v)
David Symonds535a1042015-09-11 08:27:00 +1000474 if x == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100475 return 0
476 }
477 n += len(p.tagcode)
478 n += p.valSize(x)
479 return
480}
481
Rob Pikeaaa3a622010-03-20 22:32:34 -0700482// Encode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000483func (o *Buffer) enc_string(p *Properties, base structPointer) error {
484 v := *structPointer_String(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700485 if v == nil {
486 return ErrNil
487 }
488 x := *v
Rob Pike99fa2b62010-12-02 10:39:42 -0800489 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700490 o.EncodeStringBytes(x)
491 return nil
492}
493
David Symondsabd3b412014-11-28 11:43:44 +1100494func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
495 v := *structPointer_StringVal(base, p.field)
496 if v == "" {
497 return ErrNil
498 }
499 o.buf = append(o.buf, p.tagcode...)
500 o.EncodeStringBytes(v)
501 return nil
502}
503
David Symonds0bf1ad52013-10-11 09:07:50 +1100504func size_string(p *Properties, base structPointer) (n int) {
505 v := *structPointer_String(base, p.field)
506 if v == nil {
507 return 0
508 }
509 x := *v
510 n += len(p.tagcode)
511 n += sizeStringBytes(x)
512 return
513}
514
David Symondsabd3b412014-11-28 11:43:44 +1100515func size_proto3_string(p *Properties, base structPointer) (n int) {
516 v := *structPointer_StringVal(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000517 if v == "" && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100518 return 0
519 }
520 n += len(p.tagcode)
521 n += sizeStringBytes(v)
522 return
523}
524
Rob Pike97e934d2011-04-11 12:52:49 -0700525// All protocol buffer fields are nillable, but be careful.
526func isNil(v reflect.Value) bool {
527 switch v.Kind() {
David Symonds007ed9d2012-07-24 10:59:36 +1000528 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
Rob Pike97e934d2011-04-11 12:52:49 -0700529 return v.IsNil()
530 }
531 return false
532}
533
Rob Pikeaaa3a622010-03-20 22:32:34 -0700534// Encode a message struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000535func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000536 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000537 structp := structPointer_GetStructPointer(base, p.field)
538 if structPointer_IsNil(structp) {
David Symondsa80b2822012-03-14 14:31:25 +1100539 return ErrNil
540 }
541
Rob Pikeaaa3a622010-03-20 22:32:34 -0700542 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100543 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000544 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700545 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000546 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700547 return err
548 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800549 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700550 o.EncodeRawBytes(data)
David Symonds68c687d2015-07-27 19:06:00 +1000551 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700552 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700553
Rob Pike99fa2b62010-12-02 10:39:42 -0800554 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100555 return o.enc_len_struct(p.sprop, structp, &state)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700556}
557
David Symonds0bf1ad52013-10-11 09:07:50 +1100558func size_struct_message(p *Properties, base structPointer) int {
559 structp := structPointer_GetStructPointer(base, p.field)
560 if structPointer_IsNil(structp) {
561 return 0
562 }
563
564 // Can the object marshal itself?
565 if p.isMarshaler {
566 m := structPointer_Interface(structp, p.stype).(Marshaler)
567 data, _ := m.Marshal()
568 n0 := len(p.tagcode)
569 n1 := sizeRawBytes(data)
570 return n0 + n1
571 }
572
573 n0 := len(p.tagcode)
David Symondsba7896c2014-11-20 15:29:05 +1100574 n1 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +1100575 n2 := sizeVarint(uint64(n1)) // size of encoded length
576 return n0 + n1 + n2
577}
578
Rob Pikeaaa3a622010-03-20 22:32:34 -0700579// Encode a group struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000580func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000581 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000582 b := structPointer_GetStructPointer(base, p.field)
583 if structPointer_IsNil(b) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700584 return ErrNil
585 }
586
587 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100588 err := o.enc_struct(p.sprop, b)
David Symonds4646c372013-09-09 13:18:58 +1000589 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700590 return err
591 }
592 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
David Symonds4646c372013-09-09 13:18:58 +1000593 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700594}
595
David Symonds0bf1ad52013-10-11 09:07:50 +1100596func size_struct_group(p *Properties, base structPointer) (n int) {
597 b := structPointer_GetStructPointer(base, p.field)
598 if structPointer_IsNil(b) {
599 return 0
600 }
601
602 n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symondsba7896c2014-11-20 15:29:05 +1100603 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +1100604 n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
605 return
606}
607
Rob Pikeaaa3a622010-03-20 22:32:34 -0700608// Encode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000609func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
610 s := *structPointer_BoolSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700611 l := len(s)
612 if l == 0 {
613 return ErrNil
614 }
615 for _, x := range s {
Rob Pike99fa2b62010-12-02 10:39:42 -0800616 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000617 v := uint64(0)
618 if x {
619 v = 1
Rob Pikeaaa3a622010-03-20 22:32:34 -0700620 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000621 p.valEnc(o, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700622 }
623 return nil
624}
625
David Symonds0bf1ad52013-10-11 09:07:50 +1100626func size_slice_bool(p *Properties, base structPointer) int {
627 s := *structPointer_BoolSlice(base, p.field)
628 l := len(s)
629 if l == 0 {
630 return 0
631 }
632 return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
633}
634
David Symonds5b7775e2010-12-01 10:09:04 +1100635// Encode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000636func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
637 s := *structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100638 l := len(s)
639 if l == 0 {
640 return ErrNil
641 }
642 o.buf = append(o.buf, p.tagcode...)
643 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
644 for _, x := range s {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000645 v := uint64(0)
646 if x {
647 v = 1
David Symonds5b7775e2010-12-01 10:09:04 +1100648 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000649 p.valEnc(o, v)
David Symonds5b7775e2010-12-01 10:09:04 +1100650 }
651 return nil
652}
653
David Symonds0bf1ad52013-10-11 09:07:50 +1100654func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
655 s := *structPointer_BoolSlice(base, p.field)
656 l := len(s)
657 if l == 0 {
658 return 0
659 }
660 n += len(p.tagcode)
661 n += sizeVarint(uint64(l))
662 n += l // each bool takes exactly one byte
663 return
664}
665
Rob Pikeaaa3a622010-03-20 22:32:34 -0700666// Encode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000667func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
668 s := *structPointer_Bytes(base, p.field)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800669 if s == nil {
670 return ErrNil
671 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800672 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700673 o.EncodeRawBytes(s)
674 return nil
675}
676
David Symondsabd3b412014-11-28 11:43:44 +1100677func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
678 s := *structPointer_Bytes(base, p.field)
679 if len(s) == 0 {
680 return ErrNil
681 }
682 o.buf = append(o.buf, p.tagcode...)
683 o.EncodeRawBytes(s)
684 return nil
685}
686
David Symonds0bf1ad52013-10-11 09:07:50 +1100687func size_slice_byte(p *Properties, base structPointer) (n int) {
688 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000689 if s == nil && !p.oneof {
David Symonds0bf1ad52013-10-11 09:07:50 +1100690 return 0
691 }
692 n += len(p.tagcode)
693 n += sizeRawBytes(s)
694 return
695}
696
David Symondsabd3b412014-11-28 11:43:44 +1100697func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
698 s := *structPointer_Bytes(base, p.field)
David Symonds535a1042015-09-11 08:27:00 +1000699 if len(s) == 0 && !p.oneof {
David Symondsabd3b412014-11-28 11:43:44 +1100700 return 0
701 }
702 n += len(p.tagcode)
703 n += sizeRawBytes(s)
704 return
705}
706
Rob Pikeaaa3a622010-03-20 22:32:34 -0700707// Encode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000708func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
709 s := structPointer_Word32Slice(base, p.field)
710 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700711 if l == 0 {
712 return ErrNil
713 }
714 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800715 o.buf = append(o.buf, p.tagcode...)
David Symonds0ec36a22014-08-12 13:21:46 +1000716 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
Rob Pikeaaa3a622010-03-20 22:32:34 -0700717 p.valEnc(o, uint64(x))
718 }
719 return nil
720}
721
David Symonds0bf1ad52013-10-11 09:07:50 +1100722func size_slice_int32(p *Properties, base structPointer) (n int) {
723 s := structPointer_Word32Slice(base, p.field)
724 l := s.Len()
725 if l == 0 {
726 return 0
727 }
728 for i := 0; i < l; i++ {
729 n += len(p.tagcode)
David Symonds0ec36a22014-08-12 13:21:46 +1000730 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
David Symonds0bf1ad52013-10-11 09:07:50 +1100731 n += p.valSize(uint64(x))
732 }
733 return
734}
735
David Symonds5b7775e2010-12-01 10:09:04 +1100736// Encode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000737func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
738 s := structPointer_Word32Slice(base, p.field)
739 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100740 if l == 0 {
741 return ErrNil
742 }
743 // TODO: Reuse a Buffer.
744 buf := NewBuffer(nil)
745 for i := 0; i < l; i++ {
David Symonds0ec36a22014-08-12 13:21:46 +1000746 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
747 p.valEnc(buf, uint64(x))
748 }
749
750 o.buf = append(o.buf, p.tagcode...)
751 o.EncodeVarint(uint64(len(buf.buf)))
752 o.buf = append(o.buf, buf.buf...)
753 return nil
754}
755
756func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
757 s := structPointer_Word32Slice(base, p.field)
758 l := s.Len()
759 if l == 0 {
760 return 0
761 }
762 var bufSize int
763 for i := 0; i < l; i++ {
764 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
765 bufSize += p.valSize(uint64(x))
766 }
767
768 n += len(p.tagcode)
769 n += sizeVarint(uint64(bufSize))
770 n += bufSize
771 return
772}
773
774// Encode a slice of uint32s ([]uint32).
775// Exactly the same as int32, except for no sign extension.
776func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
777 s := structPointer_Word32Slice(base, p.field)
778 l := s.Len()
779 if l == 0 {
780 return ErrNil
781 }
782 for i := 0; i < l; i++ {
783 o.buf = append(o.buf, p.tagcode...)
784 x := s.Index(i)
785 p.valEnc(o, uint64(x))
786 }
787 return nil
788}
789
790func size_slice_uint32(p *Properties, base structPointer) (n int) {
791 s := structPointer_Word32Slice(base, p.field)
792 l := s.Len()
793 if l == 0 {
794 return 0
795 }
796 for i := 0; i < l; i++ {
797 n += len(p.tagcode)
798 x := s.Index(i)
799 n += p.valSize(uint64(x))
800 }
801 return
802}
803
804// Encode a slice of uint32s ([]uint32) in packed format.
805// Exactly the same as int32, except for no sign extension.
806func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
807 s := structPointer_Word32Slice(base, p.field)
808 l := s.Len()
809 if l == 0 {
810 return ErrNil
811 }
812 // TODO: Reuse a Buffer.
813 buf := NewBuffer(nil)
814 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000815 p.valEnc(buf, uint64(s.Index(i)))
David Symonds5b7775e2010-12-01 10:09:04 +1100816 }
817
818 o.buf = append(o.buf, p.tagcode...)
819 o.EncodeVarint(uint64(len(buf.buf)))
820 o.buf = append(o.buf, buf.buf...)
821 return nil
822}
823
David Symonds0ec36a22014-08-12 13:21:46 +1000824func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +1100825 s := structPointer_Word32Slice(base, p.field)
826 l := s.Len()
827 if l == 0 {
828 return 0
829 }
830 var bufSize int
831 for i := 0; i < l; i++ {
832 bufSize += p.valSize(uint64(s.Index(i)))
833 }
834
835 n += len(p.tagcode)
836 n += sizeVarint(uint64(bufSize))
837 n += bufSize
838 return
839}
840
Rob Pikeaaa3a622010-03-20 22:32:34 -0700841// Encode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000842func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
843 s := structPointer_Word64Slice(base, p.field)
844 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700845 if l == 0 {
846 return ErrNil
847 }
848 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800849 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000850 p.valEnc(o, s.Index(i))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700851 }
852 return nil
853}
854
David Symonds0bf1ad52013-10-11 09:07:50 +1100855func size_slice_int64(p *Properties, base structPointer) (n int) {
856 s := structPointer_Word64Slice(base, p.field)
857 l := s.Len()
858 if l == 0 {
859 return 0
860 }
861 for i := 0; i < l; i++ {
862 n += len(p.tagcode)
863 n += p.valSize(s.Index(i))
864 }
865 return
866}
867
David Symonds5b7775e2010-12-01 10:09:04 +1100868// Encode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000869func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
870 s := structPointer_Word64Slice(base, p.field)
871 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100872 if l == 0 {
873 return ErrNil
874 }
875 // TODO: Reuse a Buffer.
876 buf := NewBuffer(nil)
877 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000878 p.valEnc(buf, s.Index(i))
David Symonds5b7775e2010-12-01 10:09:04 +1100879 }
880
881 o.buf = append(o.buf, p.tagcode...)
882 o.EncodeVarint(uint64(len(buf.buf)))
883 o.buf = append(o.buf, buf.buf...)
884 return nil
885}
886
David Symonds0bf1ad52013-10-11 09:07:50 +1100887func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
888 s := structPointer_Word64Slice(base, p.field)
889 l := s.Len()
890 if l == 0 {
891 return 0
892 }
893 var bufSize int
894 for i := 0; i < l; i++ {
895 bufSize += p.valSize(s.Index(i))
896 }
897
898 n += len(p.tagcode)
899 n += sizeVarint(uint64(bufSize))
900 n += bufSize
901 return
902}
903
Rob Pikeaaa3a622010-03-20 22:32:34 -0700904// Encode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000905func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
906 ss := *structPointer_BytesSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700907 l := len(ss)
908 if l == 0 {
909 return ErrNil
910 }
911 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800912 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100913 o.EncodeRawBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700914 }
915 return nil
916}
917
David Symonds0bf1ad52013-10-11 09:07:50 +1100918func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
919 ss := *structPointer_BytesSlice(base, p.field)
920 l := len(ss)
921 if l == 0 {
922 return 0
923 }
924 n += l * len(p.tagcode)
925 for i := 0; i < l; i++ {
926 n += sizeRawBytes(ss[i])
927 }
928 return
929}
930
Rob Pikeaaa3a622010-03-20 22:32:34 -0700931// Encode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000932func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
933 ss := *structPointer_StringSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700934 l := len(ss)
935 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800936 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100937 o.EncodeStringBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700938 }
939 return nil
940}
941
David Symonds0bf1ad52013-10-11 09:07:50 +1100942func size_slice_string(p *Properties, base structPointer) (n int) {
943 ss := *structPointer_StringSlice(base, p.field)
944 l := len(ss)
945 n += l * len(p.tagcode)
946 for i := 0; i < l; i++ {
947 n += sizeStringBytes(ss[i])
948 }
949 return
950}
951
Rob Pikeaaa3a622010-03-20 22:32:34 -0700952// Encode a slice of message structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000953func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000954 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000955 s := structPointer_StructPointerSlice(base, p.field)
956 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700957
958 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000959 structp := s.Index(i)
960 if structPointer_IsNil(structp) {
David Symondsf62db482015-02-23 12:37:00 +1100961 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700962 }
963
964 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100965 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000966 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700967 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000968 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700969 return err
970 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800971 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700972 o.EncodeRawBytes(data)
973 continue
974 }
975
David Symonds1d8ba132014-01-13 16:01:15 +1100976 o.buf = append(o.buf, p.tagcode...)
David Symondsba7896c2014-11-20 15:29:05 +1100977 err := o.enc_len_struct(p.sprop, structp, &state)
David Symonds4646c372013-09-09 13:18:58 +1000978 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700979 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +1100980 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -0700981 }
982 return err
983 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700984 }
David Symonds4646c372013-09-09 13:18:58 +1000985 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700986}
987
David Symonds0bf1ad52013-10-11 09:07:50 +1100988func size_slice_struct_message(p *Properties, base structPointer) (n int) {
989 s := structPointer_StructPointerSlice(base, p.field)
990 l := s.Len()
991 n += l * len(p.tagcode)
992 for i := 0; i < l; i++ {
993 structp := s.Index(i)
994 if structPointer_IsNil(structp) {
995 return // return the size up to this point
996 }
997
998 // Can the object marshal itself?
999 if p.isMarshaler {
1000 m := structPointer_Interface(structp, p.stype).(Marshaler)
1001 data, _ := m.Marshal()
1002 n += len(p.tagcode)
1003 n += sizeRawBytes(data)
1004 continue
1005 }
1006
David Symondsba7896c2014-11-20 15:29:05 +11001007 n0 := size_struct(p.sprop, structp)
David Symonds0bf1ad52013-10-11 09:07:50 +11001008 n1 := sizeVarint(uint64(n0)) // size of encoded length
1009 n += n0 + n1
1010 }
1011 return
1012}
1013
Rob Pikeaaa3a622010-03-20 22:32:34 -07001014// Encode a slice of group structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +10001015func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001016 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +10001017 s := structPointer_StructPointerSlice(base, p.field)
1018 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001019
1020 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +10001021 b := s.Index(i)
1022 if structPointer_IsNil(b) {
David Symondsf62db482015-02-23 12:37:00 +11001023 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001024 }
1025
1026 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
1027
David Symondsba7896c2014-11-20 15:29:05 +11001028 err := o.enc_struct(p.sprop, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001029
David Symonds4646c372013-09-09 13:18:58 +10001030 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001031 if err == ErrNil {
David Symondsf62db482015-02-23 12:37:00 +11001032 return errRepeatedHasNil
Rob Pikeaaa3a622010-03-20 22:32:34 -07001033 }
1034 return err
1035 }
1036
1037 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
1038 }
David Symonds4646c372013-09-09 13:18:58 +10001039 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -07001040}
1041
David Symonds0bf1ad52013-10-11 09:07:50 +11001042func size_slice_struct_group(p *Properties, base structPointer) (n int) {
1043 s := structPointer_StructPointerSlice(base, p.field)
1044 l := s.Len()
1045
1046 n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
1047 n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
1048 for i := 0; i < l; i++ {
1049 b := s.Index(i)
1050 if structPointer_IsNil(b) {
1051 return // return size up to this point
1052 }
1053
David Symondsba7896c2014-11-20 15:29:05 +11001054 n += size_struct(p.sprop, b)
David Symonds0bf1ad52013-10-11 09:07:50 +11001055 }
1056 return
1057}
1058
Rob Pikeaaa3a622010-03-20 22:32:34 -07001059// Encode an extension map.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001060func (o *Buffer) enc_map(p *Properties, base structPointer) error {
1061 v := *structPointer_ExtMap(base, p.field)
David Symonds1d72f7a2011-08-19 18:28:52 +10001062 if err := encodeExtensionMap(v); err != nil {
1063 return err
1064 }
David Symondsdf583ae2012-12-06 14:06:53 +11001065 // Fast-path for common cases: zero or one extensions.
1066 if len(v) <= 1 {
1067 for _, e := range v {
1068 o.buf = append(o.buf, e.enc...)
1069 }
1070 return nil
1071 }
1072
1073 // Sort keys to provide a deterministic encoding.
1074 keys := make([]int, 0, len(v))
1075 for k := range v {
1076 keys = append(keys, int(k))
1077 }
1078 sort.Ints(keys)
1079
1080 for _, k := range keys {
1081 o.buf = append(o.buf, v[int32(k)].enc...)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001082 }
1083 return nil
1084}
1085
David Symonds0bf1ad52013-10-11 09:07:50 +11001086func size_map(p *Properties, base structPointer) int {
1087 v := *structPointer_ExtMap(base, p.field)
1088 return sizeExtensionMap(v)
1089}
1090
David Symonds3ea3e052014-12-22 16:15:28 +11001091// Encode a map field.
1092func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
1093 var state errorState // XXX: or do we need to plumb this through?
1094
1095 /*
1096 A map defined as
1097 map<key_type, value_type> map_field = N;
1098 is encoded in the same way as
1099 message MapFieldEntry {
1100 key_type key = 1;
1101 value_type value = 2;
1102 }
1103 repeated MapFieldEntry map_field = N;
1104 */
1105
David Symonds0f7a9ca2015-07-20 16:00:00 +10001106 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001107 if v.Len() == 0 {
1108 return nil
1109 }
1110
1111 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1112
1113 enc := func() error {
1114 if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
1115 return err
1116 }
1117 if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil {
1118 return err
1119 }
1120 return nil
1121 }
1122
David Symondsefcaa342015-10-28 16:14:47 +11001123 // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
1124 for _, key := range v.MapKeys() {
David Symonds3ea3e052014-12-22 16:15:28 +11001125 val := v.MapIndex(key)
1126
David Symondscab84a32015-05-19 09:29:00 +10001127 // The only illegal map entry values are nil message pointers.
1128 if val.Kind() == reflect.Ptr && val.IsNil() {
1129 return errors.New("proto: map has nil element")
1130 }
1131
David Symonds3ea3e052014-12-22 16:15:28 +11001132 keycopy.Set(key)
1133 valcopy.Set(val)
1134
1135 o.buf = append(o.buf, p.tagcode...)
1136 if err := o.enc_len_thing(enc, &state); err != nil {
1137 return err
1138 }
1139 }
1140 return nil
1141}
1142
1143func size_new_map(p *Properties, base structPointer) int {
David Symonds0f7a9ca2015-07-20 16:00:00 +10001144 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +11001145
1146 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1147
1148 n := 0
1149 for _, key := range v.MapKeys() {
1150 val := v.MapIndex(key)
1151 keycopy.Set(key)
1152 valcopy.Set(val)
1153
David Symondsa8323e22015-03-24 10:18:00 +11001154 // Tag codes for key and val are the responsibility of the sub-sizer.
1155 keysize := p.mkeyprop.size(p.mkeyprop, keybase)
1156 valsize := p.mvalprop.size(p.mvalprop, valbase)
1157 entry := keysize + valsize
1158 // Add on tag code and length of map entry itself.
1159 n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
David Symonds3ea3e052014-12-22 16:15:28 +11001160 }
1161 return n
1162}
1163
1164// mapEncodeScratch returns a new reflect.Value matching the map's value type,
1165// and a structPointer suitable for passing to an encoder or sizer.
1166func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
1167 // Prepare addressable doubly-indirect placeholders for the key and value types.
1168 // This is needed because the element-type encoders expect **T, but the map iteration produces T.
1169
1170 keycopy = reflect.New(mapType.Key()).Elem() // addressable K
1171 keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
1172 keyptr.Set(keycopy.Addr()) //
1173 keybase = toStructPointer(keyptr.Addr()) // **K
1174
1175 // Value types are more varied and require special handling.
1176 switch mapType.Elem().Kind() {
1177 case reflect.Slice:
1178 // []byte
1179 var dummy []byte
1180 valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
1181 valbase = toStructPointer(valcopy.Addr())
1182 case reflect.Ptr:
1183 // message; the generated field type is map[K]*Msg (so V is *Msg),
1184 // so we only need one level of indirection.
1185 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1186 valbase = toStructPointer(valcopy.Addr())
1187 default:
1188 // everything else
1189 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1190 valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
1191 valptr.Set(valcopy.Addr()) //
1192 valbase = toStructPointer(valptr.Addr()) // **V
1193 }
1194 return
1195}
1196
Rob Pikeaaa3a622010-03-20 22:32:34 -07001197// Encode a struct.
David Symondsba7896c2014-11-20 15:29:05 +11001198func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +10001199 var state errorState
David Symondsd15e81b2011-10-03 14:31:12 -07001200 // Encode fields in tag order so that decoders may use optimizations
1201 // that depend on the ordering.
David Symonds380d2d02014-11-24 10:50:43 +11001202 // https://developers.google.com/protocol-buffers/docs/encoding#order
David Symondsd15e81b2011-10-03 14:31:12 -07001203 for _, i := range prop.order {
1204 p := prop.Prop[i]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001205 if p.enc != nil {
1206 err := p.enc(o, p, base)
David Symonds4a2eeb52013-09-25 11:54:08 +10001207 if err != nil {
1208 if err == ErrNil {
1209 if p.Required && state.err == nil {
David Symondse583a5f2013-09-27 10:02:37 +10001210 state.err = &RequiredNotSetError{p.Name}
David Symonds4a2eeb52013-09-25 11:54:08 +10001211 }
David Symondsf62db482015-02-23 12:37:00 +11001212 } else if err == errRepeatedHasNil {
1213 // Give more context to nil values in repeated fields.
1214 return errors.New("repeated field " + p.OrigName + " has nil element")
David Symonds4a2eeb52013-09-25 11:54:08 +10001215 } else if !state.shouldContinue(err, p) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001216 return err
1217 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001218 }
1219 }
1220 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001221
David Symonds59b73b32015-08-24 13:22:02 +10001222 // Do oneof fields.
1223 if prop.oneofMarshaler != nil {
1224 m := structPointer_Interface(base, prop.stype).(Message)
1225 if err := prop.oneofMarshaler(m, o); err != nil {
1226 return err
1227 }
1228 }
1229
David Symonds10c93ba2012-08-04 16:38:08 +10001230 // Add unrecognized fields at the end.
Russ Coxd4ce3f12012-09-12 10:36:26 +10001231 if prop.unrecField.IsValid() {
1232 v := *structPointer_Bytes(base, prop.unrecField)
1233 if len(v) > 0 {
1234 o.buf = append(o.buf, v...)
1235 }
David Symonds10c93ba2012-08-04 16:38:08 +10001236 }
1237
David Symonds4646c372013-09-09 13:18:58 +10001238 return state.err
1239}
1240
David Symondsba7896c2014-11-20 15:29:05 +11001241func size_struct(prop *StructProperties, base structPointer) (n int) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001242 for _, i := range prop.order {
1243 p := prop.Prop[i]
1244 if p.size != nil {
1245 n += p.size(p, base)
1246 }
1247 }
1248
1249 // Add unrecognized fields at the end.
1250 if prop.unrecField.IsValid() {
1251 v := *structPointer_Bytes(base, prop.unrecField)
1252 n += len(v)
1253 }
1254
David Symonds59b73b32015-08-24 13:22:02 +10001255 // Factor in any oneof fields.
Damien Neil08794902015-12-04 07:39:00 +11001256 if prop.oneofSizer != nil {
1257 m := structPointer_Interface(base, prop.stype).(Message)
1258 n += prop.oneofSizer(m)
David Symonds59b73b32015-08-24 13:22:02 +10001259 }
1260
David Symonds0bf1ad52013-10-11 09:07:50 +11001261 return
1262}
1263
David Symonds1d8ba132014-01-13 16:01:15 +11001264var zeroes [20]byte // longer than any conceivable sizeVarint
1265
1266// Encode a struct, preceded by its encoded length (as a varint).
David Symondsba7896c2014-11-20 15:29:05 +11001267func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
David Symonds3ea3e052014-12-22 16:15:28 +11001268 return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
1269}
1270
1271// Encode something, preceded by its encoded length (as a varint).
1272func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
David Symonds1d8ba132014-01-13 16:01:15 +11001273 iLen := len(o.buf)
1274 o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
1275 iMsg := len(o.buf)
David Symonds3ea3e052014-12-22 16:15:28 +11001276 err := enc()
David Symonds1d8ba132014-01-13 16:01:15 +11001277 if err != nil && !state.shouldContinue(err, nil) {
1278 return err
1279 }
1280 lMsg := len(o.buf) - iMsg
1281 lLen := sizeVarint(uint64(lMsg))
1282 switch x := lLen - (iMsg - iLen); {
1283 case x > 0: // actual length is x bytes larger than the space we reserved
1284 // Move msg x bytes right.
1285 o.buf = append(o.buf, zeroes[:x]...)
1286 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1287 case x < 0: // actual length is x bytes smaller than the space we reserved
1288 // Move msg x bytes left.
1289 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1290 o.buf = o.buf[:len(o.buf)+x] // x is negative
1291 }
1292 // Encode the length in the reserved space.
1293 o.buf = o.buf[:iLen]
1294 o.EncodeVarint(uint64(lMsg))
1295 o.buf = o.buf[:len(o.buf)+lMsg]
1296 return state.err
1297}
1298
David Symonds4646c372013-09-09 13:18:58 +10001299// errorState maintains the first error that occurs and updates that error
1300// with additional context.
1301type errorState struct {
1302 err error
1303}
1304
1305// shouldContinue reports whether encoding should continue upon encountering the
David Symondse583a5f2013-09-27 10:02:37 +10001306// given error. If the error is RequiredNotSetError, shouldContinue returns true
David Symonds4646c372013-09-09 13:18:58 +10001307// and, if this is the first appearance of that error, remembers it for future
1308// reporting.
1309//
1310// If prop is not nil, it may update any error with additional context about the
1311// field with the error.
1312func (s *errorState) shouldContinue(err error, prop *Properties) bool {
1313 // Ignore unset required fields.
David Symondse583a5f2013-09-27 10:02:37 +10001314 reqNotSet, ok := err.(*RequiredNotSetError)
David Symonds4646c372013-09-09 13:18:58 +10001315 if !ok {
1316 return false
1317 }
1318 if s.err == nil {
1319 if prop != nil {
David Symondse583a5f2013-09-27 10:02:37 +10001320 err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
David Symonds4646c372013-09-09 13:18:58 +10001321 }
1322 s.err = err
1323 }
1324 return true
Rob Pikeaaa3a622010-03-20 22:32:34 -07001325}