github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/protobuf/proto/encode.go (about)

     1  // Go support for Protocol Buffers - Google's data interchange format
     2  //
     3  // Copyright 2010 The Go Authors.  All rights reserved.
     4  // https://yougam/libraries/golang/protobuf
     5  //
     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  
    32  package proto
    33  
    34  /*
    35   * Routines for encoding data into the wire format for protocol buffers.
    36   */
    37  
    38  import (
    39  	"errors"
    40  	"fmt"
    41  	"reflect"
    42  	"sort"
    43  )
    44  
    45  // RequiredNotSetError is the error returned if Marshal is called with
    46  // a protocol buffer struct whose required fields have not
    47  // 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.
    50  //
    51  // When printed, RequiredNotSetError reports the first unset required field in a
    52  // message. If the field cannot be precisely determined, it is reported as
    53  // "{Unknown}".
    54  type RequiredNotSetError struct {
    55  	field string
    56  }
    57  
    58  func (e *RequiredNotSetError) Error() string {
    59  	return fmt.Sprintf("proto: required field %q not set", e.field)
    60  }
    61  
    62  var (
    63  	// errRepeatedHasNil is the error returned if Marshal is called with
    64  	// a struct with a repeated field containing a nil element.
    65  	errRepeatedHasNil = errors.New("proto: repeated field has nil element")
    66  
    67  	// 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  
    71  	// ErrNil is the error returned if Marshal is called with nil.
    72  	ErrNil = errors.New("proto: Marshal called with nil")
    73  )
    74  
    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  
    79  const maxVarintBytes = 10 // maximum length of a varint
    80  
    81  // 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.
    87  func EncodeVarint(x uint64) []byte {
    88  	var buf [maxVarintBytes]byte
    89  	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.
   103  func (p *Buffer) EncodeVarint(x uint64) error {
   104  	for x >= 1<<7 {
   105  		p.buf = append(p.buf, uint8(x&0x7f|0x80))
   106  		x >>= 7
   107  	}
   108  	p.buf = append(p.buf, uint8(x))
   109  	return nil
   110  }
   111  
   112  // SizeVarint returns the varint encoding size of an integer.
   113  func SizeVarint(x uint64) int {
   114  	return sizeVarint(x)
   115  }
   116  
   117  func 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  
   128  // EncodeFixed64 writes a 64-bit integer to the Buffer.
   129  // This is the format for the
   130  // fixed64, sfixed64, and double protocol buffer types.
   131  func (p *Buffer) EncodeFixed64(x uint64) error {
   132  	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))
   141  	return nil
   142  }
   143  
   144  func sizeFixed64(x uint64) int {
   145  	return 8
   146  }
   147  
   148  // EncodeFixed32 writes a 32-bit integer to the Buffer.
   149  // This is the format for the
   150  // fixed32, sfixed32, and float protocol buffer types.
   151  func (p *Buffer) EncodeFixed32(x uint64) error {
   152  	p.buf = append(p.buf,
   153  		uint8(x),
   154  		uint8(x>>8),
   155  		uint8(x>>16),
   156  		uint8(x>>24))
   157  	return nil
   158  }
   159  
   160  func sizeFixed32(x uint64) int {
   161  	return 4
   162  }
   163  
   164  // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
   165  // to the Buffer.
   166  // This is the format used for the sint64 protocol buffer type.
   167  func (p *Buffer) EncodeZigzag64(x uint64) error {
   168  	// use signed number to get arithmetic right shift.
   169  	return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
   170  }
   171  
   172  func sizeZigzag64(x uint64) int {
   173  	return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
   174  }
   175  
   176  // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
   177  // to the Buffer.
   178  // This is the format used for the sint32 protocol buffer type.
   179  func (p *Buffer) EncodeZigzag32(x uint64) error {
   180  	// use signed number to get arithmetic right shift.
   181  	return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
   182  }
   183  
   184  func sizeZigzag32(x uint64) int {
   185  	return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
   186  }
   187  
   188  // 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.
   191  func (p *Buffer) EncodeRawBytes(b []byte) error {
   192  	p.EncodeVarint(uint64(len(b)))
   193  	p.buf = append(p.buf, b...)
   194  	return nil
   195  }
   196  
   197  func sizeRawBytes(b []byte) int {
   198  	return sizeVarint(uint64(len(b))) +
   199  		len(b)
   200  }
   201  
   202  // EncodeStringBytes writes an encoded string to the Buffer.
   203  // This is the format used for the proto2 string type.
   204  func (p *Buffer) EncodeStringBytes(s string) error {
   205  	p.EncodeVarint(uint64(len(s)))
   206  	p.buf = append(p.buf, s...)
   207  	return nil
   208  }
   209  
   210  func sizeStringBytes(s string) int {
   211  	return sizeVarint(uint64(len(s))) +
   212  		len(s)
   213  }
   214  
   215  // Marshaler is the interface representing objects that can marshal themselves.
   216  type Marshaler interface {
   217  	Marshal() ([]byte, error)
   218  }
   219  
   220  // Marshal takes the protocol buffer
   221  // and encodes it into the wire format, returning the data.
   222  func Marshal(pb Message) ([]byte, error) {
   223  	// 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)
   229  	var state errorState
   230  	if err != nil && !state.shouldContinue(err, nil) {
   231  		return nil, err
   232  	}
   233  	if p.buf == nil && err == nil {
   234  		// Return a non-nil slice on success.
   235  		return []byte{}, nil
   236  	}
   237  	return p.buf, err
   238  }
   239  
   240  // EncodeMessage writes the protocol buffer to the Buffer,
   241  // prefixed by a varint-encoded length.
   242  func (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  
   254  // Marshal takes the protocol buffer
   255  // and encodes it into the wire format, writing the result to the
   256  // Buffer.
   257  func (p *Buffer) Marshal(pb Message) error {
   258  	// 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  		}
   264  		p.buf = append(p.buf, data...)
   265  		return nil
   266  	}
   267  
   268  	t, base, err := getbase(pb)
   269  	if structPointer_IsNil(base) {
   270  		return ErrNil
   271  	}
   272  	if err == nil {
   273  		err = p.enc_struct(GetProperties(t.Elem()), base)
   274  	}
   275  
   276  	if collectStats {
   277  		stats.Encode++
   278  	}
   279  
   280  	return err
   281  }
   282  
   283  // Size returns the encoded size of a protocol buffer.
   284  func 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 {
   297  		n = size_struct(GetProperties(t.Elem()), base)
   298  	}
   299  
   300  	if collectStats {
   301  		stats.Size++
   302  	}
   303  
   304  	return
   305  }
   306  
   307  // Individual type encoders.
   308  
   309  // Encode a bool.
   310  func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
   311  	v := *structPointer_Bool(base, p.field)
   312  	if v == nil {
   313  		return ErrNil
   314  	}
   315  	x := 0
   316  	if *v {
   317  		x = 1
   318  	}
   319  	o.buf = append(o.buf, p.tagcode...)
   320  	p.valEnc(o, uint64(x))
   321  	return nil
   322  }
   323  
   324  func (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  
   334  func 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  
   342  func size_proto3_bool(p *Properties, base structPointer) int {
   343  	v := *structPointer_BoolVal(base, p.field)
   344  	if !v && !p.oneof {
   345  		return 0
   346  	}
   347  	return len(p.tagcode) + 1 // each bool takes exactly one byte
   348  }
   349  
   350  // Encode an int32.
   351  func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
   352  	v := structPointer_Word32(base, p.field)
   353  	if word32_IsNil(v) {
   354  		return ErrNil
   355  	}
   356  	x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
   357  	o.buf = append(o.buf, p.tagcode...)
   358  	p.valEnc(o, uint64(x))
   359  	return nil
   360  }
   361  
   362  func (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  
   373  func size_int32(p *Properties, base structPointer) (n int) {
   374  	v := structPointer_Word32(base, p.field)
   375  	if word32_IsNil(v) {
   376  		return 0
   377  	}
   378  	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  
   384  func 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
   387  	if x == 0 && !p.oneof {
   388  		return 0
   389  	}
   390  	n += len(p.tagcode)
   391  	n += p.valSize(uint64(x))
   392  	return
   393  }
   394  
   395  // Encode a uint32.
   396  // Exactly the same as int32, except for no sign extension.
   397  func (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  
   408  func (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  
   419  func size_uint32(p *Properties, base structPointer) (n int) {
   420  	v := structPointer_Word32(base, p.field)
   421  	if word32_IsNil(v) {
   422  		return 0
   423  	}
   424  	x := word32_Get(v)
   425  	n += len(p.tagcode)
   426  	n += p.valSize(uint64(x))
   427  	return
   428  }
   429  
   430  func size_proto3_uint32(p *Properties, base structPointer) (n int) {
   431  	v := structPointer_Word32Val(base, p.field)
   432  	x := word32Val_Get(v)
   433  	if x == 0 && !p.oneof {
   434  		return 0
   435  	}
   436  	n += len(p.tagcode)
   437  	n += p.valSize(uint64(x))
   438  	return
   439  }
   440  
   441  // Encode an int64.
   442  func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
   443  	v := structPointer_Word64(base, p.field)
   444  	if word64_IsNil(v) {
   445  		return ErrNil
   446  	}
   447  	x := word64_Get(v)
   448  	o.buf = append(o.buf, p.tagcode...)
   449  	p.valEnc(o, x)
   450  	return nil
   451  }
   452  
   453  func (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  
   464  func 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  
   475  func size_proto3_int64(p *Properties, base structPointer) (n int) {
   476  	v := structPointer_Word64Val(base, p.field)
   477  	x := word64Val_Get(v)
   478  	if x == 0 && !p.oneof {
   479  		return 0
   480  	}
   481  	n += len(p.tagcode)
   482  	n += p.valSize(x)
   483  	return
   484  }
   485  
   486  // Encode a string.
   487  func (o *Buffer) enc_string(p *Properties, base structPointer) error {
   488  	v := *structPointer_String(base, p.field)
   489  	if v == nil {
   490  		return ErrNil
   491  	}
   492  	x := *v
   493  	o.buf = append(o.buf, p.tagcode...)
   494  	o.EncodeStringBytes(x)
   495  	return nil
   496  }
   497  
   498  func (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  
   508  func 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  
   519  func size_proto3_string(p *Properties, base structPointer) (n int) {
   520  	v := *structPointer_StringVal(base, p.field)
   521  	if v == "" && !p.oneof {
   522  		return 0
   523  	}
   524  	n += len(p.tagcode)
   525  	n += sizeStringBytes(v)
   526  	return
   527  }
   528  
   529  // All protocol buffer fields are nillable, but be careful.
   530  func isNil(v reflect.Value) bool {
   531  	switch v.Kind() {
   532  	case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
   533  		return v.IsNil()
   534  	}
   535  	return false
   536  }
   537  
   538  // Encode a message struct.
   539  func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
   540  	var state errorState
   541  	structp := structPointer_GetStructPointer(base, p.field)
   542  	if structPointer_IsNil(structp) {
   543  		return ErrNil
   544  	}
   545  
   546  	// Can the object marshal itself?
   547  	if p.isMarshaler {
   548  		m := structPointer_Interface(structp, p.stype).(Marshaler)
   549  		data, err := m.Marshal()
   550  		if err != nil && !state.shouldContinue(err, nil) {
   551  			return err
   552  		}
   553  		o.buf = append(o.buf, p.tagcode...)
   554  		o.EncodeRawBytes(data)
   555  		return state.err
   556  	}
   557  
   558  	o.buf = append(o.buf, p.tagcode...)
   559  	return o.enc_len_struct(p.sprop, structp, &state)
   560  }
   561  
   562  func 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)
   578  	n1 := size_struct(p.sprop, structp)
   579  	n2 := sizeVarint(uint64(n1)) // size of encoded length
   580  	return n0 + n1 + n2
   581  }
   582  
   583  // Encode a group struct.
   584  func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
   585  	var state errorState
   586  	b := structPointer_GetStructPointer(base, p.field)
   587  	if structPointer_IsNil(b) {
   588  		return ErrNil
   589  	}
   590  
   591  	o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
   592  	err := o.enc_struct(p.sprop, b)
   593  	if err != nil && !state.shouldContinue(err, nil) {
   594  		return err
   595  	}
   596  	o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
   597  	return state.err
   598  }
   599  
   600  func 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))
   607  	n += size_struct(p.sprop, b)
   608  	n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
   609  	return
   610  }
   611  
   612  // Encode a slice of bools ([]bool).
   613  func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
   614  	s := *structPointer_BoolSlice(base, p.field)
   615  	l := len(s)
   616  	if l == 0 {
   617  		return ErrNil
   618  	}
   619  	for _, x := range s {
   620  		o.buf = append(o.buf, p.tagcode...)
   621  		v := uint64(0)
   622  		if x {
   623  			v = 1
   624  		}
   625  		p.valEnc(o, v)
   626  	}
   627  	return nil
   628  }
   629  
   630  func 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  
   639  // Encode a slice of bools ([]bool) in packed format.
   640  func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
   641  	s := *structPointer_BoolSlice(base, p.field)
   642  	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 {
   649  		v := uint64(0)
   650  		if x {
   651  			v = 1
   652  		}
   653  		p.valEnc(o, v)
   654  	}
   655  	return nil
   656  }
   657  
   658  func 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  
   670  // Encode a slice of bytes ([]byte).
   671  func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
   672  	s := *structPointer_Bytes(base, p.field)
   673  	if s == nil {
   674  		return ErrNil
   675  	}
   676  	o.buf = append(o.buf, p.tagcode...)
   677  	o.EncodeRawBytes(s)
   678  	return nil
   679  }
   680  
   681  func (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  
   691  func size_slice_byte(p *Properties, base structPointer) (n int) {
   692  	s := *structPointer_Bytes(base, p.field)
   693  	if s == nil && !p.oneof {
   694  		return 0
   695  	}
   696  	n += len(p.tagcode)
   697  	n += sizeRawBytes(s)
   698  	return
   699  }
   700  
   701  func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
   702  	s := *structPointer_Bytes(base, p.field)
   703  	if len(s) == 0 && !p.oneof {
   704  		return 0
   705  	}
   706  	n += len(p.tagcode)
   707  	n += sizeRawBytes(s)
   708  	return
   709  }
   710  
   711  // Encode a slice of int32s ([]int32).
   712  func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
   713  	s := structPointer_Word32Slice(base, p.field)
   714  	l := s.Len()
   715  	if l == 0 {
   716  		return ErrNil
   717  	}
   718  	for i := 0; i < l; i++ {
   719  		o.buf = append(o.buf, p.tagcode...)
   720  		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
   721  		p.valEnc(o, uint64(x))
   722  	}
   723  	return nil
   724  }
   725  
   726  func 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)
   734  		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
   735  		n += p.valSize(uint64(x))
   736  	}
   737  	return
   738  }
   739  
   740  // Encode a slice of int32s ([]int32) in packed format.
   741  func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
   742  	s := structPointer_Word32Slice(base, p.field)
   743  	l := s.Len()
   744  	if l == 0 {
   745  		return ErrNil
   746  	}
   747  	// TODO: Reuse a Buffer.
   748  	buf := NewBuffer(nil)
   749  	for i := 0; i < l; i++ {
   750  		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  
   760  func 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.
   780  func (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  
   794  func 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.
   810  func (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++ {
   819  		p.valEnc(buf, uint64(s.Index(i)))
   820  	}
   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  
   828  func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
   829  	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  
   845  // Encode a slice of int64s ([]int64).
   846  func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
   847  	s := structPointer_Word64Slice(base, p.field)
   848  	l := s.Len()
   849  	if l == 0 {
   850  		return ErrNil
   851  	}
   852  	for i := 0; i < l; i++ {
   853  		o.buf = append(o.buf, p.tagcode...)
   854  		p.valEnc(o, s.Index(i))
   855  	}
   856  	return nil
   857  }
   858  
   859  func 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  
   872  // Encode a slice of int64s ([]int64) in packed format.
   873  func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
   874  	s := structPointer_Word64Slice(base, p.field)
   875  	l := s.Len()
   876  	if l == 0 {
   877  		return ErrNil
   878  	}
   879  	// TODO: Reuse a Buffer.
   880  	buf := NewBuffer(nil)
   881  	for i := 0; i < l; i++ {
   882  		p.valEnc(buf, s.Index(i))
   883  	}
   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  
   891  func 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  
   908  // Encode a slice of slice of bytes ([][]byte).
   909  func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
   910  	ss := *structPointer_BytesSlice(base, p.field)
   911  	l := len(ss)
   912  	if l == 0 {
   913  		return ErrNil
   914  	}
   915  	for i := 0; i < l; i++ {
   916  		o.buf = append(o.buf, p.tagcode...)
   917  		o.EncodeRawBytes(ss[i])
   918  	}
   919  	return nil
   920  }
   921  
   922  func 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  
   935  // Encode a slice of strings ([]string).
   936  func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
   937  	ss := *structPointer_StringSlice(base, p.field)
   938  	l := len(ss)
   939  	for i := 0; i < l; i++ {
   940  		o.buf = append(o.buf, p.tagcode...)
   941  		o.EncodeStringBytes(ss[i])
   942  	}
   943  	return nil
   944  }
   945  
   946  func 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  
   956  // Encode a slice of message structs ([]*struct).
   957  func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
   958  	var state errorState
   959  	s := structPointer_StructPointerSlice(base, p.field)
   960  	l := s.Len()
   961  
   962  	for i := 0; i < l; i++ {
   963  		structp := s.Index(i)
   964  		if structPointer_IsNil(structp) {
   965  			return errRepeatedHasNil
   966  		}
   967  
   968  		// Can the object marshal itself?
   969  		if p.isMarshaler {
   970  			m := structPointer_Interface(structp, p.stype).(Marshaler)
   971  			data, err := m.Marshal()
   972  			if err != nil && !state.shouldContinue(err, nil) {
   973  				return err
   974  			}
   975  			o.buf = append(o.buf, p.tagcode...)
   976  			o.EncodeRawBytes(data)
   977  			continue
   978  		}
   979  
   980  		o.buf = append(o.buf, p.tagcode...)
   981  		err := o.enc_len_struct(p.sprop, structp, &state)
   982  		if err != nil && !state.shouldContinue(err, nil) {
   983  			if err == ErrNil {
   984  				return errRepeatedHasNil
   985  			}
   986  			return err
   987  		}
   988  	}
   989  	return state.err
   990  }
   991  
   992  func 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  
  1011  		n0 := size_struct(p.sprop, structp)
  1012  		n1 := sizeVarint(uint64(n0)) // size of encoded length
  1013  		n += n0 + n1
  1014  	}
  1015  	return
  1016  }
  1017  
  1018  // Encode a slice of group structs ([]*struct).
  1019  func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
  1020  	var state errorState
  1021  	s := structPointer_StructPointerSlice(base, p.field)
  1022  	l := s.Len()
  1023  
  1024  	for i := 0; i < l; i++ {
  1025  		b := s.Index(i)
  1026  		if structPointer_IsNil(b) {
  1027  			return errRepeatedHasNil
  1028  		}
  1029  
  1030  		o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  1031  
  1032  		err := o.enc_struct(p.sprop, b)
  1033  
  1034  		if err != nil && !state.shouldContinue(err, nil) {
  1035  			if err == ErrNil {
  1036  				return errRepeatedHasNil
  1037  			}
  1038  			return err
  1039  		}
  1040  
  1041  		o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  1042  	}
  1043  	return state.err
  1044  }
  1045  
  1046  func 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  
  1058  		n += size_struct(p.sprop, b)
  1059  	}
  1060  	return
  1061  }
  1062  
  1063  // Encode an extension map.
  1064  func (o *Buffer) enc_map(p *Properties, base structPointer) error {
  1065  	v := *structPointer_ExtMap(base, p.field)
  1066  	if err := encodeExtensionMap(v); err != nil {
  1067  		return err
  1068  	}
  1069  	// 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...)
  1086  	}
  1087  	return nil
  1088  }
  1089  
  1090  func size_map(p *Properties, base structPointer) int {
  1091  	v := *structPointer_ExtMap(base, p.field)
  1092  	return sizeExtensionMap(v)
  1093  }
  1094  
  1095  // Encode a map field.
  1096  func (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  
  1110  	v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1111  	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  
  1127  	// Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
  1128  	for _, key := range v.MapKeys() {
  1129  		val := v.MapIndex(key)
  1130  
  1131  		// 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  
  1136  		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  
  1147  func size_new_map(p *Properties, base structPointer) int {
  1148  	v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1149  
  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  
  1158  		// 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
  1164  	}
  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.
  1170  func 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  
  1201  // Encode a struct.
  1202  func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
  1203  	var state errorState
  1204  	// Encode fields in tag order so that decoders may use optimizations
  1205  	// that depend on the ordering.
  1206  	// https://developers.google.com/protocol-buffers/docs/encoding#order
  1207  	for _, i := range prop.order {
  1208  		p := prop.Prop[i]
  1209  		if p.enc != nil {
  1210  			err := p.enc(o, p, base)
  1211  			if err != nil {
  1212  				if err == ErrNil {
  1213  					if p.Required && state.err == nil {
  1214  						state.err = &RequiredNotSetError{p.Name}
  1215  					}
  1216  				} 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")
  1219  				} else if !state.shouldContinue(err, p) {
  1220  					return err
  1221  				}
  1222  			}
  1223  		}
  1224  	}
  1225  
  1226  	// Do oneof fields.
  1227  	if prop.oneofMarshaler != nil {
  1228  		m := structPointer_Interface(base, prop.stype).(Message)
  1229  		if err := prop.oneofMarshaler(m, o); err == ErrNil {
  1230  			return errOneofHasNil
  1231  		} else if err != nil {
  1232  			return err
  1233  		}
  1234  	}
  1235  
  1236  	// Add unrecognized fields at the end.
  1237  	if prop.unrecField.IsValid() {
  1238  		v := *structPointer_Bytes(base, prop.unrecField)
  1239  		if len(v) > 0 {
  1240  			o.buf = append(o.buf, v...)
  1241  		}
  1242  	}
  1243  
  1244  	return state.err
  1245  }
  1246  
  1247  func size_struct(prop *StructProperties, base structPointer) (n int) {
  1248  	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  
  1261  	// Factor in any oneof fields.
  1262  	if prop.oneofSizer != nil {
  1263  		m := structPointer_Interface(base, prop.stype).(Message)
  1264  		n += prop.oneofSizer(m)
  1265  	}
  1266  
  1267  	return
  1268  }
  1269  
  1270  var zeroes [20]byte // longer than any conceivable sizeVarint
  1271  
  1272  // Encode a struct, preceded by its encoded length (as a varint).
  1273  func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
  1274  	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).
  1278  func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
  1279  	iLen := len(o.buf)
  1280  	o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
  1281  	iMsg := len(o.buf)
  1282  	err := enc()
  1283  	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  
  1305  // errorState maintains the first error that occurs and updates that error
  1306  // with additional context.
  1307  type errorState struct {
  1308  	err error
  1309  }
  1310  
  1311  // shouldContinue reports whether encoding should continue upon encountering the
  1312  // given error. If the error is RequiredNotSetError, shouldContinue returns true
  1313  // 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.
  1318  func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  1319  	// Ignore unset required fields.
  1320  	reqNotSet, ok := err.(*RequiredNotSetError)
  1321  	if !ok {
  1322  		return false
  1323  	}
  1324  	if s.err == nil {
  1325  		if prop != nil {
  1326  			err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  1327  		}
  1328  		s.err = err
  1329  	}
  1330  	return true
  1331  }