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