| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto |
| 33 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 34 | /* |
| 35 | * Types and routines for supporting protocol buffer extensions. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 39 | "errors" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | "reflect" |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 41 | "strconv" |
| David Symonds | 29d5d39 | 2012-12-20 12:14:58 +1100 | [diff] [blame] | 42 | "sync" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 45 | // ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message. |
| 46 | var ErrMissingExtension = errors.New("proto: missing extension") |
| 47 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 48 | // ExtensionRange represents a range of message extensions for a protocol buffer. |
| 49 | // Used in code generated by the protocol compiler. |
| 50 | type ExtensionRange struct { |
| 51 | Start, End int32 // both inclusive |
| 52 | } |
| 53 | |
| 54 | // extendableProto is an interface implemented by any protocol buffer that may be extended. |
| 55 | type extendableProto interface { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 56 | Message |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 57 | ExtensionRangeArray() []ExtensionRange |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 58 | ExtensionMap() map[int32]Extension |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 61 | var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem() |
| 62 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 63 | // ExtensionDesc represents an extension specification. |
| 64 | // Used in generated code from the protocol compiler. |
| 65 | type ExtensionDesc struct { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 66 | ExtendedType Message // nil pointer to the type that is being extended |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 67 | ExtensionType interface{} // nil pointer to the extension type |
| 68 | Field int32 // field number |
| David Symonds | c057ad5 | 2012-02-11 15:43:42 +1100 | [diff] [blame] | 69 | Name string // fully-qualified name of extension, for text formatting |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 70 | Tag string // protobuf tag style |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 73 | func (ed *ExtensionDesc) repeated() bool { |
| 74 | t := reflect.TypeOf(ed.ExtensionType) |
| 75 | return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 76 | } |
| 77 | |
| David Symonds | f511ee3 | 2012-09-06 11:36:28 +1000 | [diff] [blame] | 78 | // Extension represents an extension in a message. |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 79 | type Extension struct { |
| David Symonds | f511ee3 | 2012-09-06 11:36:28 +1000 | [diff] [blame] | 80 | // When an extension is stored in a message using SetExtension |
| 81 | // only desc and value are set. When the message is marshaled |
| 82 | // enc will be set to the encoded form of the message. |
| 83 | // |
| 84 | // When a message is unmarshaled and contains extensions, each |
| 85 | // extension will have only enc set. When such an extension is |
| 86 | // accessed using GetExtension (or GetExtensions) desc and value |
| 87 | // will be set. |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 88 | desc *ExtensionDesc |
| 89 | value interface{} |
| 90 | enc []byte |
| 91 | } |
| 92 | |
| 93 | // SetRawExtension is for testing only. |
| 94 | func SetRawExtension(base extendableProto, id int32, b []byte) { |
| 95 | base.ExtensionMap()[id] = Extension{enc: b} |
| 96 | } |
| 97 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 98 | // isExtensionField returns true iff the given field number is in an extension range. |
| 99 | func isExtensionField(pb extendableProto, field int32) bool { |
| 100 | for _, er := range pb.ExtensionRangeArray() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 101 | if er.Start <= field && field <= er.End { |
| 102 | return true |
| 103 | } |
| 104 | } |
| 105 | return false |
| 106 | } |
| 107 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 108 | // checkExtensionTypes checks that the given extension is valid for pb. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 109 | func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 110 | // Check the extended type. |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 111 | if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b { |
| David Symonds | a7f3a0f | 2013-09-09 13:32:33 +1000 | [diff] [blame] | 112 | return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 113 | } |
| 114 | // Check the range. |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 115 | if !isExtensionField(pb, extension.Field) { |
| David Symonds | a7f3a0f | 2013-09-09 13:32:33 +1000 | [diff] [blame] | 116 | return errors.New("proto: bad extension number; not in declared ranges") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 117 | } |
| 118 | return nil |
| 119 | } |
| 120 | |
| David Symonds | 29d5d39 | 2012-12-20 12:14:58 +1100 | [diff] [blame] | 121 | // extPropKey is sufficient to uniquely identify an extension. |
| 122 | type extPropKey struct { |
| 123 | base reflect.Type |
| 124 | field int32 |
| 125 | } |
| 126 | |
| 127 | var extProp = struct { |
| 128 | sync.RWMutex |
| 129 | m map[extPropKey]*Properties |
| 130 | }{ |
| 131 | m: make(map[extPropKey]*Properties), |
| 132 | } |
| 133 | |
| 134 | func extensionProperties(ed *ExtensionDesc) *Properties { |
| 135 | key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field} |
| 136 | |
| 137 | extProp.RLock() |
| 138 | if prop, ok := extProp.m[key]; ok { |
| 139 | extProp.RUnlock() |
| 140 | return prop |
| 141 | } |
| 142 | extProp.RUnlock() |
| 143 | |
| 144 | extProp.Lock() |
| 145 | defer extProp.Unlock() |
| 146 | // Check again. |
| 147 | if prop, ok := extProp.m[key]; ok { |
| 148 | return prop |
| 149 | } |
| 150 | |
| 151 | prop := new(Properties) |
| 152 | prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil) |
| 153 | extProp.m[key] = prop |
| 154 | return prop |
| 155 | } |
| 156 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 157 | // encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 158 | func encodeExtensionMap(m map[int32]Extension) error { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 159 | for k, e := range m { |
| 160 | if e.value == nil || e.desc == nil { |
| 161 | // Extension is only in its encoded form. |
| 162 | continue |
| 163 | } |
| 164 | |
| 165 | // We don't skip extensions that have an encoded form set, |
| 166 | // because the extension value may have been mutated after |
| 167 | // the last time this function was called. |
| 168 | |
| 169 | et := reflect.TypeOf(e.desc.ExtensionType) |
| David Symonds | 29d5d39 | 2012-12-20 12:14:58 +1100 | [diff] [blame] | 170 | props := extensionProperties(e.desc) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 171 | |
| 172 | p := NewBuffer(nil) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 173 | // If e.value has type T, the encoder expects a *struct{ X T }. |
| 174 | // Pass a *T with a zero field and hope it all works out. |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 175 | x := reflect.New(et) |
| 176 | x.Elem().Set(reflect.ValueOf(e.value)) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 177 | if err := props.enc(p, props, toStructPointer(x)); err != nil { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 178 | return err |
| 179 | } |
| 180 | e.enc = p.buf |
| 181 | m[k] = e |
| 182 | } |
| 183 | return nil |
| 184 | } |
| 185 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 186 | func sizeExtensionMap(m map[int32]Extension) (n int) { |
| 187 | for _, e := range m { |
| 188 | if e.value == nil || e.desc == nil { |
| 189 | // Extension is only in its encoded form. |
| 190 | n += len(e.enc) |
| 191 | continue |
| 192 | } |
| 193 | |
| 194 | // We don't skip extensions that have an encoded form set, |
| 195 | // because the extension value may have been mutated after |
| 196 | // the last time this function was called. |
| 197 | |
| 198 | et := reflect.TypeOf(e.desc.ExtensionType) |
| 199 | props := extensionProperties(e.desc) |
| 200 | |
| 201 | // If e.value has type T, the encoder expects a *struct{ X T }. |
| 202 | // Pass a *T with a zero field and hope it all works out. |
| 203 | x := reflect.New(et) |
| 204 | x.Elem().Set(reflect.ValueOf(e.value)) |
| 205 | n += props.size(props, toStructPointer(x)) |
| 206 | } |
| 207 | return |
| 208 | } |
| 209 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 210 | // HasExtension returns whether the given extension is present in pb. |
| 211 | func HasExtension(pb extendableProto, extension *ExtensionDesc) bool { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 212 | // TODO: Check types, field numbers, etc.? |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 213 | _, ok := pb.ExtensionMap()[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 214 | return ok |
| 215 | } |
| 216 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 217 | // ClearExtension removes the given extension from pb. |
| 218 | func ClearExtension(pb extendableProto, extension *ExtensionDesc) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 219 | // TODO: Check types, field numbers, etc.? |
| Rob Pike | f48475f | 2011-10-28 12:07:40 -0700 | [diff] [blame] | 220 | delete(pb.ExtensionMap(), extension.Field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 223 | // GetExtension parses and returns the given extension of pb. |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 224 | // If the extension is not present it returns ErrMissingExtension. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 225 | func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 226 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 227 | return nil, err |
| 228 | } |
| 229 | |
| David Symonds | ce441e6 | 2014-11-18 13:50:03 +1100 | [diff] [blame] | 230 | emap := pb.ExtensionMap() |
| 231 | e, ok := emap[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 232 | if !ok { |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 233 | return nil, ErrMissingExtension |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 234 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 235 | if e.value != nil { |
| 236 | // Already decoded. Check the descriptor, though. |
| 237 | if e.desc != extension { |
| 238 | // This shouldn't happen. If it does, it means that |
| 239 | // GetExtension was called twice with two different |
| 240 | // descriptors with the same field number. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 241 | return nil, errors.New("proto: descriptor conflict") |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 242 | } |
| 243 | return e.value, nil |
| 244 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 246 | v, err := decodeExtension(e.enc, extension) |
| 247 | if err != nil { |
| 248 | return nil, err |
| 249 | } |
| 250 | |
| 251 | // Remember the decoded version and drop the encoded version. |
| 252 | // That way it is safe to mutate what we return. |
| 253 | e.value = v |
| 254 | e.desc = extension |
| 255 | e.enc = nil |
| David Symonds | ce441e6 | 2014-11-18 13:50:03 +1100 | [diff] [blame] | 256 | emap[extension.Field] = e |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 257 | return e.value, nil |
| 258 | } |
| 259 | |
| 260 | // decodeExtension decodes an extension encoded in b. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 261 | func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) { |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 262 | o := NewBuffer(b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 263 | |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 264 | t := reflect.TypeOf(extension.ExtensionType) |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 265 | rep := extension.repeated() |
| 266 | |
| David Symonds | 29d5d39 | 2012-12-20 12:14:58 +1100 | [diff] [blame] | 267 | props := extensionProperties(extension) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 268 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 269 | // t is a pointer to a struct, pointer to basic type or a slice. |
| 270 | // Allocate a "field" to store the pointer/slice itself; the |
| 271 | // pointer/slice will be stored here. We pass |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 272 | // the address of this field to props.dec. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 273 | // This passes a zero field and a *t and lets props.dec |
| 274 | // interpret it as a *struct{ x t }. |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 275 | value := reflect.New(t).Elem() |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 276 | |
| 277 | for { |
| 278 | // Discard wire type and field number varint. It isn't needed. |
| 279 | if _, err := o.DecodeVarint(); err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 283 | if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil { |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 284 | return nil, err |
| 285 | } |
| 286 | |
| 287 | if !rep || o.index >= len(o.buf) { |
| 288 | break |
| 289 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 290 | } |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 291 | return value.Interface(), nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 294 | // GetExtensions returns a slice of the extensions present in pb that are also listed in es. |
| 295 | // The returned slice has the same length as es; missing extensions will appear as nil elements. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 296 | func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 297 | epb, ok := pb.(extendableProto) |
| 298 | if !ok { |
| David Symonds | a7f3a0f | 2013-09-09 13:32:33 +1000 | [diff] [blame] | 299 | err = errors.New("proto: not an extendable proto") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 300 | return |
| 301 | } |
| 302 | extensions = make([]interface{}, len(es)) |
| 303 | for i, e := range es { |
| 304 | extensions[i], err = GetExtension(epb, e) |
| David Symonds | 8b1c6b7 | 2014-06-20 14:28:56 +1000 | [diff] [blame] | 305 | if err == ErrMissingExtension { |
| 306 | err = nil |
| 307 | } |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 308 | if err != nil { |
| 309 | return |
| 310 | } |
| 311 | } |
| 312 | return |
| 313 | } |
| 314 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 315 | // SetExtension sets the specified extension of pb to the specified value. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 316 | func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 317 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 318 | return err |
| 319 | } |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 320 | typ := reflect.TypeOf(extension.ExtensionType) |
| 321 | if typ != reflect.TypeOf(value) { |
| David Symonds | a7f3a0f | 2013-09-09 13:32:33 +1000 | [diff] [blame] | 322 | return errors.New("proto: bad extension value type") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 325 | pb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 326 | return nil |
| 327 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 328 | |
| 329 | // A global registry of extensions. |
| 330 | // The generated code will register the generated descriptors by calling RegisterExtension. |
| 331 | |
| 332 | var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc) |
| 333 | |
| 334 | // RegisterExtension is called from the generated code. |
| 335 | func RegisterExtension(desc *ExtensionDesc) { |
| 336 | st := reflect.TypeOf(desc.ExtendedType).Elem() |
| 337 | m := extensionMaps[st] |
| 338 | if m == nil { |
| 339 | m = make(map[int32]*ExtensionDesc) |
| 340 | extensionMaps[st] = m |
| 341 | } |
| 342 | if _, ok := m[desc.Field]; ok { |
| 343 | panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field))) |
| 344 | } |
| 345 | m[desc.Field] = desc |
| 346 | } |
| David Symonds | c7cb3fd | 2011-08-29 16:49:58 +1000 | [diff] [blame] | 347 | |
| 348 | // RegisteredExtensions returns a map of the registered extensions of a |
| 349 | // protocol buffer struct, indexed by the extension number. |
| 350 | // The argument pb should be a nil pointer to the struct type. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 351 | func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc { |
| David Symonds | c7cb3fd | 2011-08-29 16:49:58 +1000 | [diff] [blame] | 352 | return extensionMaps[reflect.TypeOf(pb).Elem()] |
| 353 | } |