| 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. |
| David Symonds | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 4 | // https://github.com/golang/protobuf |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 ( |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 63 | // errRepeatedHasNil is the error returned if Marshal is called with |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 64 | // a struct with a repeated field containing a nil element. |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [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 | |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 108 | // SizeVarint returns the varint encoding size of an integer. |
| 109 | func SizeVarint(x uint64) int { |
| 110 | return sizeVarint(x) |
| 111 | } |
| 112 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 113 | func sizeVarint(x uint64) (n int) { |
| 114 | for { |
| 115 | n++ |
| 116 | x >>= 7 |
| 117 | if x == 0 { |
| 118 | break |
| 119 | } |
| 120 | } |
| 121 | return n |
| 122 | } |
| 123 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 124 | // EncodeFixed64 writes a 64-bit integer to the Buffer. |
| 125 | // This is the format for the |
| 126 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 127 | func (p *Buffer) EncodeFixed64(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), |
| 133 | uint8(x>>32), |
| 134 | uint8(x>>40), |
| 135 | uint8(x>>48), |
| 136 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 137 | return nil |
| 138 | } |
| 139 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 140 | func sizeFixed64(x uint64) int { |
| 141 | return 8 |
| 142 | } |
| 143 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 144 | // EncodeFixed32 writes a 32-bit integer to the Buffer. |
| 145 | // This is the format for the |
| 146 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 147 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 148 | p.buf = append(p.buf, |
| 149 | uint8(x), |
| 150 | uint8(x>>8), |
| 151 | uint8(x>>16), |
| 152 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 153 | return nil |
| 154 | } |
| 155 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 156 | func sizeFixed32(x uint64) int { |
| 157 | return 4 |
| 158 | } |
| 159 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 160 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 161 | // to the Buffer. |
| 162 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 163 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 164 | // use signed number to get arithmetic right shift. |
| 165 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 166 | } |
| 167 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 168 | func sizeZigzag64(x uint64) int { |
| 169 | return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 170 | } |
| 171 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 173 | // to the Buffer. |
| 174 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 175 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | // use signed number to get arithmetic right shift. |
| 177 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 178 | } |
| 179 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 180 | func sizeZigzag32(x uint64) int { |
| 181 | return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 182 | } |
| 183 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 184 | // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. |
| 185 | // This is the format used for the bytes protocol buffer |
| 186 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 187 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 188 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 189 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 190 | return nil |
| 191 | } |
| 192 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 193 | func sizeRawBytes(b []byte) int { |
| 194 | return sizeVarint(uint64(len(b))) + |
| 195 | len(b) |
| 196 | } |
| 197 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 198 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 199 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 200 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 201 | p.EncodeVarint(uint64(len(s))) |
| 202 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 203 | return nil |
| 204 | } |
| 205 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 206 | func sizeStringBytes(s string) int { |
| 207 | return sizeVarint(uint64(len(s))) + |
| 208 | len(s) |
| 209 | } |
| 210 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 211 | // Marshaler is the interface representing objects that can marshal themselves. |
| 212 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 213 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 216 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 217 | // and encodes it into the wire format, returning the data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 218 | func Marshal(pb Message) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 219 | // Can the object marshal itself? |
| 220 | if m, ok := pb.(Marshaler); ok { |
| 221 | return m.Marshal() |
| 222 | } |
| 223 | p := NewBuffer(nil) |
| 224 | err := p.Marshal(pb) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 225 | var state errorState |
| 226 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 227 | return nil, err |
| 228 | } |
| David Symonds | 8b25330 | 2014-01-14 16:24:19 +1100 | [diff] [blame] | 229 | if p.buf == nil && err == nil { |
| 230 | // Return a non-nil slice on success. |
| 231 | return []byte{}, nil |
| 232 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 233 | return p.buf, err |
| 234 | } |
| 235 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 236 | // EncodeMessage writes the protocol buffer to the Buffer, |
| 237 | // prefixed by a varint-encoded length. |
| 238 | func (p *Buffer) EncodeMessage(pb Message) error { |
| 239 | t, base, err := getbase(pb) |
| 240 | if structPointer_IsNil(base) { |
| 241 | return ErrNil |
| 242 | } |
| 243 | if err == nil { |
| 244 | var state errorState |
| 245 | err = p.enc_len_struct(GetProperties(t.Elem()), base, &state) |
| 246 | } |
| 247 | return err |
| 248 | } |
| 249 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 250 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 251 | // and encodes it into the wire format, writing the result to the |
| 252 | // Buffer. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 253 | func (p *Buffer) Marshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 254 | // Can the object marshal itself? |
| 255 | if m, ok := pb.(Marshaler); ok { |
| 256 | data, err := m.Marshal() |
| 257 | if err != nil { |
| 258 | return err |
| 259 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 260 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 261 | return nil |
| 262 | } |
| 263 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 264 | t, base, err := getbase(pb) |
| 265 | if structPointer_IsNil(base) { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 266 | return ErrNil |
| 267 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 268 | if err == nil { |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 269 | err = p.enc_struct(GetProperties(t.Elem()), base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 272 | if collectStats { |
| 273 | stats.Encode++ |
| 274 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 275 | |
| 276 | return err |
| 277 | } |
| 278 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 279 | // Size returns the encoded size of a protocol buffer. |
| 280 | func Size(pb Message) (n int) { |
| 281 | // Can the object marshal itself? If so, Size is slow. |
| 282 | // TODO: add Size to Marshaler, or add a Sizer interface. |
| 283 | if m, ok := pb.(Marshaler); ok { |
| 284 | b, _ := m.Marshal() |
| 285 | return len(b) |
| 286 | } |
| 287 | |
| 288 | t, base, err := getbase(pb) |
| 289 | if structPointer_IsNil(base) { |
| 290 | return 0 |
| 291 | } |
| 292 | if err == nil { |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 293 | n = size_struct(GetProperties(t.Elem()), base) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if collectStats { |
| 297 | stats.Size++ |
| 298 | } |
| 299 | |
| 300 | return |
| 301 | } |
| 302 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 303 | // Individual type encoders. |
| 304 | |
| 305 | // Encode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 306 | func (o *Buffer) enc_bool(p *Properties, base structPointer) error { |
| 307 | v := *structPointer_Bool(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 308 | if v == nil { |
| 309 | return ErrNil |
| 310 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 311 | x := 0 |
| 312 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 313 | x = 1 |
| 314 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 315 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 316 | p.valEnc(o, uint64(x)) |
| 317 | return nil |
| 318 | } |
| 319 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 320 | func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error { |
| 321 | v := *structPointer_BoolVal(base, p.field) |
| 322 | if !v { |
| 323 | return ErrNil |
| 324 | } |
| 325 | o.buf = append(o.buf, p.tagcode...) |
| 326 | p.valEnc(o, 1) |
| 327 | return nil |
| 328 | } |
| 329 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 330 | func size_bool(p *Properties, base structPointer) int { |
| 331 | v := *structPointer_Bool(base, p.field) |
| 332 | if v == nil { |
| 333 | return 0 |
| 334 | } |
| 335 | return len(p.tagcode) + 1 // each bool takes exactly one byte |
| 336 | } |
| 337 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 338 | func size_proto3_bool(p *Properties, base structPointer) int { |
| 339 | v := *structPointer_BoolVal(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 340 | if !v && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 341 | return 0 |
| 342 | } |
| 343 | return len(p.tagcode) + 1 // each bool takes exactly one byte |
| 344 | } |
| 345 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 346 | // Encode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 347 | func (o *Buffer) enc_int32(p *Properties, base structPointer) error { |
| 348 | v := structPointer_Word32(base, p.field) |
| 349 | if word32_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 350 | return ErrNil |
| 351 | } |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 352 | x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 353 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 354 | p.valEnc(o, uint64(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 355 | return nil |
| 356 | } |
| 357 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 358 | func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error { |
| 359 | v := structPointer_Word32Val(base, p.field) |
| 360 | x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range |
| 361 | if x == 0 { |
| 362 | return ErrNil |
| 363 | } |
| 364 | o.buf = append(o.buf, p.tagcode...) |
| 365 | p.valEnc(o, uint64(x)) |
| 366 | return nil |
| 367 | } |
| 368 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 369 | func size_int32(p *Properties, base structPointer) (n int) { |
| 370 | v := structPointer_Word32(base, p.field) |
| 371 | if word32_IsNil(v) { |
| 372 | return 0 |
| 373 | } |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 374 | x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range |
| 375 | n += len(p.tagcode) |
| 376 | n += p.valSize(uint64(x)) |
| 377 | return |
| 378 | } |
| 379 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 380 | func size_proto3_int32(p *Properties, base structPointer) (n int) { |
| 381 | v := structPointer_Word32Val(base, p.field) |
| 382 | x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 383 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 384 | return 0 |
| 385 | } |
| 386 | n += len(p.tagcode) |
| 387 | n += p.valSize(uint64(x)) |
| 388 | return |
| 389 | } |
| 390 | |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 391 | // Encode a uint32. |
| 392 | // Exactly the same as int32, except for no sign extension. |
| 393 | func (o *Buffer) enc_uint32(p *Properties, base structPointer) error { |
| 394 | v := structPointer_Word32(base, p.field) |
| 395 | if word32_IsNil(v) { |
| 396 | return ErrNil |
| 397 | } |
| 398 | x := word32_Get(v) |
| 399 | o.buf = append(o.buf, p.tagcode...) |
| 400 | p.valEnc(o, uint64(x)) |
| 401 | return nil |
| 402 | } |
| 403 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 404 | func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error { |
| 405 | v := structPointer_Word32Val(base, p.field) |
| 406 | x := word32Val_Get(v) |
| 407 | if x == 0 { |
| 408 | return ErrNil |
| 409 | } |
| 410 | o.buf = append(o.buf, p.tagcode...) |
| 411 | p.valEnc(o, uint64(x)) |
| 412 | return nil |
| 413 | } |
| 414 | |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 415 | func size_uint32(p *Properties, base structPointer) (n int) { |
| 416 | v := structPointer_Word32(base, p.field) |
| 417 | if word32_IsNil(v) { |
| 418 | return 0 |
| 419 | } |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 420 | x := word32_Get(v) |
| 421 | n += len(p.tagcode) |
| 422 | n += p.valSize(uint64(x)) |
| 423 | return |
| 424 | } |
| 425 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 426 | func size_proto3_uint32(p *Properties, base structPointer) (n int) { |
| 427 | v := structPointer_Word32Val(base, p.field) |
| 428 | x := word32Val_Get(v) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 429 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 430 | return 0 |
| 431 | } |
| 432 | n += len(p.tagcode) |
| 433 | n += p.valSize(uint64(x)) |
| 434 | return |
| 435 | } |
| 436 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 437 | // Encode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 438 | func (o *Buffer) enc_int64(p *Properties, base structPointer) error { |
| 439 | v := structPointer_Word64(base, p.field) |
| 440 | if word64_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 441 | return ErrNil |
| 442 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 443 | x := word64_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 444 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 445 | p.valEnc(o, x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 446 | return nil |
| 447 | } |
| 448 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 449 | func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error { |
| 450 | v := structPointer_Word64Val(base, p.field) |
| 451 | x := word64Val_Get(v) |
| 452 | if x == 0 { |
| 453 | return ErrNil |
| 454 | } |
| 455 | o.buf = append(o.buf, p.tagcode...) |
| 456 | p.valEnc(o, x) |
| 457 | return nil |
| 458 | } |
| 459 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 460 | func size_int64(p *Properties, base structPointer) (n int) { |
| 461 | v := structPointer_Word64(base, p.field) |
| 462 | if word64_IsNil(v) { |
| 463 | return 0 |
| 464 | } |
| 465 | x := word64_Get(v) |
| 466 | n += len(p.tagcode) |
| 467 | n += p.valSize(x) |
| 468 | return |
| 469 | } |
| 470 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 471 | func size_proto3_int64(p *Properties, base structPointer) (n int) { |
| 472 | v := structPointer_Word64Val(base, p.field) |
| 473 | x := word64Val_Get(v) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 474 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 475 | return 0 |
| 476 | } |
| 477 | n += len(p.tagcode) |
| 478 | n += p.valSize(x) |
| 479 | return |
| 480 | } |
| 481 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 482 | // Encode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 483 | func (o *Buffer) enc_string(p *Properties, base structPointer) error { |
| 484 | v := *structPointer_String(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 485 | if v == nil { |
| 486 | return ErrNil |
| 487 | } |
| 488 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 489 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 490 | o.EncodeStringBytes(x) |
| 491 | return nil |
| 492 | } |
| 493 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 494 | func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error { |
| 495 | v := *structPointer_StringVal(base, p.field) |
| 496 | if v == "" { |
| 497 | return ErrNil |
| 498 | } |
| 499 | o.buf = append(o.buf, p.tagcode...) |
| 500 | o.EncodeStringBytes(v) |
| 501 | return nil |
| 502 | } |
| 503 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 504 | func size_string(p *Properties, base structPointer) (n int) { |
| 505 | v := *structPointer_String(base, p.field) |
| 506 | if v == nil { |
| 507 | return 0 |
| 508 | } |
| 509 | x := *v |
| 510 | n += len(p.tagcode) |
| 511 | n += sizeStringBytes(x) |
| 512 | return |
| 513 | } |
| 514 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 515 | func size_proto3_string(p *Properties, base structPointer) (n int) { |
| 516 | v := *structPointer_StringVal(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 517 | if v == "" && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 518 | return 0 |
| 519 | } |
| 520 | n += len(p.tagcode) |
| 521 | n += sizeStringBytes(v) |
| 522 | return |
| 523 | } |
| 524 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 525 | // All protocol buffer fields are nillable, but be careful. |
| 526 | func isNil(v reflect.Value) bool { |
| 527 | switch v.Kind() { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 528 | case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 529 | return v.IsNil() |
| 530 | } |
| 531 | return false |
| 532 | } |
| 533 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 534 | // Encode a message struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 535 | func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 536 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 537 | structp := structPointer_GetStructPointer(base, p.field) |
| 538 | if structPointer_IsNil(structp) { |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 539 | return ErrNil |
| 540 | } |
| 541 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 542 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 543 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 544 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 545 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 546 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 547 | return err |
| 548 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 549 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 550 | o.EncodeRawBytes(data) |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 551 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 552 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 553 | |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 554 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 555 | return o.enc_len_struct(p.sprop, structp, &state) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 558 | func size_struct_message(p *Properties, base structPointer) int { |
| 559 | structp := structPointer_GetStructPointer(base, p.field) |
| 560 | if structPointer_IsNil(structp) { |
| 561 | return 0 |
| 562 | } |
| 563 | |
| 564 | // Can the object marshal itself? |
| 565 | if p.isMarshaler { |
| 566 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 567 | data, _ := m.Marshal() |
| 568 | n0 := len(p.tagcode) |
| 569 | n1 := sizeRawBytes(data) |
| 570 | return n0 + n1 |
| 571 | } |
| 572 | |
| 573 | n0 := len(p.tagcode) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 574 | n1 := size_struct(p.sprop, structp) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 575 | n2 := sizeVarint(uint64(n1)) // size of encoded length |
| 576 | return n0 + n1 + n2 |
| 577 | } |
| 578 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 579 | // Encode a group struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 580 | func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 581 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 582 | b := structPointer_GetStructPointer(base, p.field) |
| 583 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 584 | return ErrNil |
| 585 | } |
| 586 | |
| 587 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 588 | err := o.enc_struct(p.sprop, b) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 589 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 590 | return err |
| 591 | } |
| 592 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 593 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 596 | func size_struct_group(p *Properties, base structPointer) (n int) { |
| 597 | b := structPointer_GetStructPointer(base, p.field) |
| 598 | if structPointer_IsNil(b) { |
| 599 | return 0 |
| 600 | } |
| 601 | |
| 602 | n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 603 | n += size_struct(p.sprop, b) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 604 | n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 605 | return |
| 606 | } |
| 607 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 608 | // Encode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 609 | func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { |
| 610 | s := *structPointer_BoolSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 611 | l := len(s) |
| 612 | if l == 0 { |
| 613 | return ErrNil |
| 614 | } |
| 615 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 616 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 617 | v := uint64(0) |
| 618 | if x { |
| 619 | v = 1 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 620 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 621 | p.valEnc(o, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 622 | } |
| 623 | return nil |
| 624 | } |
| 625 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 626 | func size_slice_bool(p *Properties, base structPointer) int { |
| 627 | s := *structPointer_BoolSlice(base, p.field) |
| 628 | l := len(s) |
| 629 | if l == 0 { |
| 630 | return 0 |
| 631 | } |
| 632 | return l * (len(p.tagcode) + 1) // each bool takes exactly one byte |
| 633 | } |
| 634 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 635 | // Encode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 636 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { |
| 637 | s := *structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 638 | l := len(s) |
| 639 | if l == 0 { |
| 640 | return ErrNil |
| 641 | } |
| 642 | o.buf = append(o.buf, p.tagcode...) |
| 643 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 644 | for _, x := range s { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 645 | v := uint64(0) |
| 646 | if x { |
| 647 | v = 1 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 648 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 649 | p.valEnc(o, v) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 650 | } |
| 651 | return nil |
| 652 | } |
| 653 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 654 | func size_slice_packed_bool(p *Properties, base structPointer) (n int) { |
| 655 | s := *structPointer_BoolSlice(base, p.field) |
| 656 | l := len(s) |
| 657 | if l == 0 { |
| 658 | return 0 |
| 659 | } |
| 660 | n += len(p.tagcode) |
| 661 | n += sizeVarint(uint64(l)) |
| 662 | n += l // each bool takes exactly one byte |
| 663 | return |
| 664 | } |
| 665 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 666 | // Encode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 667 | func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { |
| 668 | s := *structPointer_Bytes(base, p.field) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 669 | if s == nil { |
| 670 | return ErrNil |
| 671 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 672 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 673 | o.EncodeRawBytes(s) |
| 674 | return nil |
| 675 | } |
| 676 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 677 | func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error { |
| 678 | s := *structPointer_Bytes(base, p.field) |
| 679 | if len(s) == 0 { |
| 680 | return ErrNil |
| 681 | } |
| 682 | o.buf = append(o.buf, p.tagcode...) |
| 683 | o.EncodeRawBytes(s) |
| 684 | return nil |
| 685 | } |
| 686 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 687 | func size_slice_byte(p *Properties, base structPointer) (n int) { |
| 688 | s := *structPointer_Bytes(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 689 | if s == nil && !p.oneof { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 690 | return 0 |
| 691 | } |
| 692 | n += len(p.tagcode) |
| 693 | n += sizeRawBytes(s) |
| 694 | return |
| 695 | } |
| 696 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 697 | func size_proto3_slice_byte(p *Properties, base structPointer) (n int) { |
| 698 | s := *structPointer_Bytes(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 699 | if len(s) == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 700 | return 0 |
| 701 | } |
| 702 | n += len(p.tagcode) |
| 703 | n += sizeRawBytes(s) |
| 704 | return |
| 705 | } |
| 706 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 707 | // Encode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 708 | func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { |
| 709 | s := structPointer_Word32Slice(base, p.field) |
| 710 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 711 | if l == 0 { |
| 712 | return ErrNil |
| 713 | } |
| 714 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 715 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 716 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 717 | p.valEnc(o, uint64(x)) |
| 718 | } |
| 719 | return nil |
| 720 | } |
| 721 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 722 | func size_slice_int32(p *Properties, base structPointer) (n int) { |
| 723 | s := structPointer_Word32Slice(base, p.field) |
| 724 | l := s.Len() |
| 725 | if l == 0 { |
| 726 | return 0 |
| 727 | } |
| 728 | for i := 0; i < l; i++ { |
| 729 | n += len(p.tagcode) |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 730 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 731 | n += p.valSize(uint64(x)) |
| 732 | } |
| 733 | return |
| 734 | } |
| 735 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 736 | // Encode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 737 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { |
| 738 | s := structPointer_Word32Slice(base, p.field) |
| 739 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 740 | if l == 0 { |
| 741 | return ErrNil |
| 742 | } |
| 743 | // TODO: Reuse a Buffer. |
| 744 | buf := NewBuffer(nil) |
| 745 | for i := 0; i < l; i++ { |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 746 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| 747 | p.valEnc(buf, uint64(x)) |
| 748 | } |
| 749 | |
| 750 | o.buf = append(o.buf, p.tagcode...) |
| 751 | o.EncodeVarint(uint64(len(buf.buf))) |
| 752 | o.buf = append(o.buf, buf.buf...) |
| 753 | return nil |
| 754 | } |
| 755 | |
| 756 | func size_slice_packed_int32(p *Properties, base structPointer) (n int) { |
| 757 | s := structPointer_Word32Slice(base, p.field) |
| 758 | l := s.Len() |
| 759 | if l == 0 { |
| 760 | return 0 |
| 761 | } |
| 762 | var bufSize int |
| 763 | for i := 0; i < l; i++ { |
| 764 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| 765 | bufSize += p.valSize(uint64(x)) |
| 766 | } |
| 767 | |
| 768 | n += len(p.tagcode) |
| 769 | n += sizeVarint(uint64(bufSize)) |
| 770 | n += bufSize |
| 771 | return |
| 772 | } |
| 773 | |
| 774 | // Encode a slice of uint32s ([]uint32). |
| 775 | // Exactly the same as int32, except for no sign extension. |
| 776 | func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error { |
| 777 | s := structPointer_Word32Slice(base, p.field) |
| 778 | l := s.Len() |
| 779 | if l == 0 { |
| 780 | return ErrNil |
| 781 | } |
| 782 | for i := 0; i < l; i++ { |
| 783 | o.buf = append(o.buf, p.tagcode...) |
| 784 | x := s.Index(i) |
| 785 | p.valEnc(o, uint64(x)) |
| 786 | } |
| 787 | return nil |
| 788 | } |
| 789 | |
| 790 | func size_slice_uint32(p *Properties, base structPointer) (n int) { |
| 791 | s := structPointer_Word32Slice(base, p.field) |
| 792 | l := s.Len() |
| 793 | if l == 0 { |
| 794 | return 0 |
| 795 | } |
| 796 | for i := 0; i < l; i++ { |
| 797 | n += len(p.tagcode) |
| 798 | x := s.Index(i) |
| 799 | n += p.valSize(uint64(x)) |
| 800 | } |
| 801 | return |
| 802 | } |
| 803 | |
| 804 | // Encode a slice of uint32s ([]uint32) in packed format. |
| 805 | // Exactly the same as int32, except for no sign extension. |
| 806 | func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error { |
| 807 | s := structPointer_Word32Slice(base, p.field) |
| 808 | l := s.Len() |
| 809 | if l == 0 { |
| 810 | return ErrNil |
| 811 | } |
| 812 | // TODO: Reuse a Buffer. |
| 813 | buf := NewBuffer(nil) |
| 814 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 815 | p.valEnc(buf, uint64(s.Index(i))) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | o.buf = append(o.buf, p.tagcode...) |
| 819 | o.EncodeVarint(uint64(len(buf.buf))) |
| 820 | o.buf = append(o.buf, buf.buf...) |
| 821 | return nil |
| 822 | } |
| 823 | |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 824 | func size_slice_packed_uint32(p *Properties, base structPointer) (n int) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 825 | s := structPointer_Word32Slice(base, p.field) |
| 826 | l := s.Len() |
| 827 | if l == 0 { |
| 828 | return 0 |
| 829 | } |
| 830 | var bufSize int |
| 831 | for i := 0; i < l; i++ { |
| 832 | bufSize += p.valSize(uint64(s.Index(i))) |
| 833 | } |
| 834 | |
| 835 | n += len(p.tagcode) |
| 836 | n += sizeVarint(uint64(bufSize)) |
| 837 | n += bufSize |
| 838 | return |
| 839 | } |
| 840 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 841 | // Encode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 842 | func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { |
| 843 | s := structPointer_Word64Slice(base, p.field) |
| 844 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 845 | if l == 0 { |
| 846 | return ErrNil |
| 847 | } |
| 848 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 849 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 850 | p.valEnc(o, s.Index(i)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 851 | } |
| 852 | return nil |
| 853 | } |
| 854 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 855 | func size_slice_int64(p *Properties, base structPointer) (n int) { |
| 856 | s := structPointer_Word64Slice(base, p.field) |
| 857 | l := s.Len() |
| 858 | if l == 0 { |
| 859 | return 0 |
| 860 | } |
| 861 | for i := 0; i < l; i++ { |
| 862 | n += len(p.tagcode) |
| 863 | n += p.valSize(s.Index(i)) |
| 864 | } |
| 865 | return |
| 866 | } |
| 867 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 868 | // Encode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 869 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { |
| 870 | s := structPointer_Word64Slice(base, p.field) |
| 871 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 872 | if l == 0 { |
| 873 | return ErrNil |
| 874 | } |
| 875 | // TODO: Reuse a Buffer. |
| 876 | buf := NewBuffer(nil) |
| 877 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 878 | p.valEnc(buf, s.Index(i)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | o.buf = append(o.buf, p.tagcode...) |
| 882 | o.EncodeVarint(uint64(len(buf.buf))) |
| 883 | o.buf = append(o.buf, buf.buf...) |
| 884 | return nil |
| 885 | } |
| 886 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 887 | func size_slice_packed_int64(p *Properties, base structPointer) (n int) { |
| 888 | s := structPointer_Word64Slice(base, p.field) |
| 889 | l := s.Len() |
| 890 | if l == 0 { |
| 891 | return 0 |
| 892 | } |
| 893 | var bufSize int |
| 894 | for i := 0; i < l; i++ { |
| 895 | bufSize += p.valSize(s.Index(i)) |
| 896 | } |
| 897 | |
| 898 | n += len(p.tagcode) |
| 899 | n += sizeVarint(uint64(bufSize)) |
| 900 | n += bufSize |
| 901 | return |
| 902 | } |
| 903 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 904 | // Encode a slice of slice of bytes ([][]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 905 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { |
| 906 | ss := *structPointer_BytesSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 907 | l := len(ss) |
| 908 | if l == 0 { |
| 909 | return ErrNil |
| 910 | } |
| 911 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 912 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 913 | o.EncodeRawBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 914 | } |
| 915 | return nil |
| 916 | } |
| 917 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 918 | func size_slice_slice_byte(p *Properties, base structPointer) (n int) { |
| 919 | ss := *structPointer_BytesSlice(base, p.field) |
| 920 | l := len(ss) |
| 921 | if l == 0 { |
| 922 | return 0 |
| 923 | } |
| 924 | n += l * len(p.tagcode) |
| 925 | for i := 0; i < l; i++ { |
| 926 | n += sizeRawBytes(ss[i]) |
| 927 | } |
| 928 | return |
| 929 | } |
| 930 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 931 | // Encode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 932 | func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { |
| 933 | ss := *structPointer_StringSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 934 | l := len(ss) |
| 935 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 936 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 937 | o.EncodeStringBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 938 | } |
| 939 | return nil |
| 940 | } |
| 941 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 942 | func size_slice_string(p *Properties, base structPointer) (n int) { |
| 943 | ss := *structPointer_StringSlice(base, p.field) |
| 944 | l := len(ss) |
| 945 | n += l * len(p.tagcode) |
| 946 | for i := 0; i < l; i++ { |
| 947 | n += sizeStringBytes(ss[i]) |
| 948 | } |
| 949 | return |
| 950 | } |
| 951 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 952 | // Encode a slice of message structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 953 | func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 954 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 955 | s := structPointer_StructPointerSlice(base, p.field) |
| 956 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 957 | |
| 958 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 959 | structp := s.Index(i) |
| 960 | if structPointer_IsNil(structp) { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 961 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 965 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 966 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 967 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 968 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 969 | return err |
| 970 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 971 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 972 | o.EncodeRawBytes(data) |
| 973 | continue |
| 974 | } |
| 975 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 976 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 977 | err := o.enc_len_struct(p.sprop, structp, &state) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 978 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 979 | if err == ErrNil { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 980 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 981 | } |
| 982 | return err |
| 983 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 984 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 985 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 988 | func size_slice_struct_message(p *Properties, base structPointer) (n int) { |
| 989 | s := structPointer_StructPointerSlice(base, p.field) |
| 990 | l := s.Len() |
| 991 | n += l * len(p.tagcode) |
| 992 | for i := 0; i < l; i++ { |
| 993 | structp := s.Index(i) |
| 994 | if structPointer_IsNil(structp) { |
| 995 | return // return the size up to this point |
| 996 | } |
| 997 | |
| 998 | // Can the object marshal itself? |
| 999 | if p.isMarshaler { |
| 1000 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 1001 | data, _ := m.Marshal() |
| 1002 | n += len(p.tagcode) |
| 1003 | n += sizeRawBytes(data) |
| 1004 | continue |
| 1005 | } |
| 1006 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1007 | n0 := size_struct(p.sprop, structp) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1008 | n1 := sizeVarint(uint64(n0)) // size of encoded length |
| 1009 | n += n0 + n1 |
| 1010 | } |
| 1011 | return |
| 1012 | } |
| 1013 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1014 | // Encode a slice of group structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1015 | func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1016 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1017 | s := structPointer_StructPointerSlice(base, p.field) |
| 1018 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1019 | |
| 1020 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1021 | b := s.Index(i) |
| 1022 | if structPointer_IsNil(b) { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1023 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 1027 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1028 | err := o.enc_struct(p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1029 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1030 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1031 | if err == ErrNil { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1032 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1033 | } |
| 1034 | return err |
| 1035 | } |
| 1036 | |
| 1037 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 1038 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1039 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1042 | func size_slice_struct_group(p *Properties, base structPointer) (n int) { |
| 1043 | s := structPointer_StructPointerSlice(base, p.field) |
| 1044 | l := s.Len() |
| 1045 | |
| 1046 | n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup)) |
| 1047 | n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup)) |
| 1048 | for i := 0; i < l; i++ { |
| 1049 | b := s.Index(i) |
| 1050 | if structPointer_IsNil(b) { |
| 1051 | return // return size up to this point |
| 1052 | } |
| 1053 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1054 | n += size_struct(p.sprop, b) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1055 | } |
| 1056 | return |
| 1057 | } |
| 1058 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1059 | // Encode an extension map. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1060 | func (o *Buffer) enc_map(p *Properties, base structPointer) error { |
| 1061 | v := *structPointer_ExtMap(base, p.field) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1062 | if err := encodeExtensionMap(v); err != nil { |
| 1063 | return err |
| 1064 | } |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1065 | // Fast-path for common cases: zero or one extensions. |
| 1066 | if len(v) <= 1 { |
| 1067 | for _, e := range v { |
| 1068 | o.buf = append(o.buf, e.enc...) |
| 1069 | } |
| 1070 | return nil |
| 1071 | } |
| 1072 | |
| 1073 | // Sort keys to provide a deterministic encoding. |
| 1074 | keys := make([]int, 0, len(v)) |
| 1075 | for k := range v { |
| 1076 | keys = append(keys, int(k)) |
| 1077 | } |
| 1078 | sort.Ints(keys) |
| 1079 | |
| 1080 | for _, k := range keys { |
| 1081 | o.buf = append(o.buf, v[int32(k)].enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1082 | } |
| 1083 | return nil |
| 1084 | } |
| 1085 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1086 | func size_map(p *Properties, base structPointer) int { |
| 1087 | v := *structPointer_ExtMap(base, p.field) |
| 1088 | return sizeExtensionMap(v) |
| 1089 | } |
| 1090 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1091 | // Encode a map field. |
| 1092 | func (o *Buffer) enc_new_map(p *Properties, base structPointer) error { |
| 1093 | var state errorState // XXX: or do we need to plumb this through? |
| 1094 | |
| 1095 | /* |
| 1096 | A map defined as |
| 1097 | map<key_type, value_type> map_field = N; |
| 1098 | is encoded in the same way as |
| 1099 | message MapFieldEntry { |
| 1100 | key_type key = 1; |
| 1101 | value_type value = 2; |
| 1102 | } |
| 1103 | repeated MapFieldEntry map_field = N; |
| 1104 | */ |
| 1105 | |
| David Symonds | 0f7a9ca | 2015-07-20 16:00:00 +1000 | [diff] [blame] | 1106 | v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1107 | if v.Len() == 0 { |
| 1108 | return nil |
| 1109 | } |
| 1110 | |
| 1111 | keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) |
| 1112 | |
| 1113 | enc := func() error { |
| 1114 | if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil { |
| 1115 | return err |
| 1116 | } |
| 1117 | if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil { |
| 1118 | return err |
| 1119 | } |
| 1120 | return nil |
| 1121 | } |
| 1122 | |
| David Symonds | efcaa34 | 2015-10-28 16:14:47 +1100 | [diff] [blame] | 1123 | // Don't sort map keys. It is not required by the spec, and C++ doesn't do it. |
| 1124 | for _, key := range v.MapKeys() { |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1125 | val := v.MapIndex(key) |
| 1126 | |
| David Symonds | cab84a3 | 2015-05-19 09:29:00 +1000 | [diff] [blame] | 1127 | // The only illegal map entry values are nil message pointers. |
| 1128 | if val.Kind() == reflect.Ptr && val.IsNil() { |
| 1129 | return errors.New("proto: map has nil element") |
| 1130 | } |
| 1131 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1132 | keycopy.Set(key) |
| 1133 | valcopy.Set(val) |
| 1134 | |
| 1135 | o.buf = append(o.buf, p.tagcode...) |
| 1136 | if err := o.enc_len_thing(enc, &state); err != nil { |
| 1137 | return err |
| 1138 | } |
| 1139 | } |
| 1140 | return nil |
| 1141 | } |
| 1142 | |
| 1143 | func size_new_map(p *Properties, base structPointer) int { |
| David Symonds | 0f7a9ca | 2015-07-20 16:00:00 +1000 | [diff] [blame] | 1144 | v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1145 | |
| 1146 | keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) |
| 1147 | |
| 1148 | n := 0 |
| 1149 | for _, key := range v.MapKeys() { |
| 1150 | val := v.MapIndex(key) |
| 1151 | keycopy.Set(key) |
| 1152 | valcopy.Set(val) |
| 1153 | |
| David Symonds | a8323e2 | 2015-03-24 10:18:00 +1100 | [diff] [blame] | 1154 | // Tag codes for key and val are the responsibility of the sub-sizer. |
| 1155 | keysize := p.mkeyprop.size(p.mkeyprop, keybase) |
| 1156 | valsize := p.mvalprop.size(p.mvalprop, valbase) |
| 1157 | entry := keysize + valsize |
| 1158 | // Add on tag code and length of map entry itself. |
| 1159 | n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1160 | } |
| 1161 | return n |
| 1162 | } |
| 1163 | |
| 1164 | // mapEncodeScratch returns a new reflect.Value matching the map's value type, |
| 1165 | // and a structPointer suitable for passing to an encoder or sizer. |
| 1166 | func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) { |
| 1167 | // Prepare addressable doubly-indirect placeholders for the key and value types. |
| 1168 | // This is needed because the element-type encoders expect **T, but the map iteration produces T. |
| 1169 | |
| 1170 | keycopy = reflect.New(mapType.Key()).Elem() // addressable K |
| 1171 | keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K |
| 1172 | keyptr.Set(keycopy.Addr()) // |
| 1173 | keybase = toStructPointer(keyptr.Addr()) // **K |
| 1174 | |
| 1175 | // Value types are more varied and require special handling. |
| 1176 | switch mapType.Elem().Kind() { |
| 1177 | case reflect.Slice: |
| 1178 | // []byte |
| 1179 | var dummy []byte |
| 1180 | valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte |
| 1181 | valbase = toStructPointer(valcopy.Addr()) |
| 1182 | case reflect.Ptr: |
| 1183 | // message; the generated field type is map[K]*Msg (so V is *Msg), |
| 1184 | // so we only need one level of indirection. |
| 1185 | valcopy = reflect.New(mapType.Elem()).Elem() // addressable V |
| 1186 | valbase = toStructPointer(valcopy.Addr()) |
| 1187 | default: |
| 1188 | // everything else |
| 1189 | valcopy = reflect.New(mapType.Elem()).Elem() // addressable V |
| 1190 | valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V |
| 1191 | valptr.Set(valcopy.Addr()) // |
| 1192 | valbase = toStructPointer(valptr.Addr()) // **V |
| 1193 | } |
| 1194 | return |
| 1195 | } |
| 1196 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1197 | // Encode a struct. |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1198 | func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1199 | var state errorState |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 1200 | // Encode fields in tag order so that decoders may use optimizations |
| 1201 | // that depend on the ordering. |
| David Symonds | 380d2d0 | 2014-11-24 10:50:43 +1100 | [diff] [blame] | 1202 | // https://developers.google.com/protocol-buffers/docs/encoding#order |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 1203 | for _, i := range prop.order { |
| 1204 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1205 | if p.enc != nil { |
| 1206 | err := p.enc(o, p, base) |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1207 | if err != nil { |
| 1208 | if err == ErrNil { |
| 1209 | if p.Required && state.err == nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1210 | state.err = &RequiredNotSetError{p.Name} |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1211 | } |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1212 | } else if err == errRepeatedHasNil { |
| 1213 | // Give more context to nil values in repeated fields. |
| 1214 | return errors.New("repeated field " + p.OrigName + " has nil element") |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1215 | } else if !state.shouldContinue(err, p) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1216 | return err |
| 1217 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1221 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1222 | // Do oneof fields. |
| 1223 | if prop.oneofMarshaler != nil { |
| 1224 | m := structPointer_Interface(base, prop.stype).(Message) |
| 1225 | if err := prop.oneofMarshaler(m, o); err != nil { |
| 1226 | return err |
| 1227 | } |
| 1228 | } |
| 1229 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1230 | // Add unrecognized fields at the end. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1231 | if prop.unrecField.IsValid() { |
| 1232 | v := *structPointer_Bytes(base, prop.unrecField) |
| 1233 | if len(v) > 0 { |
| 1234 | o.buf = append(o.buf, v...) |
| 1235 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1236 | } |
| 1237 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1238 | return state.err |
| 1239 | } |
| 1240 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1241 | func size_struct(prop *StructProperties, base structPointer) (n int) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1242 | for _, i := range prop.order { |
| 1243 | p := prop.Prop[i] |
| 1244 | if p.size != nil { |
| 1245 | n += p.size(p, base) |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | // Add unrecognized fields at the end. |
| 1250 | if prop.unrecField.IsValid() { |
| 1251 | v := *structPointer_Bytes(base, prop.unrecField) |
| 1252 | n += len(v) |
| 1253 | } |
| 1254 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1255 | // Factor in any oneof fields. |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 1256 | if prop.oneofSizer != nil { |
| 1257 | m := structPointer_Interface(base, prop.stype).(Message) |
| 1258 | n += prop.oneofSizer(m) |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1259 | } |
| 1260 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1261 | return |
| 1262 | } |
| 1263 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1264 | var zeroes [20]byte // longer than any conceivable sizeVarint |
| 1265 | |
| 1266 | // Encode a struct, preceded by its encoded length (as a varint). |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1267 | func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error { |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1268 | return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state) |
| 1269 | } |
| 1270 | |
| 1271 | // Encode something, preceded by its encoded length (as a varint). |
| 1272 | func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error { |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1273 | iLen := len(o.buf) |
| 1274 | o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length |
| 1275 | iMsg := len(o.buf) |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1276 | err := enc() |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1277 | if err != nil && !state.shouldContinue(err, nil) { |
| 1278 | return err |
| 1279 | } |
| 1280 | lMsg := len(o.buf) - iMsg |
| 1281 | lLen := sizeVarint(uint64(lMsg)) |
| 1282 | switch x := lLen - (iMsg - iLen); { |
| 1283 | case x > 0: // actual length is x bytes larger than the space we reserved |
| 1284 | // Move msg x bytes right. |
| 1285 | o.buf = append(o.buf, zeroes[:x]...) |
| 1286 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 1287 | case x < 0: // actual length is x bytes smaller than the space we reserved |
| 1288 | // Move msg x bytes left. |
| 1289 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 1290 | o.buf = o.buf[:len(o.buf)+x] // x is negative |
| 1291 | } |
| 1292 | // Encode the length in the reserved space. |
| 1293 | o.buf = o.buf[:iLen] |
| 1294 | o.EncodeVarint(uint64(lMsg)) |
| 1295 | o.buf = o.buf[:len(o.buf)+lMsg] |
| 1296 | return state.err |
| 1297 | } |
| 1298 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1299 | // errorState maintains the first error that occurs and updates that error |
| 1300 | // with additional context. |
| 1301 | type errorState struct { |
| 1302 | err error |
| 1303 | } |
| 1304 | |
| 1305 | // shouldContinue reports whether encoding should continue upon encountering the |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1306 | // given error. If the error is RequiredNotSetError, shouldContinue returns true |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1307 | // and, if this is the first appearance of that error, remembers it for future |
| 1308 | // reporting. |
| 1309 | // |
| 1310 | // If prop is not nil, it may update any error with additional context about the |
| 1311 | // field with the error. |
| 1312 | func (s *errorState) shouldContinue(err error, prop *Properties) bool { |
| 1313 | // Ignore unset required fields. |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1314 | reqNotSet, ok := err.(*RequiredNotSetError) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1315 | if !ok { |
| 1316 | return false |
| 1317 | } |
| 1318 | if s.err == nil { |
| 1319 | if prop != nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1320 | err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field} |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1321 | } |
| 1322 | s.err = err |
| 1323 | } |
| 1324 | return true |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1325 | } |