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