| 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 ( |
| 39 | "fmt" |
| 40 | "os" |
| 41 | "reflect" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 42 | "sort" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 43 | "strconv" |
| 44 | "strings" |
| 45 | "sync" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | const debug bool = false |
| 49 | |
| 50 | // Constants that identify the encoding of a value on the wire. |
| 51 | const ( |
| 52 | WireVarint = 0 |
| 53 | WireFixed64 = 1 |
| 54 | WireBytes = 2 |
| 55 | WireStartGroup = 3 |
| 56 | WireEndGroup = 4 |
| 57 | WireFixed32 = 5 |
| 58 | ) |
| 59 | |
| 60 | const startSize = 10 // initial slice/string sizes |
| 61 | |
| 62 | // Encoders are defined in encoder.go |
| 63 | // An encoder outputs the full representation of a field, including its |
| 64 | // tag and encoder type. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 65 | type encoder func(p *Buffer, prop *Properties, base structPointer) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 66 | |
| 67 | // A valueEncoder encodes a single integer in a particular encoding. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 68 | type valueEncoder func(o *Buffer, x uint64) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 69 | |
| 70 | // Decoders are defined in decode.go |
| 71 | // A decoder creates a value from its wire representation. |
| 72 | // Unrecognized subelements are saved in unrec. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 73 | type decoder func(p *Buffer, prop *Properties, base structPointer) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 74 | |
| 75 | // A valueDecoder decodes a single integer in a particular encoding. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 76 | type valueDecoder func(o *Buffer) (x uint64, err error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 77 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 78 | // tagMap is an optimization over map[int]int for typical protocol buffer |
| 79 | // use-cases. Encoded protocol buffers are often in tag order with small tag |
| 80 | // numbers. |
| 81 | type tagMap struct { |
| 82 | fastTags []int |
| 83 | slowTags map[int]int |
| 84 | } |
| 85 | |
| 86 | // tagMapFastLimit is the upper bound on the tag number that will be stored in |
| 87 | // the tagMap slice rather than its map. |
| 88 | const tagMapFastLimit = 1024 |
| 89 | |
| 90 | func (p *tagMap) get(t int) (int, bool) { |
| 91 | if t > 0 && t < tagMapFastLimit { |
| 92 | if t >= len(p.fastTags) { |
| 93 | return 0, false |
| 94 | } |
| 95 | fi := p.fastTags[t] |
| 96 | return fi, fi >= 0 |
| 97 | } |
| 98 | fi, ok := p.slowTags[t] |
| 99 | return fi, ok |
| 100 | } |
| 101 | |
| 102 | func (p *tagMap) put(t int, fi int) { |
| 103 | if t > 0 && t < tagMapFastLimit { |
| 104 | for len(p.fastTags) < t+1 { |
| 105 | p.fastTags = append(p.fastTags, -1) |
| 106 | } |
| 107 | p.fastTags[t] = fi |
| 108 | return |
| 109 | } |
| 110 | if p.slowTags == nil { |
| 111 | p.slowTags = make(map[int]int) |
| 112 | } |
| 113 | p.slowTags[t] = fi |
| 114 | } |
| 115 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 116 | // StructProperties represents properties for all the fields of a struct. |
| 117 | type StructProperties struct { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 118 | Prop []*Properties // properties for each field |
| 119 | reqCount int // required count |
| 120 | tags tagMap // map from proto tag to struct field number |
| 121 | origNames map[string]int // map from original name to struct field number |
| 122 | order []int // list of struct field numbers in tag order |
| 123 | unrecField field // field id of the XXX_unrecognized []byte field |
| 124 | extendable bool // is this an extendable proto |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 127 | // Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec. |
| 128 | // See encoder.go, (*Buffer).enc_struct. |
| 129 | |
| 130 | func (sp *StructProperties) Len() int { return len(sp.order) } |
| 131 | func (sp *StructProperties) Less(i, j int) bool { |
| 132 | return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag |
| 133 | } |
| 134 | func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] } |
| 135 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 136 | // Properties represents the protocol-specific behavior of a single struct field. |
| 137 | type Properties struct { |
| 138 | Name string // name of the field, for error messages |
| 139 | OrigName string // original name before protocol compiler (always set) |
| 140 | Wire string |
| 141 | WireType int |
| 142 | Tag int |
| 143 | Required bool |
| 144 | Optional bool |
| 145 | Repeated bool |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 146 | Packed bool // relevant for repeated primitives only |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 147 | Enum string // set for enum types only |
| 148 | Default string // default value |
| 149 | def_uint64 uint64 |
| 150 | |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 151 | enc encoder |
| 152 | valEnc valueEncoder // set for bool and numeric types only |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 153 | field field |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 154 | tagcode []byte // encoding of EncodeVarint((Tag<<3)|WireType) |
| 155 | tagbuf [8]byte |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 156 | stype reflect.Type // set for struct types only |
| 157 | sprop *StructProperties // set for struct types only |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 158 | isMarshaler bool |
| 159 | isUnmarshaler bool |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 160 | |
| David Symonds | 049646b | 2011-10-21 11:13:45 +1100 | [diff] [blame] | 161 | dec decoder |
| 162 | valDec valueDecoder // set for bool and numeric types only |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 163 | |
| 164 | // If this is a packable field, this will be the decoder for the packed version of the field. |
| 165 | packedDec decoder |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 168 | // String formats the properties in the protobuf struct field tag style. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 169 | func (p *Properties) String() string { |
| 170 | s := p.Wire |
| 171 | s = "," |
| 172 | s += strconv.Itoa(p.Tag) |
| 173 | if p.Required { |
| 174 | s += ",req" |
| 175 | } |
| 176 | if p.Optional { |
| 177 | s += ",opt" |
| 178 | } |
| 179 | if p.Repeated { |
| 180 | s += ",rep" |
| 181 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 182 | if p.Packed { |
| 183 | s += ",packed" |
| 184 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 185 | if p.OrigName != p.Name { |
| 186 | s += ",name=" + p.OrigName |
| 187 | } |
| 188 | if len(p.Enum) > 0 { |
| 189 | s += ",enum=" + p.Enum |
| 190 | } |
| 191 | if len(p.Default) > 0 { |
| 192 | s += ",def=" + p.Default |
| 193 | } |
| 194 | return s |
| 195 | } |
| 196 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 197 | // Parse populates p by parsing a string in the protobuf struct field tag style. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 198 | func (p *Properties) Parse(s string) { |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 199 | // "bytes,49,opt,name=foo,def=hello!" |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 200 | fields := strings.Split(s, ",") // breaks def=, but handled below. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 201 | if len(fields) < 2 { |
| 202 | fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s) |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | p.Wire = fields[0] |
| 207 | switch p.Wire { |
| 208 | case "varint": |
| 209 | p.WireType = WireVarint |
| 210 | p.valEnc = (*Buffer).EncodeVarint |
| 211 | p.valDec = (*Buffer).DecodeVarint |
| 212 | case "fixed32": |
| 213 | p.WireType = WireFixed32 |
| 214 | p.valEnc = (*Buffer).EncodeFixed32 |
| 215 | p.valDec = (*Buffer).DecodeFixed32 |
| 216 | case "fixed64": |
| 217 | p.WireType = WireFixed64 |
| 218 | p.valEnc = (*Buffer).EncodeFixed64 |
| 219 | p.valDec = (*Buffer).DecodeFixed64 |
| 220 | case "zigzag32": |
| 221 | p.WireType = WireVarint |
| 222 | p.valEnc = (*Buffer).EncodeZigzag32 |
| 223 | p.valDec = (*Buffer).DecodeZigzag32 |
| 224 | case "zigzag64": |
| 225 | p.WireType = WireVarint |
| 226 | p.valEnc = (*Buffer).EncodeZigzag64 |
| 227 | p.valDec = (*Buffer).DecodeZigzag64 |
| 228 | case "bytes", "group": |
| 229 | p.WireType = WireBytes |
| 230 | // no numeric converter for non-numeric types |
| 231 | default: |
| 232 | fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s) |
| 233 | return |
| 234 | } |
| 235 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 236 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 237 | p.Tag, err = strconv.Atoi(fields[1]) |
| 238 | if err != nil { |
| 239 | return |
| 240 | } |
| 241 | |
| 242 | for i := 2; i < len(fields); i++ { |
| 243 | f := fields[i] |
| 244 | switch { |
| 245 | case f == "req": |
| 246 | p.Required = true |
| 247 | case f == "opt": |
| 248 | p.Optional = true |
| 249 | case f == "rep": |
| 250 | p.Repeated = true |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 251 | case f == "packed": |
| 252 | p.Packed = true |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 253 | case strings.HasPrefix(f, "name="): |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 254 | p.OrigName = f[5:len(f)] |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 255 | case strings.HasPrefix(f, "enum="): |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 256 | p.Enum = f[5:len(f)] |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 257 | case strings.HasPrefix(f, "def="): |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 258 | p.Default = f[4:len(f)] // rest of string |
| 259 | if i+1 < len(fields) { |
| 260 | // Commas aren't escaped, and def is always last. |
| 261 | p.Default += "," + strings.Join(fields[i+1:len(fields)], ",") |
| 262 | break |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 268 | func logNoSliceEnc(t1, t2 reflect.Type) { |
| 269 | fmt.Fprintf(os.Stderr, "proto: no slice oenc for %T = []%T\n", t1, t2) |
| 270 | } |
| 271 | |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 272 | var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem() |
| 273 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 274 | // Initialize the fields for encoding and decoding. |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 275 | func (p *Properties) setEncAndDec(typ reflect.Type, lockGetProp bool) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 276 | p.enc = nil |
| 277 | p.dec = nil |
| 278 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 279 | switch t1 := typ; t1.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 280 | default: |
| 281 | fmt.Fprintf(os.Stderr, "proto: no coders for %T\n", t1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 282 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 283 | case reflect.Ptr: |
| 284 | switch t2 := t1.Elem(); t2.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 285 | default: |
| 286 | fmt.Fprintf(os.Stderr, "proto: no encoder function for %T -> %T\n", t1, t2) |
| 287 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 288 | case reflect.Bool: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 289 | p.enc = (*Buffer).enc_bool |
| 290 | p.dec = (*Buffer).dec_bool |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 291 | case reflect.Int32, reflect.Uint32: |
| 292 | p.enc = (*Buffer).enc_int32 |
| 293 | p.dec = (*Buffer).dec_int32 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 294 | case reflect.Int64, reflect.Uint64: |
| 295 | p.enc = (*Buffer).enc_int64 |
| 296 | p.dec = (*Buffer).dec_int64 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 297 | case reflect.Float32: |
| 298 | p.enc = (*Buffer).enc_int32 // can just treat them as bits |
| 299 | p.dec = (*Buffer).dec_int32 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 300 | case reflect.Float64: |
| 301 | p.enc = (*Buffer).enc_int64 // can just treat them as bits |
| 302 | p.dec = (*Buffer).dec_int64 |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 303 | case reflect.String: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 304 | p.enc = (*Buffer).enc_string |
| 305 | p.dec = (*Buffer).dec_string |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 306 | case reflect.Struct: |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 307 | p.stype = t1.Elem() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 308 | p.isMarshaler = isMarshaler(t1) |
| 309 | p.isUnmarshaler = isUnmarshaler(t1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 310 | if p.Wire == "bytes" { |
| 311 | p.enc = (*Buffer).enc_struct_message |
| 312 | p.dec = (*Buffer).dec_struct_message |
| 313 | } else { |
| 314 | p.enc = (*Buffer).enc_struct_group |
| 315 | p.dec = (*Buffer).dec_struct_group |
| 316 | } |
| 317 | } |
| 318 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 319 | case reflect.Slice: |
| 320 | switch t2 := t1.Elem(); t2.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 321 | default: |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 322 | logNoSliceEnc(t1, t2) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 323 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 324 | case reflect.Bool: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 325 | if p.Packed { |
| 326 | p.enc = (*Buffer).enc_slice_packed_bool |
| 327 | } else { |
| 328 | p.enc = (*Buffer).enc_slice_bool |
| 329 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 330 | p.dec = (*Buffer).dec_slice_bool |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 331 | p.packedDec = (*Buffer).dec_slice_packed_bool |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 332 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 333 | switch t2.Bits() { |
| 334 | case 32: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 335 | if p.Packed { |
| 336 | p.enc = (*Buffer).enc_slice_packed_int32 |
| 337 | } else { |
| 338 | p.enc = (*Buffer).enc_slice_int32 |
| 339 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 340 | p.dec = (*Buffer).dec_slice_int32 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 341 | p.packedDec = (*Buffer).dec_slice_packed_int32 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 342 | case 64: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 343 | if p.Packed { |
| 344 | p.enc = (*Buffer).enc_slice_packed_int64 |
| 345 | } else { |
| 346 | p.enc = (*Buffer).enc_slice_int64 |
| 347 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 348 | p.dec = (*Buffer).dec_slice_int64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 349 | p.packedDec = (*Buffer).dec_slice_packed_int64 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 350 | case 8: |
| 351 | if t2.Kind() == reflect.Uint8 { |
| 352 | p.enc = (*Buffer).enc_slice_byte |
| 353 | p.dec = (*Buffer).dec_slice_byte |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 354 | } |
| 355 | default: |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 356 | logNoSliceEnc(t1, t2) |
| 357 | break |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 358 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 359 | case reflect.Float32, reflect.Float64: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 360 | switch t2.Bits() { |
| 361 | case 32: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 362 | // can just treat them as bits |
| 363 | if p.Packed { |
| 364 | p.enc = (*Buffer).enc_slice_packed_int32 |
| 365 | } else { |
| 366 | p.enc = (*Buffer).enc_slice_int32 |
| 367 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 368 | p.dec = (*Buffer).dec_slice_int32 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 369 | p.packedDec = (*Buffer).dec_slice_packed_int32 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 370 | case 64: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 371 | // can just treat them as bits |
| 372 | if p.Packed { |
| 373 | p.enc = (*Buffer).enc_slice_packed_int64 |
| 374 | } else { |
| 375 | p.enc = (*Buffer).enc_slice_int64 |
| 376 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 377 | p.dec = (*Buffer).dec_slice_int64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 378 | p.packedDec = (*Buffer).dec_slice_packed_int64 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 379 | default: |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 380 | logNoSliceEnc(t1, t2) |
| 381 | break |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 382 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 383 | case reflect.String: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 384 | p.enc = (*Buffer).enc_slice_string |
| 385 | p.dec = (*Buffer).dec_slice_string |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 386 | case reflect.Ptr: |
| 387 | switch t3 := t2.Elem(); t3.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 388 | default: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 389 | fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T -> %T\n", t1, t2, t3) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 390 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 391 | case reflect.Struct: |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 392 | p.stype = t2.Elem() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 393 | p.isMarshaler = isMarshaler(t2) |
| 394 | p.isUnmarshaler = isUnmarshaler(t2) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 395 | p.enc = (*Buffer).enc_slice_struct_group |
| 396 | p.dec = (*Buffer).dec_slice_struct_group |
| 397 | if p.Wire == "bytes" { |
| 398 | p.enc = (*Buffer).enc_slice_struct_message |
| 399 | p.dec = (*Buffer).dec_slice_struct_message |
| 400 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 401 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 402 | case reflect.Slice: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 403 | switch t2.Elem().Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 404 | default: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 405 | fmt.Fprintf(os.Stderr, "proto: no slice elem oenc for %T -> %T -> %T\n", t1, t2, t2.Elem()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 406 | break |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 407 | case reflect.Uint8: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 408 | p.enc = (*Buffer).enc_slice_slice_byte |
| 409 | p.dec = (*Buffer).dec_slice_slice_byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // precalculate tag code |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 415 | wire := p.WireType |
| 416 | if p.Packed { |
| 417 | wire = WireBytes |
| 418 | } |
| David Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 419 | x := uint32(p.Tag)<<3 | uint32(wire) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 420 | i := 0 |
| 421 | for i = 0; x > 127; i++ { |
| 422 | p.tagbuf[i] = 0x80 | uint8(x&0x7F) |
| 423 | x >>= 7 |
| 424 | } |
| 425 | p.tagbuf[i] = uint8(x) |
| 426 | p.tagcode = p.tagbuf[0 : i+1] |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 427 | |
| 428 | if p.stype != nil { |
| 429 | if lockGetProp { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 430 | p.sprop = GetProperties(p.stype) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 431 | } else { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 432 | p.sprop = getPropertiesLocked(p.stype) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 433 | } |
| 434 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 437 | var ( |
| 438 | marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() |
| 439 | unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() |
| 440 | ) |
| 441 | |
| 442 | // isMarshaler reports whether type t implements Marshaler. |
| 443 | func isMarshaler(t reflect.Type) bool { |
| 444 | // We're checking for (likely) pointer-receiver methods |
| 445 | // so if t is not a pointer, something is very wrong. |
| 446 | // The calls above only invoke isMarshaler on pointer types. |
| 447 | if t.Kind() != reflect.Ptr { |
| 448 | panic("proto: misuse of isMarshaler") |
| 449 | } |
| 450 | return t.Implements(marshalerType) |
| 451 | } |
| 452 | |
| 453 | // isUnmarshaler reports whether type t implements Unmarshaler. |
| 454 | func isUnmarshaler(t reflect.Type) bool { |
| 455 | // We're checking for (likely) pointer-receiver methods |
| 456 | // so if t is not a pointer, something is very wrong. |
| 457 | // The calls above only invoke isUnmarshaler on pointer types. |
| 458 | if t.Kind() != reflect.Ptr { |
| 459 | panic("proto: misuse of isUnmarshaler") |
| 460 | } |
| 461 | return t.Implements(unmarshalerType) |
| 462 | } |
| 463 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 464 | // Init populates the properties from a protocol buffer struct tag. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 465 | func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) { |
| 466 | p.init(typ, name, tag, f, true) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 467 | } |
| 468 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 469 | func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, lockGetProp bool) { |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 470 | // "bytes,49,opt,def=hello!" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 471 | p.Name = name |
| 472 | p.OrigName = name |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 473 | if f != nil { |
| 474 | p.field = toField(f) |
| 475 | } |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 476 | if tag == "" { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 477 | return |
| 478 | } |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 479 | p.Parse(tag) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 480 | p.setEncAndDec(typ, lockGetProp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | var ( |
| 484 | mutex sync.Mutex |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 485 | propertiesMap = make(map[reflect.Type]*StructProperties) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 486 | ) |
| 487 | |
| 488 | // GetProperties returns the list of properties for the type represented by t. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 489 | func GetProperties(t reflect.Type) *StructProperties { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 490 | mutex.Lock() |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 491 | sprop := getPropertiesLocked(t) |
| 492 | mutex.Unlock() |
| 493 | return sprop |
| 494 | } |
| 495 | |
| 496 | // getPropertiesLocked requires that mutex is held. |
| 497 | func getPropertiesLocked(t reflect.Type) *StructProperties { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 498 | if prop, ok := propertiesMap[t]; ok { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 499 | if collectStats { |
| 500 | stats.Chit++ |
| 501 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 502 | return prop |
| 503 | } |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 504 | if collectStats { |
| 505 | stats.Cmiss++ |
| 506 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 507 | |
| 508 | prop := new(StructProperties) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 509 | // in case of recursive protos, fill this in now. |
| 510 | propertiesMap[t] = prop |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 511 | |
| 512 | // build properties |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 513 | prop.extendable = reflect.PtrTo(t).Implements(extendableProtoType) |
| 514 | prop.unrecField = invalidField |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 515 | prop.Prop = make([]*Properties, t.NumField()) |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 516 | prop.order = make([]int, t.NumField()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 517 | for i := 0; i < t.NumField(); i++ { |
| 518 | f := t.Field(i) |
| 519 | p := new(Properties) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 520 | p.init(f.Type, f.Name, f.Tag.Get("protobuf"), &f, false) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 521 | if f.Name == "XXX_extensions" { // special case |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | p.enc = (*Buffer).enc_map |
| 523 | p.dec = nil // not needed |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 524 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 525 | if f.Name == "XXX_unrecognized" { // special case |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 526 | prop.unrecField = toField(&f) |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 527 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 528 | prop.Prop[i] = p |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 529 | prop.order[i] = i |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 530 | if debug { |
| 531 | print(i, " ", f.Name, " ", t.String(), " ") |
| 532 | if p.Tag > 0 { |
| 533 | print(p.String()) |
| 534 | } |
| 535 | print("\n") |
| 536 | } |
| 537 | if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") { |
| 538 | fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]") |
| 539 | } |
| 540 | } |
| 541 | |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 542 | // Re-order prop.order. |
| 543 | sort.Sort(prop) |
| 544 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 545 | // build required counts |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 546 | // build tags |
| 547 | reqCount := 0 |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 548 | prop.origNames = make(map[string]int) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 549 | for i, p := range prop.Prop { |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 550 | if strings.HasPrefix(p.Name, "XXX_") { |
| 551 | // Internal fields should not appear in tags/origNames maps. |
| 552 | // They are handled specially when encoding and decoding. |
| 553 | continue |
| 554 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 555 | if p.Required { |
| 556 | reqCount++ |
| 557 | } |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 558 | prop.tags.put(p.Tag, i) |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 559 | prop.origNames[p.OrigName] = i |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 560 | } |
| 561 | prop.reqCount = reqCount |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 562 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 563 | return prop |
| 564 | } |
| 565 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 566 | // Return the Properties object for the x[0]'th field of the structure. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 567 | func propByIndex(t reflect.Type, x []int) *Properties { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 568 | if len(x) != 1 { |
| 569 | fmt.Fprintf(os.Stderr, "proto: field index dimension %d (not 1) for type %s\n", len(x), t) |
| 570 | return nil |
| 571 | } |
| 572 | prop := GetProperties(t) |
| 573 | return prop.Prop[x[0]] |
| 574 | } |
| 575 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 576 | // Get the address and type of a pointer to a struct from an interface. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 577 | func getbase(pb Message) (t reflect.Type, b structPointer, err error) { |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 578 | if pb == nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 579 | err = ErrNil |
| 580 | return |
| 581 | } |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 582 | // get the reflect type of the pointer to the struct. |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 583 | t = reflect.TypeOf(pb) |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 584 | // get the address of the struct. |
| 585 | value := reflect.ValueOf(pb) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame^] | 586 | b = toStructPointer(value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 587 | return |
| 588 | } |
| 589 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 590 | // A global registry of enum types. |
| 591 | // The generated code will register the generated maps by calling RegisterEnum. |
| 592 | |
| 593 | var enumNameMaps = make(map[string]map[int32]string) |
| 594 | var enumValueMaps = make(map[string]map[string]int32) |
| 595 | |
| 596 | // RegisterEnum is called from the generated code to install the enum descriptor |
| 597 | // maps into the global table to aid parsing ASCII protocol buffers. |
| 598 | func RegisterEnum(typeName string, nameMap map[int32]string, valueMap map[string]int32) { |
| 599 | if _, ok := enumNameMaps[typeName]; ok { |
| Rob Pike | 79c6379 | 2010-03-24 17:48:35 -0700 | [diff] [blame] | 600 | panic("proto: duplicate enum registered: " + typeName) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 601 | } |
| 602 | enumNameMaps[typeName] = nameMap |
| 603 | enumValueMaps[typeName] = valueMap |
| 604 | } |