| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto |
| 33 | |
| 34 | /* |
| 35 | * Routines for encoding data into the wire format for protocol buffers. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 39 | "errors" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | "reflect" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | // ErrRequiredNotSet is the error returned if Marshal is called with |
| 44 | // a protocol buffer struct whose required fields have not |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 45 | // all been initialized. It is also the error returned if Unmarshal is |
| 46 | // called with an encoded protocol buffer that does not include all the |
| 47 | // required fields. |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 48 | type ErrRequiredNotSet struct { |
| 49 | t reflect.Type |
| 50 | } |
| 51 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 52 | func (e *ErrRequiredNotSet) Error() string { |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 53 | return "proto: required fields not set in " + e.t.String() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 54 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 55 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 56 | var ( |
| 57 | // ErrRepeatedHasNil is the error returned if Marshal is called with |
| 58 | // a struct with a repeated field containing a nil element. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 59 | ErrRepeatedHasNil = errors.New("proto: repeated field has nil") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 60 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 61 | // ErrNil is the error returned if Marshal is called with nil. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 62 | ErrNil = errors.New("proto: Marshal called with nil") |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 63 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 64 | |
| 65 | // The fundamental encoders that put bytes on the wire. |
| 66 | // Those that take integer types all accept uint64 and are |
| 67 | // therefore of type valueEncoder. |
| 68 | |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 69 | const maxVarintBytes = 10 // maximum length of a varint |
| 70 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 71 | // EncodeVarint returns the varint encoding of x. |
| 72 | // This is the format for the |
| 73 | // int32, int64, uint32, uint64, bool, and enum |
| 74 | // protocol buffer types. |
| 75 | // Not used by the package itself, but helpful to clients |
| 76 | // wishing to use the same encoding. |
| 77 | func EncodeVarint(x uint64) []byte { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 78 | var buf [maxVarintBytes]byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 79 | var n int |
| 80 | for n = 0; x > 127; n++ { |
| 81 | buf[n] = 0x80 | uint8(x&0x7F) |
| 82 | x >>= 7 |
| 83 | } |
| 84 | buf[n] = uint8(x) |
| 85 | n++ |
| 86 | return buf[0:n] |
| 87 | } |
| 88 | |
| 89 | // EncodeVarint writes a varint-encoded integer to the Buffer. |
| 90 | // This is the format for the |
| 91 | // int32, int64, uint32, uint64, bool, and enum |
| 92 | // protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 93 | func (p *Buffer) EncodeVarint(x uint64) error { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 94 | for x >= 1<<7 { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 95 | p.buf = append(p.buf, uint8(x&0x7f|0x80)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 96 | x >>= 7 |
| 97 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 98 | p.buf = append(p.buf, uint8(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // EncodeFixed64 writes a 64-bit integer to the Buffer. |
| 103 | // This is the format for the |
| 104 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 105 | func (p *Buffer) EncodeFixed64(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 106 | p.buf = append(p.buf, |
| 107 | uint8(x), |
| 108 | uint8(x>>8), |
| 109 | uint8(x>>16), |
| 110 | uint8(x>>24), |
| 111 | uint8(x>>32), |
| 112 | uint8(x>>40), |
| 113 | uint8(x>>48), |
| 114 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // EncodeFixed32 writes a 32-bit integer to the Buffer. |
| 119 | // This is the format for the |
| 120 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 121 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 122 | p.buf = append(p.buf, |
| 123 | uint8(x), |
| 124 | uint8(x>>8), |
| 125 | uint8(x>>16), |
| 126 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 131 | // to the Buffer. |
| 132 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 133 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 134 | // use signed number to get arithmetic right shift. |
| 135 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 136 | } |
| 137 | |
| 138 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 139 | // to the Buffer. |
| 140 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 141 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 142 | // use signed number to get arithmetic right shift. |
| 143 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 144 | } |
| 145 | |
| 146 | // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. |
| 147 | // This is the format used for the bytes protocol buffer |
| 148 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 149 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 150 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 151 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 152 | return nil |
| 153 | } |
| 154 | |
| 155 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 156 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 157 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 158 | p.EncodeVarint(uint64(len(s))) |
| 159 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 160 | return nil |
| 161 | } |
| 162 | |
| 163 | // Marshaler is the interface representing objects that can marshal themselves. |
| 164 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 165 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 168 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 169 | // and encodes it into the wire format, returning the data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 170 | func Marshal(pb Message) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 171 | // Can the object marshal itself? |
| 172 | if m, ok := pb.(Marshaler); ok { |
| 173 | return m.Marshal() |
| 174 | } |
| 175 | p := NewBuffer(nil) |
| 176 | err := p.Marshal(pb) |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | return p.buf, err |
| 181 | } |
| 182 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 183 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 184 | // and encodes it into the wire format, writing the result to the |
| 185 | // Buffer. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 186 | func (p *Buffer) Marshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 187 | // Can the object marshal itself? |
| 188 | if m, ok := pb.(Marshaler); ok { |
| 189 | data, err := m.Marshal() |
| 190 | if err != nil { |
| 191 | return err |
| 192 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 193 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 194 | return nil |
| 195 | } |
| 196 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 197 | t, base, err := getbase(pb) |
| 198 | if structPointer_IsNil(base) { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 199 | return ErrNil |
| 200 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 201 | if err == nil { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 202 | err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 205 | if collectStats { |
| 206 | stats.Encode++ |
| 207 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 208 | |
| 209 | return err |
| 210 | } |
| 211 | |
| 212 | // Individual type encoders. |
| 213 | |
| 214 | // Encode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 215 | func (o *Buffer) enc_bool(p *Properties, base structPointer) error { |
| 216 | v := *structPointer_Bool(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 217 | if v == nil { |
| 218 | return ErrNil |
| 219 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 220 | x := 0 |
| 221 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 222 | x = 1 |
| 223 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 224 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 225 | p.valEnc(o, uint64(x)) |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | // Encode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 230 | func (o *Buffer) enc_int32(p *Properties, base structPointer) error { |
| 231 | v := structPointer_Word32(base, p.field) |
| 232 | if word32_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 233 | return ErrNil |
| 234 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 235 | x := word32_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 236 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 237 | p.valEnc(o, uint64(int32(x))) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 238 | return nil |
| 239 | } |
| 240 | |
| 241 | // Encode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 242 | func (o *Buffer) enc_int64(p *Properties, base structPointer) error { |
| 243 | v := structPointer_Word64(base, p.field) |
| 244 | if word64_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | return ErrNil |
| 246 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 247 | x := word64_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 248 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 249 | p.valEnc(o, x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 250 | return nil |
| 251 | } |
| 252 | |
| 253 | // Encode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 254 | func (o *Buffer) enc_string(p *Properties, base structPointer) error { |
| 255 | v := *structPointer_String(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 256 | if v == nil { |
| 257 | return ErrNil |
| 258 | } |
| 259 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 260 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 261 | o.EncodeStringBytes(x) |
| 262 | return nil |
| 263 | } |
| 264 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 265 | // All protocol buffer fields are nillable, but be careful. |
| 266 | func isNil(v reflect.Value) bool { |
| 267 | switch v.Kind() { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 268 | case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 269 | return v.IsNil() |
| 270 | } |
| 271 | return false |
| 272 | } |
| 273 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 274 | // Encode a message struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 275 | func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { |
| 276 | structp := structPointer_GetStructPointer(base, p.field) |
| 277 | if structPointer_IsNil(structp) { |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 278 | return ErrNil |
| 279 | } |
| 280 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 281 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 282 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 283 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 284 | data, err := m.Marshal() |
| 285 | if err != nil { |
| 286 | return err |
| 287 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 288 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 289 | o.EncodeRawBytes(data) |
| 290 | return nil |
| 291 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 292 | |
| 293 | // need the length before we can write out the message itself, |
| 294 | // so marshal into a separate byte buffer first. |
| 295 | obuf := o.buf |
| 296 | o.buf = o.bufalloc() |
| 297 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 298 | err := o.enc_struct(p.stype, p.sprop, structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 299 | |
| 300 | nbuf := o.buf |
| 301 | o.buf = obuf |
| 302 | if err != nil { |
| 303 | o.buffree(nbuf) |
| 304 | return err |
| 305 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 306 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 307 | o.EncodeRawBytes(nbuf) |
| 308 | o.buffree(nbuf) |
| 309 | return nil |
| 310 | } |
| 311 | |
| 312 | // Encode a group struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 313 | func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { |
| 314 | b := structPointer_GetStructPointer(base, p.field) |
| 315 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 316 | return ErrNil |
| 317 | } |
| 318 | |
| 319 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 320 | err := o.enc_struct(p.stype, p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 321 | if err != nil { |
| 322 | return err |
| 323 | } |
| 324 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 325 | return nil |
| 326 | } |
| 327 | |
| 328 | // Encode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 329 | func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { |
| 330 | s := *structPointer_BoolSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 331 | l := len(s) |
| 332 | if l == 0 { |
| 333 | return ErrNil |
| 334 | } |
| 335 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 336 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 337 | v := uint64(0) |
| 338 | if x { |
| 339 | v = 1 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 340 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 341 | p.valEnc(o, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 342 | } |
| 343 | return nil |
| 344 | } |
| 345 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 346 | // Encode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 347 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { |
| 348 | s := *structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 349 | l := len(s) |
| 350 | if l == 0 { |
| 351 | return ErrNil |
| 352 | } |
| 353 | o.buf = append(o.buf, p.tagcode...) |
| 354 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 355 | for _, x := range s { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 356 | v := uint64(0) |
| 357 | if x { |
| 358 | v = 1 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 359 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 360 | p.valEnc(o, v) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 361 | } |
| 362 | return nil |
| 363 | } |
| 364 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 365 | // Encode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 366 | func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { |
| 367 | s := *structPointer_Bytes(base, p.field) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 368 | if s == nil { |
| 369 | return ErrNil |
| 370 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 371 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 372 | o.EncodeRawBytes(s) |
| 373 | return nil |
| 374 | } |
| 375 | |
| 376 | // Encode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 377 | func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { |
| 378 | s := structPointer_Word32Slice(base, p.field) |
| 379 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 380 | if l == 0 { |
| 381 | return ErrNil |
| 382 | } |
| 383 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 384 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 385 | x := s.Index(i) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 386 | p.valEnc(o, uint64(x)) |
| 387 | } |
| 388 | return nil |
| 389 | } |
| 390 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 391 | // Encode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 392 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { |
| 393 | s := structPointer_Word32Slice(base, p.field) |
| 394 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 395 | if l == 0 { |
| 396 | return ErrNil |
| 397 | } |
| 398 | // TODO: Reuse a Buffer. |
| 399 | buf := NewBuffer(nil) |
| 400 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 401 | p.valEnc(buf, uint64(s.Index(i))) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | o.buf = append(o.buf, p.tagcode...) |
| 405 | o.EncodeVarint(uint64(len(buf.buf))) |
| 406 | o.buf = append(o.buf, buf.buf...) |
| 407 | return nil |
| 408 | } |
| 409 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 410 | // Encode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 411 | func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { |
| 412 | s := structPointer_Word64Slice(base, p.field) |
| 413 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 414 | if l == 0 { |
| 415 | return ErrNil |
| 416 | } |
| 417 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 418 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 419 | p.valEnc(o, s.Index(i)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 420 | } |
| 421 | return nil |
| 422 | } |
| 423 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 424 | // Encode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 425 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { |
| 426 | s := structPointer_Word64Slice(base, p.field) |
| 427 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 428 | if l == 0 { |
| 429 | return ErrNil |
| 430 | } |
| 431 | // TODO: Reuse a Buffer. |
| 432 | buf := NewBuffer(nil) |
| 433 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 434 | p.valEnc(buf, s.Index(i)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | o.buf = append(o.buf, p.tagcode...) |
| 438 | o.EncodeVarint(uint64(len(buf.buf))) |
| 439 | o.buf = append(o.buf, buf.buf...) |
| 440 | return nil |
| 441 | } |
| 442 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 443 | // Encode a slice of slice of bytes ([][]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 444 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { |
| 445 | ss := *structPointer_BytesSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 446 | l := len(ss) |
| 447 | if l == 0 { |
| 448 | return ErrNil |
| 449 | } |
| 450 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 451 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 452 | s := ss[i] |
| 453 | o.EncodeRawBytes(s) |
| 454 | } |
| 455 | return nil |
| 456 | } |
| 457 | |
| 458 | // Encode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 459 | func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { |
| 460 | ss := *structPointer_StringSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 461 | l := len(ss) |
| 462 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 463 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 464 | s := ss[i] |
| 465 | o.EncodeStringBytes(s) |
| 466 | } |
| 467 | return nil |
| 468 | } |
| 469 | |
| 470 | // Encode a slice of message structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 471 | func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { |
| 472 | s := structPointer_StructPointerSlice(base, p.field) |
| 473 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 474 | |
| 475 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 476 | structp := s.Index(i) |
| 477 | if structPointer_IsNil(structp) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 478 | return ErrRepeatedHasNil |
| 479 | } |
| 480 | |
| 481 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 482 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 483 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 484 | data, err := m.Marshal() |
| 485 | if err != nil { |
| 486 | return err |
| 487 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 488 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 489 | o.EncodeRawBytes(data) |
| 490 | continue |
| 491 | } |
| 492 | |
| 493 | obuf := o.buf |
| 494 | o.buf = o.bufalloc() |
| 495 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 496 | err := o.enc_struct(p.stype, p.sprop, structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | |
| 498 | nbuf := o.buf |
| 499 | o.buf = obuf |
| 500 | if err != nil { |
| 501 | o.buffree(nbuf) |
| 502 | if err == ErrNil { |
| 503 | return ErrRepeatedHasNil |
| 504 | } |
| 505 | return err |
| 506 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 507 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 508 | o.EncodeRawBytes(nbuf) |
| 509 | |
| 510 | o.buffree(nbuf) |
| 511 | } |
| 512 | return nil |
| 513 | } |
| 514 | |
| 515 | // Encode a slice of group structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 516 | func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { |
| 517 | s := structPointer_StructPointerSlice(base, p.field) |
| 518 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 519 | |
| 520 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 521 | b := s.Index(i) |
| 522 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 523 | return ErrRepeatedHasNil |
| 524 | } |
| 525 | |
| 526 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 527 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 528 | err := o.enc_struct(p.stype, p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 529 | |
| 530 | if err != nil { |
| 531 | if err == ErrNil { |
| 532 | return ErrRepeatedHasNil |
| 533 | } |
| 534 | return err |
| 535 | } |
| 536 | |
| 537 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 538 | } |
| 539 | return nil |
| 540 | } |
| 541 | |
| 542 | // Encode an extension map. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 543 | func (o *Buffer) enc_map(p *Properties, base structPointer) error { |
| 544 | v := *structPointer_ExtMap(base, p.field) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 545 | if err := encodeExtensionMap(v); err != nil { |
| 546 | return err |
| 547 | } |
| 548 | for _, e := range v { |
| 549 | o.buf = append(o.buf, e.enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 550 | } |
| 551 | return nil |
| 552 | } |
| 553 | |
| 554 | // Encode a struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 555 | func (o *Buffer) enc_struct(t reflect.Type, prop *StructProperties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 556 | required := prop.reqCount |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 557 | // Encode fields in tag order so that decoders may use optimizations |
| 558 | // that depend on the ordering. |
| 559 | // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order |
| 560 | for _, i := range prop.order { |
| 561 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 562 | if p.enc != nil { |
| 563 | err := p.enc(o, p, base) |
| 564 | if err != nil { |
| 565 | if err != ErrNil { |
| 566 | return err |
| 567 | } |
| 568 | } else if p.Required { |
| 569 | required-- |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | // See if we encoded all required fields. |
| 574 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 575 | return &ErrRequiredNotSet{t} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 578 | // Add unrecognized fields at the end. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 579 | if prop.unrecField.IsValid() { |
| 580 | v := *structPointer_Bytes(base, prop.unrecField) |
| 581 | if len(v) > 0 { |
| 582 | o.buf = append(o.buf, v...) |
| 583 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 584 | } |
| 585 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 586 | return nil |
| 587 | } |