| 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 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 108 | func sizeVarint(x uint64) (n int) { |
| 109 | for { |
| 110 | n++ |
| 111 | x >>= 7 |
| 112 | if x == 0 { |
| 113 | break |
| 114 | } |
| 115 | } |
| 116 | return n |
| 117 | } |
| 118 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 119 | // EncodeFixed64 writes a 64-bit integer to the Buffer. |
| 120 | // This is the format for the |
| 121 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 122 | func (p *Buffer) EncodeFixed64(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 123 | p.buf = append(p.buf, |
| 124 | uint8(x), |
| 125 | uint8(x>>8), |
| 126 | uint8(x>>16), |
| 127 | uint8(x>>24), |
| 128 | uint8(x>>32), |
| 129 | uint8(x>>40), |
| 130 | uint8(x>>48), |
| 131 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | return nil |
| 133 | } |
| 134 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 135 | func sizeFixed64(x uint64) int { |
| 136 | return 8 |
| 137 | } |
| 138 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 139 | // EncodeFixed32 writes a 32-bit integer to the Buffer. |
| 140 | // This is the format for the |
| 141 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 142 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 143 | p.buf = append(p.buf, |
| 144 | uint8(x), |
| 145 | uint8(x>>8), |
| 146 | uint8(x>>16), |
| 147 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | return nil |
| 149 | } |
| 150 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 151 | func sizeFixed32(x uint64) int { |
| 152 | return 4 |
| 153 | } |
| 154 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 155 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 156 | // to the Buffer. |
| 157 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 158 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 159 | // use signed number to get arithmetic right shift. |
| 160 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 161 | } |
| 162 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 163 | func sizeZigzag64(x uint64) int { |
| 164 | return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 165 | } |
| 166 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 167 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 168 | // to the Buffer. |
| 169 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 170 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 171 | // use signed number to get arithmetic right shift. |
| 172 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 173 | } |
| 174 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 175 | func sizeZigzag32(x uint64) int { |
| 176 | return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 177 | } |
| 178 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 179 | // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. |
| 180 | // This is the format used for the bytes protocol buffer |
| 181 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 182 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 183 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 184 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 185 | return nil |
| 186 | } |
| 187 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 188 | func sizeRawBytes(b []byte) int { |
| 189 | return sizeVarint(uint64(len(b))) + |
| 190 | len(b) |
| 191 | } |
| 192 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 193 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 194 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 195 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 196 | p.EncodeVarint(uint64(len(s))) |
| 197 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 198 | return nil |
| 199 | } |
| 200 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 201 | func sizeStringBytes(s string) int { |
| 202 | return sizeVarint(uint64(len(s))) + |
| 203 | len(s) |
| 204 | } |
| 205 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | // Marshaler is the interface representing objects that can marshal themselves. |
| 207 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 208 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 211 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 212 | // and encodes it into the wire format, returning the data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 213 | func Marshal(pb Message) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 214 | // Can the object marshal itself? |
| 215 | if m, ok := pb.(Marshaler); ok { |
| 216 | return m.Marshal() |
| 217 | } |
| 218 | p := NewBuffer(nil) |
| 219 | err := p.Marshal(pb) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 220 | var state errorState |
| 221 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 222 | return nil, err |
| 223 | } |
| 224 | return p.buf, err |
| 225 | } |
| 226 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 227 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 228 | // and encodes it into the wire format, writing the result to the |
| 229 | // Buffer. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 230 | func (p *Buffer) Marshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 231 | // Can the object marshal itself? |
| 232 | if m, ok := pb.(Marshaler); ok { |
| 233 | data, err := m.Marshal() |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 237 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 238 | return nil |
| 239 | } |
| 240 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 241 | t, base, err := getbase(pb) |
| 242 | if structPointer_IsNil(base) { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 243 | return ErrNil |
| 244 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | if err == nil { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 246 | err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 249 | if collectStats { |
| 250 | stats.Encode++ |
| 251 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 252 | |
| 253 | return err |
| 254 | } |
| 255 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 256 | // Size returns the encoded size of a protocol buffer. |
| 257 | func Size(pb Message) (n int) { |
| 258 | // Can the object marshal itself? If so, Size is slow. |
| 259 | // TODO: add Size to Marshaler, or add a Sizer interface. |
| 260 | if m, ok := pb.(Marshaler); ok { |
| 261 | b, _ := m.Marshal() |
| 262 | return len(b) |
| 263 | } |
| 264 | |
| 265 | t, base, err := getbase(pb) |
| 266 | if structPointer_IsNil(base) { |
| 267 | return 0 |
| 268 | } |
| 269 | if err == nil { |
| 270 | n = size_struct(t.Elem(), GetProperties(t.Elem()), base) |
| 271 | } |
| 272 | |
| 273 | if collectStats { |
| 274 | stats.Size++ |
| 275 | } |
| 276 | |
| 277 | return |
| 278 | } |
| 279 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 280 | // Individual type encoders. |
| 281 | |
| 282 | // Encode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 283 | func (o *Buffer) enc_bool(p *Properties, base structPointer) error { |
| 284 | v := *structPointer_Bool(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 285 | if v == nil { |
| 286 | return ErrNil |
| 287 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 288 | x := 0 |
| 289 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 290 | x = 1 |
| 291 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 292 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 293 | p.valEnc(o, uint64(x)) |
| 294 | return nil |
| 295 | } |
| 296 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 297 | func size_bool(p *Properties, base structPointer) int { |
| 298 | v := *structPointer_Bool(base, p.field) |
| 299 | if v == nil { |
| 300 | return 0 |
| 301 | } |
| 302 | return len(p.tagcode) + 1 // each bool takes exactly one byte |
| 303 | } |
| 304 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 305 | // Encode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 306 | func (o *Buffer) enc_int32(p *Properties, base structPointer) error { |
| 307 | v := structPointer_Word32(base, p.field) |
| 308 | if word32_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 309 | return ErrNil |
| 310 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 311 | x := word32_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 312 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 313 | p.valEnc(o, uint64(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 314 | return nil |
| 315 | } |
| 316 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 317 | func size_int32(p *Properties, base structPointer) (n int) { |
| 318 | v := structPointer_Word32(base, p.field) |
| 319 | if word32_IsNil(v) { |
| 320 | return 0 |
| 321 | } |
| 322 | x := word32_Get(v) |
| 323 | n += len(p.tagcode) |
| 324 | n += p.valSize(uint64(x)) |
| 325 | return |
| 326 | } |
| 327 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 328 | // Encode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 329 | func (o *Buffer) enc_int64(p *Properties, base structPointer) error { |
| 330 | v := structPointer_Word64(base, p.field) |
| 331 | if word64_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 332 | return ErrNil |
| 333 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 334 | x := word64_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 335 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 336 | p.valEnc(o, x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 337 | return nil |
| 338 | } |
| 339 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 340 | func size_int64(p *Properties, base structPointer) (n int) { |
| 341 | v := structPointer_Word64(base, p.field) |
| 342 | if word64_IsNil(v) { |
| 343 | return 0 |
| 344 | } |
| 345 | x := word64_Get(v) |
| 346 | n += len(p.tagcode) |
| 347 | n += p.valSize(x) |
| 348 | return |
| 349 | } |
| 350 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 351 | // Encode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 352 | func (o *Buffer) enc_string(p *Properties, base structPointer) error { |
| 353 | v := *structPointer_String(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 354 | if v == nil { |
| 355 | return ErrNil |
| 356 | } |
| 357 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 358 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 359 | o.EncodeStringBytes(x) |
| 360 | return nil |
| 361 | } |
| 362 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 363 | func size_string(p *Properties, base structPointer) (n int) { |
| 364 | v := *structPointer_String(base, p.field) |
| 365 | if v == nil { |
| 366 | return 0 |
| 367 | } |
| 368 | x := *v |
| 369 | n += len(p.tagcode) |
| 370 | n += sizeStringBytes(x) |
| 371 | return |
| 372 | } |
| 373 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 374 | // All protocol buffer fields are nillable, but be careful. |
| 375 | func isNil(v reflect.Value) bool { |
| 376 | switch v.Kind() { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 377 | case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 378 | return v.IsNil() |
| 379 | } |
| 380 | return false |
| 381 | } |
| 382 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 383 | // Encode a message struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 384 | func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 385 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 386 | structp := structPointer_GetStructPointer(base, p.field) |
| 387 | if structPointer_IsNil(structp) { |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 388 | return ErrNil |
| 389 | } |
| 390 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 391 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 392 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 393 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 394 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 395 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 396 | return err |
| 397 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 398 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 399 | o.EncodeRawBytes(data) |
| 400 | return nil |
| 401 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 402 | |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 403 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame^] | 404 | return o.enc_len_struct(p.stype, p.sprop, structp, &state) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 407 | func size_struct_message(p *Properties, base structPointer) int { |
| 408 | structp := structPointer_GetStructPointer(base, p.field) |
| 409 | if structPointer_IsNil(structp) { |
| 410 | return 0 |
| 411 | } |
| 412 | |
| 413 | // Can the object marshal itself? |
| 414 | if p.isMarshaler { |
| 415 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 416 | data, _ := m.Marshal() |
| 417 | n0 := len(p.tagcode) |
| 418 | n1 := sizeRawBytes(data) |
| 419 | return n0 + n1 |
| 420 | } |
| 421 | |
| 422 | n0 := len(p.tagcode) |
| 423 | n1 := size_struct(p.stype, p.sprop, structp) |
| 424 | n2 := sizeVarint(uint64(n1)) // size of encoded length |
| 425 | return n0 + n1 + n2 |
| 426 | } |
| 427 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 428 | // Encode a group struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 429 | func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 430 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 431 | b := structPointer_GetStructPointer(base, p.field) |
| 432 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 433 | return ErrNil |
| 434 | } |
| 435 | |
| 436 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 437 | err := o.enc_struct(p.stype, p.sprop, b) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 438 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 439 | return err |
| 440 | } |
| 441 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 442 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 445 | func size_struct_group(p *Properties, base structPointer) (n int) { |
| 446 | b := structPointer_GetStructPointer(base, p.field) |
| 447 | if structPointer_IsNil(b) { |
| 448 | return 0 |
| 449 | } |
| 450 | |
| 451 | n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 452 | n += size_struct(p.stype, p.sprop, b) |
| 453 | n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 454 | return |
| 455 | } |
| 456 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 457 | // Encode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 458 | func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { |
| 459 | s := *structPointer_BoolSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 460 | l := len(s) |
| 461 | if l == 0 { |
| 462 | return ErrNil |
| 463 | } |
| 464 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 465 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 466 | v := uint64(0) |
| 467 | if x { |
| 468 | v = 1 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 469 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 470 | p.valEnc(o, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 471 | } |
| 472 | return nil |
| 473 | } |
| 474 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 475 | func size_slice_bool(p *Properties, base structPointer) int { |
| 476 | s := *structPointer_BoolSlice(base, p.field) |
| 477 | l := len(s) |
| 478 | if l == 0 { |
| 479 | return 0 |
| 480 | } |
| 481 | return l * (len(p.tagcode) + 1) // each bool takes exactly one byte |
| 482 | } |
| 483 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 484 | // Encode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 485 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { |
| 486 | s := *structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 487 | l := len(s) |
| 488 | if l == 0 { |
| 489 | return ErrNil |
| 490 | } |
| 491 | o.buf = append(o.buf, p.tagcode...) |
| 492 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 493 | for _, x := range s { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 494 | v := uint64(0) |
| 495 | if x { |
| 496 | v = 1 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 497 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 498 | p.valEnc(o, v) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 499 | } |
| 500 | return nil |
| 501 | } |
| 502 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 503 | func size_slice_packed_bool(p *Properties, base structPointer) (n int) { |
| 504 | s := *structPointer_BoolSlice(base, p.field) |
| 505 | l := len(s) |
| 506 | if l == 0 { |
| 507 | return 0 |
| 508 | } |
| 509 | n += len(p.tagcode) |
| 510 | n += sizeVarint(uint64(l)) |
| 511 | n += l // each bool takes exactly one byte |
| 512 | return |
| 513 | } |
| 514 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 515 | // Encode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 516 | func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { |
| 517 | s := *structPointer_Bytes(base, p.field) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 518 | if s == nil { |
| 519 | return ErrNil |
| 520 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 521 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | o.EncodeRawBytes(s) |
| 523 | return nil |
| 524 | } |
| 525 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 526 | func size_slice_byte(p *Properties, base structPointer) (n int) { |
| 527 | s := *structPointer_Bytes(base, p.field) |
| 528 | if s == nil { |
| 529 | return 0 |
| 530 | } |
| 531 | n += len(p.tagcode) |
| 532 | n += sizeRawBytes(s) |
| 533 | return |
| 534 | } |
| 535 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 536 | // Encode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 537 | func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { |
| 538 | s := structPointer_Word32Slice(base, p.field) |
| 539 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 540 | if l == 0 { |
| 541 | return ErrNil |
| 542 | } |
| 543 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 544 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 545 | x := s.Index(i) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 546 | p.valEnc(o, uint64(x)) |
| 547 | } |
| 548 | return nil |
| 549 | } |
| 550 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 551 | func size_slice_int32(p *Properties, base structPointer) (n int) { |
| 552 | s := structPointer_Word32Slice(base, p.field) |
| 553 | l := s.Len() |
| 554 | if l == 0 { |
| 555 | return 0 |
| 556 | } |
| 557 | for i := 0; i < l; i++ { |
| 558 | n += len(p.tagcode) |
| 559 | x := s.Index(i) |
| 560 | n += p.valSize(uint64(x)) |
| 561 | } |
| 562 | return |
| 563 | } |
| 564 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 565 | // Encode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 566 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { |
| 567 | s := structPointer_Word32Slice(base, p.field) |
| 568 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 569 | if l == 0 { |
| 570 | return ErrNil |
| 571 | } |
| 572 | // TODO: Reuse a Buffer. |
| 573 | buf := NewBuffer(nil) |
| 574 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 575 | p.valEnc(buf, uint64(s.Index(i))) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | o.buf = append(o.buf, p.tagcode...) |
| 579 | o.EncodeVarint(uint64(len(buf.buf))) |
| 580 | o.buf = append(o.buf, buf.buf...) |
| 581 | return nil |
| 582 | } |
| 583 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 584 | func size_slice_packed_int32(p *Properties, base structPointer) (n int) { |
| 585 | s := structPointer_Word32Slice(base, p.field) |
| 586 | l := s.Len() |
| 587 | if l == 0 { |
| 588 | return 0 |
| 589 | } |
| 590 | var bufSize int |
| 591 | for i := 0; i < l; i++ { |
| 592 | bufSize += p.valSize(uint64(s.Index(i))) |
| 593 | } |
| 594 | |
| 595 | n += len(p.tagcode) |
| 596 | n += sizeVarint(uint64(bufSize)) |
| 597 | n += bufSize |
| 598 | return |
| 599 | } |
| 600 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 601 | // Encode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 602 | func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { |
| 603 | s := structPointer_Word64Slice(base, p.field) |
| 604 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 605 | if l == 0 { |
| 606 | return ErrNil |
| 607 | } |
| 608 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 609 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 610 | p.valEnc(o, s.Index(i)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 611 | } |
| 612 | return nil |
| 613 | } |
| 614 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 615 | func size_slice_int64(p *Properties, base structPointer) (n int) { |
| 616 | s := structPointer_Word64Slice(base, p.field) |
| 617 | l := s.Len() |
| 618 | if l == 0 { |
| 619 | return 0 |
| 620 | } |
| 621 | for i := 0; i < l; i++ { |
| 622 | n += len(p.tagcode) |
| 623 | n += p.valSize(s.Index(i)) |
| 624 | } |
| 625 | return |
| 626 | } |
| 627 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 628 | // Encode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 629 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { |
| 630 | s := structPointer_Word64Slice(base, p.field) |
| 631 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 632 | if l == 0 { |
| 633 | return ErrNil |
| 634 | } |
| 635 | // TODO: Reuse a Buffer. |
| 636 | buf := NewBuffer(nil) |
| 637 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 638 | p.valEnc(buf, s.Index(i)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | o.buf = append(o.buf, p.tagcode...) |
| 642 | o.EncodeVarint(uint64(len(buf.buf))) |
| 643 | o.buf = append(o.buf, buf.buf...) |
| 644 | return nil |
| 645 | } |
| 646 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 647 | func size_slice_packed_int64(p *Properties, base structPointer) (n int) { |
| 648 | s := structPointer_Word64Slice(base, p.field) |
| 649 | l := s.Len() |
| 650 | if l == 0 { |
| 651 | return 0 |
| 652 | } |
| 653 | var bufSize int |
| 654 | for i := 0; i < l; i++ { |
| 655 | bufSize += p.valSize(s.Index(i)) |
| 656 | } |
| 657 | |
| 658 | n += len(p.tagcode) |
| 659 | n += sizeVarint(uint64(bufSize)) |
| 660 | n += bufSize |
| 661 | return |
| 662 | } |
| 663 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 664 | // Encode a slice of slice of bytes ([][]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 665 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { |
| 666 | ss := *structPointer_BytesSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 667 | l := len(ss) |
| 668 | if l == 0 { |
| 669 | return ErrNil |
| 670 | } |
| 671 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 672 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 673 | o.EncodeRawBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 674 | } |
| 675 | return nil |
| 676 | } |
| 677 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 678 | func size_slice_slice_byte(p *Properties, base structPointer) (n int) { |
| 679 | ss := *structPointer_BytesSlice(base, p.field) |
| 680 | l := len(ss) |
| 681 | if l == 0 { |
| 682 | return 0 |
| 683 | } |
| 684 | n += l * len(p.tagcode) |
| 685 | for i := 0; i < l; i++ { |
| 686 | n += sizeRawBytes(ss[i]) |
| 687 | } |
| 688 | return |
| 689 | } |
| 690 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 691 | // Encode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 692 | func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { |
| 693 | ss := *structPointer_StringSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 694 | l := len(ss) |
| 695 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 696 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 697 | o.EncodeStringBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 698 | } |
| 699 | return nil |
| 700 | } |
| 701 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 702 | func size_slice_string(p *Properties, base structPointer) (n int) { |
| 703 | ss := *structPointer_StringSlice(base, p.field) |
| 704 | l := len(ss) |
| 705 | n += l * len(p.tagcode) |
| 706 | for i := 0; i < l; i++ { |
| 707 | n += sizeStringBytes(ss[i]) |
| 708 | } |
| 709 | return |
| 710 | } |
| 711 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 712 | // Encode a slice of message structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 713 | func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 714 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 715 | s := structPointer_StructPointerSlice(base, p.field) |
| 716 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 717 | |
| 718 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 719 | structp := s.Index(i) |
| 720 | if structPointer_IsNil(structp) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 721 | return ErrRepeatedHasNil |
| 722 | } |
| 723 | |
| 724 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 725 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 726 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 727 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 728 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 729 | return err |
| 730 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 731 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 732 | o.EncodeRawBytes(data) |
| 733 | continue |
| 734 | } |
| 735 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame^] | 736 | o.buf = append(o.buf, p.tagcode...) |
| 737 | err := o.enc_len_struct(p.stype, p.sprop, structp, &state) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 738 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 739 | if err == ErrNil { |
| 740 | return ErrRepeatedHasNil |
| 741 | } |
| 742 | return err |
| 743 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 744 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 745 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 748 | func size_slice_struct_message(p *Properties, base structPointer) (n int) { |
| 749 | s := structPointer_StructPointerSlice(base, p.field) |
| 750 | l := s.Len() |
| 751 | n += l * len(p.tagcode) |
| 752 | for i := 0; i < l; i++ { |
| 753 | structp := s.Index(i) |
| 754 | if structPointer_IsNil(structp) { |
| 755 | return // return the size up to this point |
| 756 | } |
| 757 | |
| 758 | // Can the object marshal itself? |
| 759 | if p.isMarshaler { |
| 760 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 761 | data, _ := m.Marshal() |
| 762 | n += len(p.tagcode) |
| 763 | n += sizeRawBytes(data) |
| 764 | continue |
| 765 | } |
| 766 | |
| 767 | n0 := size_struct(p.stype, p.sprop, structp) |
| 768 | n1 := sizeVarint(uint64(n0)) // size of encoded length |
| 769 | n += n0 + n1 |
| 770 | } |
| 771 | return |
| 772 | } |
| 773 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 774 | // Encode a slice of group structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 775 | func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 776 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 777 | s := structPointer_StructPointerSlice(base, p.field) |
| 778 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 779 | |
| 780 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 781 | b := s.Index(i) |
| 782 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 783 | return ErrRepeatedHasNil |
| 784 | } |
| 785 | |
| 786 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 787 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 788 | err := o.enc_struct(p.stype, p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 789 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 790 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 791 | if err == ErrNil { |
| 792 | return ErrRepeatedHasNil |
| 793 | } |
| 794 | return err |
| 795 | } |
| 796 | |
| 797 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 798 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 799 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 802 | func size_slice_struct_group(p *Properties, base structPointer) (n int) { |
| 803 | s := structPointer_StructPointerSlice(base, p.field) |
| 804 | l := s.Len() |
| 805 | |
| 806 | n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup)) |
| 807 | n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup)) |
| 808 | for i := 0; i < l; i++ { |
| 809 | b := s.Index(i) |
| 810 | if structPointer_IsNil(b) { |
| 811 | return // return size up to this point |
| 812 | } |
| 813 | |
| 814 | n += size_struct(p.stype, p.sprop, b) |
| 815 | } |
| 816 | return |
| 817 | } |
| 818 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 819 | // Encode an extension map. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 820 | func (o *Buffer) enc_map(p *Properties, base structPointer) error { |
| 821 | v := *structPointer_ExtMap(base, p.field) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 822 | if err := encodeExtensionMap(v); err != nil { |
| 823 | return err |
| 824 | } |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 825 | // Fast-path for common cases: zero or one extensions. |
| 826 | if len(v) <= 1 { |
| 827 | for _, e := range v { |
| 828 | o.buf = append(o.buf, e.enc...) |
| 829 | } |
| 830 | return nil |
| 831 | } |
| 832 | |
| 833 | // Sort keys to provide a deterministic encoding. |
| 834 | keys := make([]int, 0, len(v)) |
| 835 | for k := range v { |
| 836 | keys = append(keys, int(k)) |
| 837 | } |
| 838 | sort.Ints(keys) |
| 839 | |
| 840 | for _, k := range keys { |
| 841 | o.buf = append(o.buf, v[int32(k)].enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 842 | } |
| 843 | return nil |
| 844 | } |
| 845 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 846 | func size_map(p *Properties, base structPointer) int { |
| 847 | v := *structPointer_ExtMap(base, p.field) |
| 848 | return sizeExtensionMap(v) |
| 849 | } |
| 850 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 851 | // Encode a struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 852 | 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] | 853 | var state errorState |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 854 | // Encode fields in tag order so that decoders may use optimizations |
| 855 | // that depend on the ordering. |
| 856 | // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order |
| 857 | for _, i := range prop.order { |
| 858 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 859 | if p.enc != nil { |
| 860 | err := p.enc(o, p, base) |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 861 | if err != nil { |
| 862 | if err == ErrNil { |
| 863 | if p.Required && state.err == nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 864 | state.err = &RequiredNotSetError{p.Name} |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 865 | } |
| 866 | } else if !state.shouldContinue(err, p) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 867 | return err |
| 868 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 872 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 873 | // Add unrecognized fields at the end. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 874 | if prop.unrecField.IsValid() { |
| 875 | v := *structPointer_Bytes(base, prop.unrecField) |
| 876 | if len(v) > 0 { |
| 877 | o.buf = append(o.buf, v...) |
| 878 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 879 | } |
| 880 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 881 | return state.err |
| 882 | } |
| 883 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 884 | func size_struct(t reflect.Type, prop *StructProperties, base structPointer) (n int) { |
| 885 | for _, i := range prop.order { |
| 886 | p := prop.Prop[i] |
| 887 | if p.size != nil { |
| 888 | n += p.size(p, base) |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // Add unrecognized fields at the end. |
| 893 | if prop.unrecField.IsValid() { |
| 894 | v := *structPointer_Bytes(base, prop.unrecField) |
| 895 | n += len(v) |
| 896 | } |
| 897 | |
| 898 | return |
| 899 | } |
| 900 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame^] | 901 | var zeroes [20]byte // longer than any conceivable sizeVarint |
| 902 | |
| 903 | // Encode a struct, preceded by its encoded length (as a varint). |
| 904 | func (o *Buffer) enc_len_struct(t reflect.Type, prop *StructProperties, base structPointer, state *errorState) error { |
| 905 | iLen := len(o.buf) |
| 906 | o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length |
| 907 | iMsg := len(o.buf) |
| 908 | err := o.enc_struct(t, prop, base) |
| 909 | if err != nil && !state.shouldContinue(err, nil) { |
| 910 | return err |
| 911 | } |
| 912 | lMsg := len(o.buf) - iMsg |
| 913 | lLen := sizeVarint(uint64(lMsg)) |
| 914 | switch x := lLen - (iMsg - iLen); { |
| 915 | case x > 0: // actual length is x bytes larger than the space we reserved |
| 916 | // Move msg x bytes right. |
| 917 | o.buf = append(o.buf, zeroes[:x]...) |
| 918 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 919 | case x < 0: // actual length is x bytes smaller than the space we reserved |
| 920 | // Move msg x bytes left. |
| 921 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 922 | o.buf = o.buf[:len(o.buf)+x] // x is negative |
| 923 | } |
| 924 | // Encode the length in the reserved space. |
| 925 | o.buf = o.buf[:iLen] |
| 926 | o.EncodeVarint(uint64(lMsg)) |
| 927 | o.buf = o.buf[:len(o.buf)+lMsg] |
| 928 | return state.err |
| 929 | } |
| 930 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 931 | // errorState maintains the first error that occurs and updates that error |
| 932 | // with additional context. |
| 933 | type errorState struct { |
| 934 | err error |
| 935 | } |
| 936 | |
| 937 | // shouldContinue reports whether encoding should continue upon encountering the |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 938 | // given error. If the error is RequiredNotSetError, shouldContinue returns true |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 939 | // and, if this is the first appearance of that error, remembers it for future |
| 940 | // reporting. |
| 941 | // |
| 942 | // If prop is not nil, it may update any error with additional context about the |
| 943 | // field with the error. |
| 944 | func (s *errorState) shouldContinue(err error, prop *Properties) bool { |
| 945 | // Ignore unset required fields. |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 946 | reqNotSet, ok := err.(*RequiredNotSetError) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 947 | if !ok { |
| 948 | return false |
| 949 | } |
| 950 | if s.err == nil { |
| 951 | if prop != nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 952 | err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field} |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 953 | } |
| 954 | s.err = err |
| 955 | } |
| 956 | return true |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 957 | } |