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