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