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