| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 4 | // http://code.google.com/p/goprotobuf/ |
| 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 ( |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 39 | "errors" |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 40 | "fmt" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | "reflect" |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 42 | "sort" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 45 | // RequiredNotSetError is the error returned if Marshal is called with |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | // a protocol buffer struct whose required fields have not |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 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. |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 50 | // |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 51 | // When printed, RequiredNotSetError reports the first unset required field in a |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 52 | // message. If the field cannot be precisely determined, it is reported as |
| 53 | // "{Unknown}". |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 54 | type RequiredNotSetError struct { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 55 | field string |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 56 | } |
| 57 | |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 58 | func (e *RequiredNotSetError) Error() string { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 59 | return fmt.Sprintf("proto: required field %q not set", e.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 60 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 61 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 62 | var ( |
| 63 | // ErrRepeatedHasNil is the error returned if Marshal is called with |
| 64 | // a struct with a repeated field containing a nil element. |
| David Symonds | 381349d | 2012-09-18 15:11:46 +1000 | [diff] [blame] | 65 | ErrRepeatedHasNil = errors.New("proto: repeated field has nil element") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 66 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 67 | // ErrNil is the error returned if Marshal is called with nil. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 68 | ErrNil = errors.New("proto: Marshal called with nil") |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 69 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 70 | |
| 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 Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 75 | const maxVarintBytes = 10 // maximum length of a varint |
| 76 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 77 | // 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. |
| 83 | func EncodeVarint(x uint64) []byte { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 84 | var buf [maxVarintBytes]byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 85 | 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 Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 99 | func (p *Buffer) EncodeVarint(x uint64) error { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 100 | for x >= 1<<7 { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 101 | p.buf = append(p.buf, uint8(x&0x7f|0x80)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 102 | x >>= 7 |
| 103 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 104 | p.buf = append(p.buf, uint8(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // EncodeFixed64 writes a 64-bit integer to the Buffer. |
| 109 | // This is the format for the |
| 110 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 111 | func (p *Buffer) EncodeFixed64(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 112 | p.buf = append(p.buf, |
| 113 | uint8(x), |
| 114 | uint8(x>>8), |
| 115 | uint8(x>>16), |
| 116 | uint8(x>>24), |
| 117 | uint8(x>>32), |
| 118 | uint8(x>>40), |
| 119 | uint8(x>>48), |
| 120 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 121 | return nil |
| 122 | } |
| 123 | |
| 124 | // EncodeFixed32 writes a 32-bit integer to the Buffer. |
| 125 | // This is the format for the |
| 126 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 127 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 128 | p.buf = append(p.buf, |
| 129 | uint8(x), |
| 130 | uint8(x>>8), |
| 131 | uint8(x>>16), |
| 132 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 133 | return nil |
| 134 | } |
| 135 | |
| 136 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 137 | // to the Buffer. |
| 138 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 139 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 140 | // use signed number to get arithmetic right shift. |
| 141 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 142 | } |
| 143 | |
| 144 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 145 | // to the Buffer. |
| 146 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 147 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | // use signed number to get arithmetic right shift. |
| 149 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 150 | } |
| 151 | |
| 152 | // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. |
| 153 | // This is the format used for the bytes protocol buffer |
| 154 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 155 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 156 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 157 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 158 | return nil |
| 159 | } |
| 160 | |
| 161 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 162 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 163 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 164 | p.EncodeVarint(uint64(len(s))) |
| 165 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 166 | return nil |
| 167 | } |
| 168 | |
| 169 | // Marshaler is the interface representing objects that can marshal themselves. |
| 170 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 171 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 174 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 175 | // and encodes it into the wire format, returning the data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 176 | func Marshal(pb Message) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 177 | // Can the object marshal itself? |
| 178 | if m, ok := pb.(Marshaler); ok { |
| 179 | return m.Marshal() |
| 180 | } |
| 181 | p := NewBuffer(nil) |
| 182 | err := p.Marshal(pb) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 183 | var state errorState |
| 184 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 185 | return nil, err |
| 186 | } |
| 187 | return p.buf, err |
| 188 | } |
| 189 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 190 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 191 | // and encodes it into the wire format, writing the result to the |
| 192 | // Buffer. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 193 | func (p *Buffer) Marshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 194 | // Can the object marshal itself? |
| 195 | if m, ok := pb.(Marshaler); ok { |
| 196 | data, err := m.Marshal() |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 200 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 201 | return nil |
| 202 | } |
| 203 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 204 | t, base, err := getbase(pb) |
| 205 | if structPointer_IsNil(base) { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 206 | return ErrNil |
| 207 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 208 | if err == nil { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 209 | err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 212 | if collectStats { |
| 213 | stats.Encode++ |
| 214 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 215 | |
| 216 | return err |
| 217 | } |
| 218 | |
| 219 | // Individual type encoders. |
| 220 | |
| 221 | // Encode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 222 | func (o *Buffer) enc_bool(p *Properties, base structPointer) error { |
| 223 | v := *structPointer_Bool(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 224 | if v == nil { |
| 225 | return ErrNil |
| 226 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 227 | x := 0 |
| 228 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 229 | x = 1 |
| 230 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 231 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 232 | p.valEnc(o, uint64(x)) |
| 233 | return nil |
| 234 | } |
| 235 | |
| 236 | // Encode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 237 | func (o *Buffer) enc_int32(p *Properties, base structPointer) error { |
| 238 | v := structPointer_Word32(base, p.field) |
| 239 | if word32_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 240 | return ErrNil |
| 241 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 242 | x := word32_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 243 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 244 | p.valEnc(o, uint64(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | return nil |
| 246 | } |
| 247 | |
| 248 | // Encode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 249 | func (o *Buffer) enc_int64(p *Properties, base structPointer) error { |
| 250 | v := structPointer_Word64(base, p.field) |
| 251 | if word64_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 252 | return ErrNil |
| 253 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 254 | x := word64_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 255 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 256 | p.valEnc(o, x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 257 | return nil |
| 258 | } |
| 259 | |
| 260 | // Encode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 261 | func (o *Buffer) enc_string(p *Properties, base structPointer) error { |
| 262 | v := *structPointer_String(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 263 | if v == nil { |
| 264 | return ErrNil |
| 265 | } |
| 266 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 267 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 268 | o.EncodeStringBytes(x) |
| 269 | return nil |
| 270 | } |
| 271 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 272 | // All protocol buffer fields are nillable, but be careful. |
| 273 | func isNil(v reflect.Value) bool { |
| 274 | switch v.Kind() { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 275 | case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 276 | return v.IsNil() |
| 277 | } |
| 278 | return false |
| 279 | } |
| 280 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 281 | // Encode a message struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 282 | func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 283 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 284 | structp := structPointer_GetStructPointer(base, p.field) |
| 285 | if structPointer_IsNil(structp) { |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 286 | return ErrNil |
| 287 | } |
| 288 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 289 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 290 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 291 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 292 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 293 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 294 | return err |
| 295 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 296 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 297 | o.EncodeRawBytes(data) |
| 298 | return nil |
| 299 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 300 | |
| 301 | // need the length before we can write out the message itself, |
| 302 | // so marshal into a separate byte buffer first. |
| 303 | obuf := o.buf |
| 304 | o.buf = o.bufalloc() |
| 305 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 306 | err := o.enc_struct(p.stype, p.sprop, structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 307 | |
| 308 | nbuf := o.buf |
| 309 | o.buf = obuf |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 310 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 311 | o.buffree(nbuf) |
| 312 | return err |
| 313 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 314 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 315 | o.EncodeRawBytes(nbuf) |
| 316 | o.buffree(nbuf) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 317 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Encode a group struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 321 | func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 322 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 323 | b := structPointer_GetStructPointer(base, p.field) |
| 324 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 325 | return ErrNil |
| 326 | } |
| 327 | |
| 328 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 329 | err := o.enc_struct(p.stype, p.sprop, b) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 330 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 331 | return err |
| 332 | } |
| 333 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 334 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Encode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 338 | func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { |
| 339 | s := *structPointer_BoolSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 340 | l := len(s) |
| 341 | if l == 0 { |
| 342 | return ErrNil |
| 343 | } |
| 344 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 345 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 346 | v := uint64(0) |
| 347 | if x { |
| 348 | v = 1 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 349 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 350 | p.valEnc(o, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 351 | } |
| 352 | return nil |
| 353 | } |
| 354 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 355 | // Encode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 356 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { |
| 357 | s := *structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 358 | l := len(s) |
| 359 | if l == 0 { |
| 360 | return ErrNil |
| 361 | } |
| 362 | o.buf = append(o.buf, p.tagcode...) |
| 363 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 364 | for _, x := range s { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 365 | v := uint64(0) |
| 366 | if x { |
| 367 | v = 1 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 368 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 369 | p.valEnc(o, v) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 370 | } |
| 371 | return nil |
| 372 | } |
| 373 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 374 | // Encode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 375 | func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { |
| 376 | s := *structPointer_Bytes(base, p.field) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 377 | if s == nil { |
| 378 | return ErrNil |
| 379 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 380 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 381 | o.EncodeRawBytes(s) |
| 382 | return nil |
| 383 | } |
| 384 | |
| 385 | // Encode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 386 | func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { |
| 387 | s := structPointer_Word32Slice(base, p.field) |
| 388 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 389 | if l == 0 { |
| 390 | return ErrNil |
| 391 | } |
| 392 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 393 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 394 | x := s.Index(i) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 395 | p.valEnc(o, uint64(x)) |
| 396 | } |
| 397 | return nil |
| 398 | } |
| 399 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 400 | // Encode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 401 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { |
| 402 | s := structPointer_Word32Slice(base, p.field) |
| 403 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 404 | if l == 0 { |
| 405 | return ErrNil |
| 406 | } |
| 407 | // TODO: Reuse a Buffer. |
| 408 | buf := NewBuffer(nil) |
| 409 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 410 | p.valEnc(buf, uint64(s.Index(i))) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | o.buf = append(o.buf, p.tagcode...) |
| 414 | o.EncodeVarint(uint64(len(buf.buf))) |
| 415 | o.buf = append(o.buf, buf.buf...) |
| 416 | return nil |
| 417 | } |
| 418 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 419 | // Encode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 420 | func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { |
| 421 | s := structPointer_Word64Slice(base, p.field) |
| 422 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 423 | if l == 0 { |
| 424 | return ErrNil |
| 425 | } |
| 426 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 427 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 428 | p.valEnc(o, s.Index(i)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 429 | } |
| 430 | return nil |
| 431 | } |
| 432 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 433 | // Encode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 434 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { |
| 435 | s := structPointer_Word64Slice(base, p.field) |
| 436 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 437 | if l == 0 { |
| 438 | return ErrNil |
| 439 | } |
| 440 | // TODO: Reuse a Buffer. |
| 441 | buf := NewBuffer(nil) |
| 442 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 443 | p.valEnc(buf, s.Index(i)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | o.buf = append(o.buf, p.tagcode...) |
| 447 | o.EncodeVarint(uint64(len(buf.buf))) |
| 448 | o.buf = append(o.buf, buf.buf...) |
| 449 | return nil |
| 450 | } |
| 451 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 452 | // Encode a slice of slice of bytes ([][]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 453 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { |
| 454 | ss := *structPointer_BytesSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 455 | l := len(ss) |
| 456 | if l == 0 { |
| 457 | return ErrNil |
| 458 | } |
| 459 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 460 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 461 | s := ss[i] |
| 462 | o.EncodeRawBytes(s) |
| 463 | } |
| 464 | return nil |
| 465 | } |
| 466 | |
| 467 | // Encode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 468 | func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { |
| 469 | ss := *structPointer_StringSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 470 | l := len(ss) |
| 471 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 472 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 473 | s := ss[i] |
| 474 | o.EncodeStringBytes(s) |
| 475 | } |
| 476 | return nil |
| 477 | } |
| 478 | |
| 479 | // Encode a slice of message structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 480 | func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 481 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 482 | s := structPointer_StructPointerSlice(base, p.field) |
| 483 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 484 | |
| 485 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 486 | structp := s.Index(i) |
| 487 | if structPointer_IsNil(structp) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 488 | return ErrRepeatedHasNil |
| 489 | } |
| 490 | |
| 491 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 492 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 493 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 494 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 495 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 496 | return err |
| 497 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 498 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 499 | o.EncodeRawBytes(data) |
| 500 | continue |
| 501 | } |
| 502 | |
| 503 | obuf := o.buf |
| 504 | o.buf = o.bufalloc() |
| 505 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 506 | err := o.enc_struct(p.stype, p.sprop, structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 507 | |
| 508 | nbuf := o.buf |
| 509 | o.buf = obuf |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 510 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 511 | o.buffree(nbuf) |
| 512 | if err == ErrNil { |
| 513 | return ErrRepeatedHasNil |
| 514 | } |
| 515 | return err |
| 516 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 517 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 518 | o.EncodeRawBytes(nbuf) |
| 519 | |
| 520 | o.buffree(nbuf) |
| 521 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 522 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // Encode a slice of group structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 526 | func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 527 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 528 | s := structPointer_StructPointerSlice(base, p.field) |
| 529 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 530 | |
| 531 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 532 | b := s.Index(i) |
| 533 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 534 | return ErrRepeatedHasNil |
| 535 | } |
| 536 | |
| 537 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 538 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 539 | err := o.enc_struct(p.stype, p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 540 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 541 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 542 | if err == ErrNil { |
| 543 | return ErrRepeatedHasNil |
| 544 | } |
| 545 | return err |
| 546 | } |
| 547 | |
| 548 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 549 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 550 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // Encode an extension map. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 554 | func (o *Buffer) enc_map(p *Properties, base structPointer) error { |
| 555 | v := *structPointer_ExtMap(base, p.field) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 556 | if err := encodeExtensionMap(v); err != nil { |
| 557 | return err |
| 558 | } |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 559 | // Fast-path for common cases: zero or one extensions. |
| 560 | if len(v) <= 1 { |
| 561 | for _, e := range v { |
| 562 | o.buf = append(o.buf, e.enc...) |
| 563 | } |
| 564 | return nil |
| 565 | } |
| 566 | |
| 567 | // Sort keys to provide a deterministic encoding. |
| 568 | keys := make([]int, 0, len(v)) |
| 569 | for k := range v { |
| 570 | keys = append(keys, int(k)) |
| 571 | } |
| 572 | sort.Ints(keys) |
| 573 | |
| 574 | for _, k := range keys { |
| 575 | o.buf = append(o.buf, v[int32(k)].enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 576 | } |
| 577 | return nil |
| 578 | } |
| 579 | |
| 580 | // Encode a struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 581 | func (o *Buffer) enc_struct(t reflect.Type, prop *StructProperties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 582 | var state errorState |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 583 | // Encode fields in tag order so that decoders may use optimizations |
| 584 | // that depend on the ordering. |
| 585 | // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order |
| 586 | for _, i := range prop.order { |
| 587 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 588 | if p.enc != nil { |
| 589 | err := p.enc(o, p, base) |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 590 | if err != nil { |
| 591 | if err == ErrNil { |
| 592 | if p.Required && state.err == nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 593 | state.err = &RequiredNotSetError{p.Name} |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 594 | } |
| 595 | } else if !state.shouldContinue(err, p) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 596 | return err |
| 597 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 601 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 602 | // Add unrecognized fields at the end. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 603 | if prop.unrecField.IsValid() { |
| 604 | v := *structPointer_Bytes(base, prop.unrecField) |
| 605 | if len(v) > 0 { |
| 606 | o.buf = append(o.buf, v...) |
| 607 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 608 | } |
| 609 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 610 | return state.err |
| 611 | } |
| 612 | |
| 613 | // errorState maintains the first error that occurs and updates that error |
| 614 | // with additional context. |
| 615 | type errorState struct { |
| 616 | err error |
| 617 | } |
| 618 | |
| 619 | // shouldContinue reports whether encoding should continue upon encountering the |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 620 | // given error. If the error is RequiredNotSetError, shouldContinue returns true |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 621 | // and, if this is the first appearance of that error, remembers it for future |
| 622 | // reporting. |
| 623 | // |
| 624 | // If prop is not nil, it may update any error with additional context about the |
| 625 | // field with the error. |
| 626 | func (s *errorState) shouldContinue(err error, prop *Properties) bool { |
| 627 | // Ignore unset required fields. |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 628 | reqNotSet, ok := err.(*RequiredNotSetError) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 629 | if !ok { |
| 630 | return false |
| 631 | } |
| 632 | if s.err == nil { |
| 633 | if prop != nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame^] | 634 | err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field} |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 635 | } |
| 636 | s.err = err |
| 637 | } |
| 638 | return true |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 639 | } |