| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| David Symonds | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 4 | // https://github.com/golang/protobuf |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto |
| 33 | |
| 34 | /* |
| 35 | * Routines for encoding data into the wire format for protocol buffers. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| 39 | "fmt" |
| David Symonds | a7f1e75 | 2015-11-02 15:31:00 +1100 | [diff] [blame] | 40 | "log" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | "os" |
| 42 | "reflect" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 43 | "sort" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 44 | "strconv" |
| 45 | "strings" |
| 46 | "sync" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 47 | ) |
| 48 | |
| 49 | const debug bool = false |
| 50 | |
| 51 | // Constants that identify the encoding of a value on the wire. |
| 52 | const ( |
| 53 | WireVarint = 0 |
| 54 | WireFixed64 = 1 |
| 55 | WireBytes = 2 |
| 56 | WireStartGroup = 3 |
| 57 | WireEndGroup = 4 |
| 58 | WireFixed32 = 5 |
| 59 | ) |
| 60 | |
| 61 | const startSize = 10 // initial slice/string sizes |
| 62 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 63 | // Encoders are defined in encode.go |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 64 | // An encoder outputs the full representation of a field, including its |
| 65 | // tag and encoder type. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 66 | type encoder func(p *Buffer, prop *Properties, base structPointer) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 67 | |
| 68 | // A valueEncoder encodes a single integer in a particular encoding. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 69 | type valueEncoder func(o *Buffer, x uint64) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 70 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 71 | // Sizers are defined in encode.go |
| 72 | // A sizer returns the encoded size of a field, including its tag and encoder |
| 73 | // type. |
| 74 | type sizer func(prop *Properties, base structPointer) int |
| 75 | |
| 76 | // A valueSizer returns the encoded size of a single integer in a particular |
| 77 | // encoding. |
| 78 | type valueSizer func(x uint64) int |
| 79 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 80 | // Decoders are defined in decode.go |
| 81 | // A decoder creates a value from its wire representation. |
| 82 | // Unrecognized subelements are saved in unrec. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 83 | type decoder func(p *Buffer, prop *Properties, base structPointer) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 84 | |
| 85 | // A valueDecoder decodes a single integer in a particular encoding. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 86 | type valueDecoder func(o *Buffer) (x uint64, err error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 87 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 88 | // A oneofMarshaler does the marshaling for all oneof fields in a message. |
| 89 | type oneofMarshaler func(Message, *Buffer) error |
| 90 | |
| 91 | // A oneofUnmarshaler does the unmarshaling for a oneof field in a message. |
| 92 | type oneofUnmarshaler func(Message, int, int, *Buffer) (bool, error) |
| 93 | |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 94 | // A oneofSizer does the sizing for all oneof fields in a message. |
| 95 | type oneofSizer func(Message) int |
| 96 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 97 | // tagMap is an optimization over map[int]int for typical protocol buffer |
| 98 | // use-cases. Encoded protocol buffers are often in tag order with small tag |
| 99 | // numbers. |
| 100 | type tagMap struct { |
| 101 | fastTags []int |
| 102 | slowTags map[int]int |
| 103 | } |
| 104 | |
| 105 | // tagMapFastLimit is the upper bound on the tag number that will be stored in |
| 106 | // the tagMap slice rather than its map. |
| 107 | const tagMapFastLimit = 1024 |
| 108 | |
| 109 | func (p *tagMap) get(t int) (int, bool) { |
| 110 | if t > 0 && t < tagMapFastLimit { |
| 111 | if t >= len(p.fastTags) { |
| 112 | return 0, false |
| 113 | } |
| 114 | fi := p.fastTags[t] |
| 115 | return fi, fi >= 0 |
| 116 | } |
| 117 | fi, ok := p.slowTags[t] |
| 118 | return fi, ok |
| 119 | } |
| 120 | |
| 121 | func (p *tagMap) put(t int, fi int) { |
| 122 | if t > 0 && t < tagMapFastLimit { |
| 123 | for len(p.fastTags) < t+1 { |
| 124 | p.fastTags = append(p.fastTags, -1) |
| 125 | } |
| 126 | p.fastTags[t] = fi |
| 127 | return |
| 128 | } |
| 129 | if p.slowTags == nil { |
| 130 | p.slowTags = make(map[int]int) |
| 131 | } |
| 132 | p.slowTags[t] = fi |
| 133 | } |
| 134 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 135 | // StructProperties represents properties for all the fields of a struct. |
| David Symonds | 2bba1b2 | 2012-09-26 14:53:08 +1000 | [diff] [blame] | 136 | // decoderTags and decoderOrigNames should only be used by the decoder. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 137 | type StructProperties struct { |
| David Symonds | 2bba1b2 | 2012-09-26 14:53:08 +1000 | [diff] [blame] | 138 | Prop []*Properties // properties for each field |
| 139 | reqCount int // required count |
| 140 | decoderTags tagMap // map from proto tag to struct field number |
| 141 | decoderOrigNames map[string]int // map from original name to struct field number |
| 142 | order []int // list of struct field numbers in tag order |
| 143 | unrecField field // field id of the XXX_unrecognized []byte field |
| 144 | extendable bool // is this an extendable proto |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 145 | |
| 146 | oneofMarshaler oneofMarshaler |
| 147 | oneofUnmarshaler oneofUnmarshaler |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 148 | oneofSizer oneofSizer |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 149 | stype reflect.Type |
| David Symonds | 1baed09 | 2015-08-25 15:42:00 +1000 | [diff] [blame] | 150 | |
| 151 | // OneofTypes contains information about the oneof fields in this message. |
| 152 | // It is keyed by the original name of a field. |
| 153 | OneofTypes map[string]*OneofProperties |
| 154 | } |
| 155 | |
| 156 | // OneofProperties represents information about a specific field in a oneof. |
| 157 | type OneofProperties struct { |
| 158 | Type reflect.Type // pointer to generated struct type for this oneof field |
| 159 | Field int // struct field number of the containing oneof in the message |
| 160 | Prop *Properties |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 163 | // Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec. |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 164 | // See encode.go, (*Buffer).enc_struct. |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 165 | |
| 166 | func (sp *StructProperties) Len() int { return len(sp.order) } |
| 167 | func (sp *StructProperties) Less(i, j int) bool { |
| 168 | return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag |
| 169 | } |
| 170 | func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] } |
| 171 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | // Properties represents the protocol-specific behavior of a single struct field. |
| 173 | type Properties struct { |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 174 | Name string // name of the field, for error messages |
| 175 | OrigName string // original name before protocol compiler (always set) |
| David Symonds | d20896f | 2016-02-10 10:39:21 +1100 | [diff] [blame] | 176 | JSONName string // name to use for JSON; determined by protoc |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 177 | Wire string |
| 178 | WireType int |
| 179 | Tag int |
| 180 | Required bool |
| 181 | Optional bool |
| 182 | Repeated bool |
| 183 | Packed bool // relevant for repeated primitives only |
| 184 | Enum string // set for enum types only |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 185 | proto3 bool // whether this is known to be a proto3 field; set for []byte only |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 186 | oneof bool // whether this is a oneof field |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 187 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 188 | Default string // default value |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 189 | HasDefault bool // whether an explicit default was provided |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 190 | def_uint64 uint64 |
| 191 | |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 192 | enc encoder |
| 193 | valEnc valueEncoder // set for bool and numeric types only |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 194 | field field |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 195 | tagcode []byte // encoding of EncodeVarint((Tag<<3)|WireType) |
| 196 | tagbuf [8]byte |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 197 | stype reflect.Type // set for struct types only |
| 198 | sprop *StructProperties // set for struct types only |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 199 | isMarshaler bool |
| 200 | isUnmarshaler bool |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 201 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 202 | mtype reflect.Type // set for map types only |
| 203 | mkeyprop *Properties // set for map types only |
| 204 | mvalprop *Properties // set for map types only |
| 205 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 206 | size sizer |
| 207 | valSize valueSizer // set for bool and numeric types only |
| 208 | |
| David Symonds | 049646b | 2011-10-21 11:13:45 +1100 | [diff] [blame] | 209 | dec decoder |
| 210 | valDec valueDecoder // set for bool and numeric types only |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 211 | |
| 212 | // If this is a packable field, this will be the decoder for the packed version of the field. |
| 213 | packedDec decoder |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 216 | // String formats the properties in the protobuf struct field tag style. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 217 | func (p *Properties) String() string { |
| 218 | s := p.Wire |
| 219 | s = "," |
| 220 | s += strconv.Itoa(p.Tag) |
| 221 | if p.Required { |
| 222 | s += ",req" |
| 223 | } |
| 224 | if p.Optional { |
| 225 | s += ",opt" |
| 226 | } |
| 227 | if p.Repeated { |
| 228 | s += ",rep" |
| 229 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 230 | if p.Packed { |
| 231 | s += ",packed" |
| 232 | } |
| David Symonds | d20896f | 2016-02-10 10:39:21 +1100 | [diff] [blame] | 233 | s += ",name=" + p.OrigName |
| 234 | if p.JSONName != p.OrigName { |
| 235 | s += ",json=" + p.JSONName |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 236 | } |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 237 | if p.proto3 { |
| 238 | s += ",proto3" |
| 239 | } |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 240 | if p.oneof { |
| 241 | s += ",oneof" |
| 242 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 243 | if len(p.Enum) > 0 { |
| 244 | s += ",enum=" + p.Enum |
| 245 | } |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 246 | if p.HasDefault { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 247 | s += ",def=" + p.Default |
| 248 | } |
| 249 | return s |
| 250 | } |
| 251 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 252 | // 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] | 253 | func (p *Properties) Parse(s string) { |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 254 | // "bytes,49,opt,name=foo,def=hello!" |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 255 | fields := strings.Split(s, ",") // breaks def=, but handled below. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 256 | if len(fields) < 2 { |
| 257 | fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s) |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | p.Wire = fields[0] |
| 262 | switch p.Wire { |
| 263 | case "varint": |
| 264 | p.WireType = WireVarint |
| 265 | p.valEnc = (*Buffer).EncodeVarint |
| 266 | p.valDec = (*Buffer).DecodeVarint |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 267 | p.valSize = sizeVarint |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 268 | case "fixed32": |
| 269 | p.WireType = WireFixed32 |
| 270 | p.valEnc = (*Buffer).EncodeFixed32 |
| 271 | p.valDec = (*Buffer).DecodeFixed32 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 272 | p.valSize = sizeFixed32 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 273 | case "fixed64": |
| 274 | p.WireType = WireFixed64 |
| 275 | p.valEnc = (*Buffer).EncodeFixed64 |
| 276 | p.valDec = (*Buffer).DecodeFixed64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 277 | p.valSize = sizeFixed64 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 278 | case "zigzag32": |
| 279 | p.WireType = WireVarint |
| 280 | p.valEnc = (*Buffer).EncodeZigzag32 |
| 281 | p.valDec = (*Buffer).DecodeZigzag32 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 282 | p.valSize = sizeZigzag32 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 283 | case "zigzag64": |
| 284 | p.WireType = WireVarint |
| 285 | p.valEnc = (*Buffer).EncodeZigzag64 |
| 286 | p.valDec = (*Buffer).DecodeZigzag64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 287 | p.valSize = sizeZigzag64 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 288 | case "bytes", "group": |
| 289 | p.WireType = WireBytes |
| 290 | // no numeric converter for non-numeric types |
| 291 | default: |
| 292 | fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s) |
| 293 | return |
| 294 | } |
| 295 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 296 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 297 | p.Tag, err = strconv.Atoi(fields[1]) |
| 298 | if err != nil { |
| 299 | return |
| 300 | } |
| 301 | |
| 302 | for i := 2; i < len(fields); i++ { |
| 303 | f := fields[i] |
| 304 | switch { |
| 305 | case f == "req": |
| 306 | p.Required = true |
| 307 | case f == "opt": |
| 308 | p.Optional = true |
| 309 | case f == "rep": |
| 310 | p.Repeated = true |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 311 | case f == "packed": |
| 312 | p.Packed = true |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 313 | case strings.HasPrefix(f, "name="): |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 314 | p.OrigName = f[5:] |
| David Symonds | d20896f | 2016-02-10 10:39:21 +1100 | [diff] [blame] | 315 | case strings.HasPrefix(f, "json="): |
| 316 | p.JSONName = f[5:] |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 317 | case strings.HasPrefix(f, "enum="): |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 318 | p.Enum = f[5:] |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 319 | case f == "proto3": |
| 320 | p.proto3 = true |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 321 | case f == "oneof": |
| 322 | p.oneof = true |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 323 | case strings.HasPrefix(f, "def="): |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 324 | p.HasDefault = true |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 325 | p.Default = f[4:] // rest of string |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 326 | if i+1 < len(fields) { |
| 327 | // Commas aren't escaped, and def is always last. |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 328 | p.Default += "," + strings.Join(fields[i+1:], ",") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 329 | break |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 335 | func logNoSliceEnc(t1, t2 reflect.Type) { |
| 336 | fmt.Fprintf(os.Stderr, "proto: no slice oenc for %T = []%T\n", t1, t2) |
| 337 | } |
| 338 | |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 339 | var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem() |
| 340 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 341 | // Initialize the fields for encoding and decoding. |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 342 | func (p *Properties) setEncAndDec(typ reflect.Type, f *reflect.StructField, lockGetProp bool) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 343 | p.enc = nil |
| 344 | p.dec = nil |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 345 | p.size = nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 346 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 347 | switch t1 := typ; t1.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 348 | default: |
| David Symonds | 50386d2 | 2014-10-28 11:00:50 +1100 | [diff] [blame] | 349 | fmt.Fprintf(os.Stderr, "proto: no coders for %v\n", t1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 350 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 351 | // proto3 scalar types |
| 352 | |
| 353 | case reflect.Bool: |
| 354 | p.enc = (*Buffer).enc_proto3_bool |
| 355 | p.dec = (*Buffer).dec_proto3_bool |
| 356 | p.size = size_proto3_bool |
| 357 | case reflect.Int32: |
| 358 | p.enc = (*Buffer).enc_proto3_int32 |
| 359 | p.dec = (*Buffer).dec_proto3_int32 |
| 360 | p.size = size_proto3_int32 |
| 361 | case reflect.Uint32: |
| 362 | p.enc = (*Buffer).enc_proto3_uint32 |
| 363 | p.dec = (*Buffer).dec_proto3_int32 // can reuse |
| 364 | p.size = size_proto3_uint32 |
| 365 | case reflect.Int64, reflect.Uint64: |
| 366 | p.enc = (*Buffer).enc_proto3_int64 |
| 367 | p.dec = (*Buffer).dec_proto3_int64 |
| 368 | p.size = size_proto3_int64 |
| 369 | case reflect.Float32: |
| 370 | p.enc = (*Buffer).enc_proto3_uint32 // can just treat them as bits |
| 371 | p.dec = (*Buffer).dec_proto3_int32 |
| 372 | p.size = size_proto3_uint32 |
| 373 | case reflect.Float64: |
| 374 | p.enc = (*Buffer).enc_proto3_int64 // can just treat them as bits |
| 375 | p.dec = (*Buffer).dec_proto3_int64 |
| 376 | p.size = size_proto3_int64 |
| 377 | case reflect.String: |
| 378 | p.enc = (*Buffer).enc_proto3_string |
| 379 | p.dec = (*Buffer).dec_proto3_string |
| 380 | p.size = size_proto3_string |
| 381 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 382 | case reflect.Ptr: |
| 383 | switch t2 := t1.Elem(); t2.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 384 | default: |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 385 | fmt.Fprintf(os.Stderr, "proto: no encoder function for %v -> %v\n", t1, t2) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 386 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 387 | case reflect.Bool: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 388 | p.enc = (*Buffer).enc_bool |
| 389 | p.dec = (*Buffer).dec_bool |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 390 | p.size = size_bool |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 391 | case reflect.Int32: |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 392 | p.enc = (*Buffer).enc_int32 |
| 393 | p.dec = (*Buffer).dec_int32 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 394 | p.size = size_int32 |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 395 | case reflect.Uint32: |
| 396 | p.enc = (*Buffer).enc_uint32 |
| 397 | p.dec = (*Buffer).dec_int32 // can reuse |
| 398 | p.size = size_uint32 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 399 | case reflect.Int64, reflect.Uint64: |
| 400 | p.enc = (*Buffer).enc_int64 |
| 401 | p.dec = (*Buffer).dec_int64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 402 | p.size = size_int64 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 403 | case reflect.Float32: |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 404 | p.enc = (*Buffer).enc_uint32 // can just treat them as bits |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 405 | p.dec = (*Buffer).dec_int32 |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 406 | p.size = size_uint32 |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 407 | case reflect.Float64: |
| 408 | p.enc = (*Buffer).enc_int64 // can just treat them as bits |
| 409 | p.dec = (*Buffer).dec_int64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 410 | p.size = size_int64 |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 411 | case reflect.String: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 412 | p.enc = (*Buffer).enc_string |
| 413 | p.dec = (*Buffer).dec_string |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 414 | p.size = size_string |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 415 | case reflect.Struct: |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 416 | p.stype = t1.Elem() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 417 | p.isMarshaler = isMarshaler(t1) |
| 418 | p.isUnmarshaler = isUnmarshaler(t1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 419 | if p.Wire == "bytes" { |
| 420 | p.enc = (*Buffer).enc_struct_message |
| 421 | p.dec = (*Buffer).dec_struct_message |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 422 | p.size = size_struct_message |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 423 | } else { |
| 424 | p.enc = (*Buffer).enc_struct_group |
| 425 | p.dec = (*Buffer).dec_struct_group |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 426 | p.size = size_struct_group |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 430 | case reflect.Slice: |
| 431 | switch t2 := t1.Elem(); t2.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 432 | default: |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 433 | logNoSliceEnc(t1, t2) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 434 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 435 | case reflect.Bool: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 436 | if p.Packed { |
| 437 | p.enc = (*Buffer).enc_slice_packed_bool |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 438 | p.size = size_slice_packed_bool |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 439 | } else { |
| 440 | p.enc = (*Buffer).enc_slice_bool |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 441 | p.size = size_slice_bool |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 442 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 443 | p.dec = (*Buffer).dec_slice_bool |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 444 | p.packedDec = (*Buffer).dec_slice_packed_bool |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 445 | case reflect.Int32: |
| 446 | if p.Packed { |
| 447 | p.enc = (*Buffer).enc_slice_packed_int32 |
| 448 | p.size = size_slice_packed_int32 |
| 449 | } else { |
| 450 | p.enc = (*Buffer).enc_slice_int32 |
| 451 | p.size = size_slice_int32 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 452 | } |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 453 | p.dec = (*Buffer).dec_slice_int32 |
| 454 | p.packedDec = (*Buffer).dec_slice_packed_int32 |
| 455 | case reflect.Uint32: |
| 456 | if p.Packed { |
| 457 | p.enc = (*Buffer).enc_slice_packed_uint32 |
| 458 | p.size = size_slice_packed_uint32 |
| 459 | } else { |
| 460 | p.enc = (*Buffer).enc_slice_uint32 |
| 461 | p.size = size_slice_uint32 |
| 462 | } |
| 463 | p.dec = (*Buffer).dec_slice_int32 |
| 464 | p.packedDec = (*Buffer).dec_slice_packed_int32 |
| 465 | case reflect.Int64, reflect.Uint64: |
| 466 | if p.Packed { |
| 467 | p.enc = (*Buffer).enc_slice_packed_int64 |
| 468 | p.size = size_slice_packed_int64 |
| 469 | } else { |
| 470 | p.enc = (*Buffer).enc_slice_int64 |
| 471 | p.size = size_slice_int64 |
| 472 | } |
| 473 | p.dec = (*Buffer).dec_slice_int64 |
| 474 | p.packedDec = (*Buffer).dec_slice_packed_int64 |
| 475 | case reflect.Uint8: |
| 476 | p.enc = (*Buffer).enc_slice_byte |
| 477 | p.dec = (*Buffer).dec_slice_byte |
| 478 | p.size = size_slice_byte |
| David Symonds | 889ae49 | 2015-03-23 09:48:33 +1100 | [diff] [blame] | 479 | // This is a []byte, which is either a bytes field, |
| 480 | // or the value of a map field. In the latter case, |
| 481 | // we always encode an empty []byte, so we should not |
| 482 | // use the proto3 enc/size funcs. |
| 483 | // f == nil iff this is the key/value of a map field. |
| 484 | if p.proto3 && f != nil { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 485 | p.enc = (*Buffer).enc_proto3_slice_byte |
| 486 | p.size = size_proto3_slice_byte |
| 487 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 488 | case reflect.Float32, reflect.Float64: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 489 | switch t2.Bits() { |
| 490 | case 32: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 491 | // can just treat them as bits |
| 492 | if p.Packed { |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 493 | p.enc = (*Buffer).enc_slice_packed_uint32 |
| 494 | p.size = size_slice_packed_uint32 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 495 | } else { |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 496 | p.enc = (*Buffer).enc_slice_uint32 |
| 497 | p.size = size_slice_uint32 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 498 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 499 | p.dec = (*Buffer).dec_slice_int32 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 500 | p.packedDec = (*Buffer).dec_slice_packed_int32 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 501 | case 64: |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 502 | // can just treat them as bits |
| 503 | if p.Packed { |
| 504 | p.enc = (*Buffer).enc_slice_packed_int64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 505 | p.size = size_slice_packed_int64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 506 | } else { |
| 507 | p.enc = (*Buffer).enc_slice_int64 |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 508 | p.size = size_slice_int64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 509 | } |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 510 | p.dec = (*Buffer).dec_slice_int64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 511 | p.packedDec = (*Buffer).dec_slice_packed_int64 |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 512 | default: |
| David Symonds | 5922d07 | 2011-06-22 15:48:09 +1000 | [diff] [blame] | 513 | logNoSliceEnc(t1, t2) |
| 514 | break |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 515 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 516 | case reflect.String: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 517 | p.enc = (*Buffer).enc_slice_string |
| 518 | p.dec = (*Buffer).dec_slice_string |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 519 | p.size = size_slice_string |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 520 | case reflect.Ptr: |
| 521 | switch t3 := t2.Elem(); t3.Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | default: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 523 | 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] | 524 | break |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 525 | case reflect.Struct: |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 526 | p.stype = t2.Elem() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 527 | p.isMarshaler = isMarshaler(t2) |
| 528 | p.isUnmarshaler = isUnmarshaler(t2) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 529 | if p.Wire == "bytes" { |
| 530 | p.enc = (*Buffer).enc_slice_struct_message |
| 531 | p.dec = (*Buffer).dec_slice_struct_message |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 532 | p.size = size_slice_struct_message |
| 533 | } else { |
| 534 | p.enc = (*Buffer).enc_slice_struct_group |
| 535 | p.dec = (*Buffer).dec_slice_struct_group |
| 536 | p.size = size_slice_struct_group |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 537 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 538 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 539 | case reflect.Slice: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 540 | switch t2.Elem().Kind() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 541 | default: |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 542 | 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] | 543 | break |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 544 | case reflect.Uint8: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 545 | p.enc = (*Buffer).enc_slice_slice_byte |
| 546 | p.dec = (*Buffer).dec_slice_slice_byte |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 547 | p.size = size_slice_slice_byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 550 | |
| 551 | case reflect.Map: |
| 552 | p.enc = (*Buffer).enc_new_map |
| 553 | p.dec = (*Buffer).dec_new_map |
| 554 | p.size = size_new_map |
| 555 | |
| 556 | p.mtype = t1 |
| 557 | p.mkeyprop = &Properties{} |
| 558 | p.mkeyprop.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp) |
| 559 | p.mvalprop = &Properties{} |
| 560 | vtype := p.mtype.Elem() |
| 561 | if vtype.Kind() != reflect.Ptr && vtype.Kind() != reflect.Slice { |
| 562 | // The value type is not a message (*T) or bytes ([]byte), |
| 563 | // so we need encoders for the pointer to this type. |
| 564 | vtype = reflect.PtrTo(vtype) |
| 565 | } |
| 566 | p.mvalprop.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // precalculate tag code |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 570 | wire := p.WireType |
| 571 | if p.Packed { |
| 572 | wire = WireBytes |
| 573 | } |
| David Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 574 | x := uint32(p.Tag)<<3 | uint32(wire) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 575 | i := 0 |
| 576 | for i = 0; x > 127; i++ { |
| 577 | p.tagbuf[i] = 0x80 | uint8(x&0x7F) |
| 578 | x >>= 7 |
| 579 | } |
| 580 | p.tagbuf[i] = uint8(x) |
| 581 | p.tagcode = p.tagbuf[0 : i+1] |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 582 | |
| 583 | if p.stype != nil { |
| 584 | if lockGetProp { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 585 | p.sprop = GetProperties(p.stype) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 586 | } else { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 587 | p.sprop = getPropertiesLocked(p.stype) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 588 | } |
| 589 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 592 | var ( |
| 593 | marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() |
| 594 | unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() |
| 595 | ) |
| 596 | |
| 597 | // isMarshaler reports whether type t implements Marshaler. |
| 598 | func isMarshaler(t reflect.Type) bool { |
| 599 | // We're checking for (likely) pointer-receiver methods |
| 600 | // so if t is not a pointer, something is very wrong. |
| 601 | // The calls above only invoke isMarshaler on pointer types. |
| 602 | if t.Kind() != reflect.Ptr { |
| 603 | panic("proto: misuse of isMarshaler") |
| 604 | } |
| 605 | return t.Implements(marshalerType) |
| 606 | } |
| 607 | |
| 608 | // isUnmarshaler reports whether type t implements Unmarshaler. |
| 609 | func isUnmarshaler(t reflect.Type) bool { |
| 610 | // We're checking for (likely) pointer-receiver methods |
| 611 | // so if t is not a pointer, something is very wrong. |
| 612 | // The calls above only invoke isUnmarshaler on pointer types. |
| 613 | if t.Kind() != reflect.Ptr { |
| 614 | panic("proto: misuse of isUnmarshaler") |
| 615 | } |
| 616 | return t.Implements(unmarshalerType) |
| 617 | } |
| 618 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 619 | // Init populates the properties from a protocol buffer struct tag. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 620 | func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) { |
| 621 | p.init(typ, name, tag, f, true) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 622 | } |
| 623 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 624 | 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] | 625 | // "bytes,49,opt,def=hello!" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 626 | p.Name = name |
| 627 | p.OrigName = name |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 628 | if f != nil { |
| 629 | p.field = toField(f) |
| 630 | } |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 631 | if tag == "" { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 632 | return |
| 633 | } |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 634 | p.Parse(tag) |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 635 | p.setEncAndDec(typ, f, lockGetProp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | var ( |
| David Symonds | f7137ae | 2015-03-17 08:40:00 +1100 | [diff] [blame] | 639 | propertiesMu sync.RWMutex |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 640 | propertiesMap = make(map[reflect.Type]*StructProperties) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 641 | ) |
| 642 | |
| 643 | // GetProperties returns the list of properties for the type represented by t. |
| David Symonds | 905b3fd | 2014-10-12 16:27:29 +1100 | [diff] [blame] | 644 | // t must represent a generated struct type of a protocol message. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 645 | func GetProperties(t reflect.Type) *StructProperties { |
| David Symonds | 905b3fd | 2014-10-12 16:27:29 +1100 | [diff] [blame] | 646 | if t.Kind() != reflect.Struct { |
| 647 | panic("proto: type must have kind struct") |
| 648 | } |
| David Symonds | f7137ae | 2015-03-17 08:40:00 +1100 | [diff] [blame] | 649 | |
| 650 | // Most calls to GetProperties in a long-running program will be |
| 651 | // retrieving details for types we have seen before. |
| 652 | propertiesMu.RLock() |
| 653 | sprop, ok := propertiesMap[t] |
| 654 | propertiesMu.RUnlock() |
| 655 | if ok { |
| 656 | if collectStats { |
| 657 | stats.Chit++ |
| 658 | } |
| 659 | return sprop |
| 660 | } |
| 661 | |
| 662 | propertiesMu.Lock() |
| 663 | sprop = getPropertiesLocked(t) |
| 664 | propertiesMu.Unlock() |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 665 | return sprop |
| 666 | } |
| 667 | |
| David Symonds | f7137ae | 2015-03-17 08:40:00 +1100 | [diff] [blame] | 668 | // getPropertiesLocked requires that propertiesMu is held. |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 669 | func getPropertiesLocked(t reflect.Type) *StructProperties { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 670 | if prop, ok := propertiesMap[t]; ok { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 671 | if collectStats { |
| 672 | stats.Chit++ |
| 673 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 674 | return prop |
| 675 | } |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 676 | if collectStats { |
| 677 | stats.Cmiss++ |
| 678 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 679 | |
| 680 | prop := new(StructProperties) |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 681 | // in case of recursive protos, fill this in now. |
| 682 | propertiesMap[t] = prop |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 683 | |
| 684 | // build properties |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 685 | prop.extendable = reflect.PtrTo(t).Implements(extendableProtoType) |
| 686 | prop.unrecField = invalidField |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 687 | prop.Prop = make([]*Properties, t.NumField()) |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 688 | prop.order = make([]int, t.NumField()) |
| David Symonds | 2037090 | 2013-03-23 17:20:01 +1100 | [diff] [blame] | 689 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 690 | for i := 0; i < t.NumField(); i++ { |
| 691 | f := t.Field(i) |
| 692 | p := new(Properties) |
| David Symonds | 2037090 | 2013-03-23 17:20:01 +1100 | [diff] [blame] | 693 | name := f.Name |
| 694 | p.init(f.Type, name, f.Tag.Get("protobuf"), &f, false) |
| 695 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 696 | if f.Name == "XXX_extensions" { // special case |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 697 | p.enc = (*Buffer).enc_map |
| 698 | p.dec = nil // not needed |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 699 | p.size = size_map |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 700 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 701 | if f.Name == "XXX_unrecognized" { // special case |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 702 | prop.unrecField = toField(&f) |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 703 | } |
| Dave Day | 2ebff28 | 2016-04-18 12:44:00 +1000 | [diff] [blame] | 704 | oneof := f.Tag.Get("protobuf_oneof") // special case |
| 705 | if oneof != "" { |
| 706 | // Oneof fields don't use the traditional protobuf tag. |
| 707 | p.OrigName = oneof |
| 708 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 709 | prop.Prop[i] = p |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 710 | prop.order[i] = i |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 711 | if debug { |
| 712 | print(i, " ", f.Name, " ", t.String(), " ") |
| 713 | if p.Tag > 0 { |
| 714 | print(p.String()) |
| 715 | } |
| 716 | print("\n") |
| 717 | } |
| Dave Day | 2ebff28 | 2016-04-18 12:44:00 +1000 | [diff] [blame] | 718 | if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") && oneof == "" { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 719 | fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]") |
| 720 | } |
| 721 | } |
| 722 | |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 723 | // Re-order prop.order. |
| 724 | sort.Sort(prop) |
| 725 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 726 | type oneofMessage interface { |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 727 | XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{}) |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 728 | } |
| 729 | if om, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); ok { |
| David Symonds | 1baed09 | 2015-08-25 15:42:00 +1000 | [diff] [blame] | 730 | var oots []interface{} |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 731 | prop.oneofMarshaler, prop.oneofUnmarshaler, prop.oneofSizer, oots = om.XXX_OneofFuncs() |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 732 | prop.stype = t |
| David Symonds | 1baed09 | 2015-08-25 15:42:00 +1000 | [diff] [blame] | 733 | |
| 734 | // Interpret oneof metadata. |
| 735 | prop.OneofTypes = make(map[string]*OneofProperties) |
| 736 | for _, oot := range oots { |
| 737 | oop := &OneofProperties{ |
| 738 | Type: reflect.ValueOf(oot).Type(), // *T |
| 739 | Prop: new(Properties), |
| 740 | } |
| 741 | sft := oop.Type.Elem().Field(0) |
| 742 | oop.Prop.Name = sft.Name |
| 743 | oop.Prop.Parse(sft.Tag.Get("protobuf")) |
| 744 | // There will be exactly one interface field that |
| 745 | // this new value is assignable to. |
| 746 | for i := 0; i < t.NumField(); i++ { |
| 747 | f := t.Field(i) |
| 748 | if f.Type.Kind() != reflect.Interface { |
| 749 | continue |
| 750 | } |
| 751 | if !oop.Type.AssignableTo(f.Type) { |
| 752 | continue |
| 753 | } |
| 754 | oop.Field = i |
| 755 | break |
| 756 | } |
| 757 | prop.OneofTypes[oop.Prop.OrigName] = oop |
| 758 | } |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 759 | } |
| 760 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 761 | // build required counts |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 762 | // build tags |
| 763 | reqCount := 0 |
| David Symonds | 2bba1b2 | 2012-09-26 14:53:08 +1000 | [diff] [blame] | 764 | prop.decoderOrigNames = make(map[string]int) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 765 | for i, p := range prop.Prop { |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 766 | if strings.HasPrefix(p.Name, "XXX_") { |
| 767 | // Internal fields should not appear in tags/origNames maps. |
| 768 | // They are handled specially when encoding and decoding. |
| 769 | continue |
| 770 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 771 | if p.Required { |
| 772 | reqCount++ |
| 773 | } |
| David Symonds | 2bba1b2 | 2012-09-26 14:53:08 +1000 | [diff] [blame] | 774 | prop.decoderTags.put(p.Tag, i) |
| 775 | prop.decoderOrigNames[p.OrigName] = i |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 776 | } |
| 777 | prop.reqCount = reqCount |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 778 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 779 | return prop |
| 780 | } |
| 781 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 782 | // 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] | 783 | func propByIndex(t reflect.Type, x []int) *Properties { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 784 | if len(x) != 1 { |
| 785 | fmt.Fprintf(os.Stderr, "proto: field index dimension %d (not 1) for type %s\n", len(x), t) |
| 786 | return nil |
| 787 | } |
| 788 | prop := GetProperties(t) |
| 789 | return prop.Prop[x[0]] |
| 790 | } |
| 791 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 792 | // 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] | 793 | func getbase(pb Message) (t reflect.Type, b structPointer, err error) { |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 794 | if pb == nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 795 | err = ErrNil |
| 796 | return |
| 797 | } |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 798 | // get the reflect type of the pointer to the struct. |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 799 | t = reflect.TypeOf(pb) |
| Rob Pike | f1b341e | 2011-10-20 14:51:10 -0700 | [diff] [blame] | 800 | // get the address of the struct. |
| 801 | value := reflect.ValueOf(pb) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 802 | b = toStructPointer(value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 803 | return |
| 804 | } |
| 805 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 806 | // A global registry of enum types. |
| 807 | // The generated code will register the generated maps by calling RegisterEnum. |
| 808 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 809 | var enumValueMaps = make(map[string]map[string]int32) |
| 810 | |
| 811 | // RegisterEnum is called from the generated code to install the enum descriptor |
| David Symonds | f8a1fcc | 2013-05-03 08:51:23 +1000 | [diff] [blame] | 812 | // maps into the global table to aid parsing text format protocol buffers. |
| 813 | func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) { |
| 814 | if _, ok := enumValueMaps[typeName]; ok { |
| Rob Pike | 79c6379 | 2010-03-24 17:48:35 -0700 | [diff] [blame] | 815 | panic("proto: duplicate enum registered: " + typeName) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 816 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 817 | enumValueMaps[typeName] = valueMap |
| 818 | } |
| David Symonds | 5d7f79b | 2015-10-19 11:37:00 +1100 | [diff] [blame] | 819 | |
| 820 | // EnumValueMap returns the mapping from names to integers of the |
| 821 | // enum type enumType, or a nil if not found. |
| 822 | func EnumValueMap(enumType string) map[string]int32 { |
| 823 | return enumValueMaps[enumType] |
| 824 | } |
| David Symonds | a7f1e75 | 2015-11-02 15:31:00 +1100 | [diff] [blame] | 825 | |
| 826 | // A registry of all linked message types. |
| David Symonds | d3d7838 | 2015-11-05 09:41:00 +1100 | [diff] [blame] | 827 | // The string is a fully-qualified proto name ("pkg.Message"). |
| 828 | var ( |
| 829 | protoTypes = make(map[string]reflect.Type) |
| 830 | revProtoTypes = make(map[reflect.Type]string) |
| 831 | ) |
| David Symonds | a7f1e75 | 2015-11-02 15:31:00 +1100 | [diff] [blame] | 832 | |
| 833 | // RegisterType is called from generated code and maps from the fully qualified |
| 834 | // proto name to the type (pointer to struct) of the protocol buffer. |
| David Symonds | d3d7838 | 2015-11-05 09:41:00 +1100 | [diff] [blame] | 835 | func RegisterType(x Message, name string) { |
| David Symonds | a7f1e75 | 2015-11-02 15:31:00 +1100 | [diff] [blame] | 836 | if _, ok := protoTypes[name]; ok { |
| 837 | // TODO: Some day, make this a panic. |
| 838 | log.Printf("proto: duplicate proto type registered: %s", name) |
| 839 | return |
| 840 | } |
| David Symonds | d3d7838 | 2015-11-05 09:41:00 +1100 | [diff] [blame] | 841 | t := reflect.TypeOf(x) |
| 842 | protoTypes[name] = t |
| 843 | revProtoTypes[t] = name |
| David Symonds | a7f1e75 | 2015-11-02 15:31:00 +1100 | [diff] [blame] | 844 | } |
| David Symonds | d3d7838 | 2015-11-05 09:41:00 +1100 | [diff] [blame] | 845 | |
| 846 | // MessageName returns the fully-qualified proto name for the given message type. |
| 847 | func MessageName(x Message) string { return revProtoTypes[reflect.TypeOf(x)] } |
| 848 | |
| 849 | // MessageType returns the message type (pointer to struct) for a named message. |
| 850 | func MessageType(name string) reflect.Type { return protoTypes[name] } |