| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto |
| 33 | |
| 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" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 44 | // ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message. |
| 45 | var ErrMissingExtension = errors.New("proto: missing extension") |
| 46 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 47 | // ExtensionRange represents a range of message extensions for a protocol buffer. |
| 48 | // Used in code generated by the protocol compiler. |
| 49 | type ExtensionRange struct { |
| 50 | Start, End int32 // both inclusive |
| 51 | } |
| 52 | |
| 53 | // extendableProto is an interface implemented by any protocol buffer that may be extended. |
| 54 | type extendableProto interface { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 55 | Message |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 56 | ExtensionRangeArray() []ExtensionRange |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 57 | ExtensionMap() map[int32]Extension |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // ExtensionDesc represents an extension specification. |
| 61 | // Used in generated code from the protocol compiler. |
| 62 | type ExtensionDesc struct { |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 63 | ExtendedType Message // nil pointer to the type that is being extended |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 64 | ExtensionType interface{} // nil pointer to the extension type |
| 65 | Field int32 // field number |
| David Symonds | c057ad5 | 2012-02-11 15:43:42 +1100 | [diff] [blame] | 66 | Name string // fully-qualified name of extension, for text formatting |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 67 | Tag string // protobuf tag style |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 70 | func (ed *ExtensionDesc) repeated() bool { |
| 71 | t := reflect.TypeOf(ed.ExtensionType) |
| 72 | return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 73 | } |
| 74 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 75 | /* |
| 76 | Extension represents an extension in a message. |
| 77 | |
| 78 | When an extension is stored in a message using SetExtension |
| 79 | only desc and value are set. When the message is marshaled |
| 80 | enc will be set to the encoded form of the message. |
| 81 | |
| 82 | When a message is unmarshaled and contains extensions, each |
| 83 | extension will have only enc set. When such an extension is |
| 84 | accessed using GetExtension (or GetExtensions) desc and value |
| 85 | will be set. |
| 86 | */ |
| 87 | type Extension struct { |
| 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 { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 112 | return errors.New("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) { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 116 | return errors.New("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 | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 121 | // encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 122 | func encodeExtensionMap(m map[int32]Extension) error { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 123 | for k, e := range m { |
| 124 | if e.value == nil || e.desc == nil { |
| 125 | // Extension is only in its encoded form. |
| 126 | continue |
| 127 | } |
| 128 | |
| 129 | // We don't skip extensions that have an encoded form set, |
| 130 | // because the extension value may have been mutated after |
| 131 | // the last time this function was called. |
| 132 | |
| 133 | et := reflect.TypeOf(e.desc.ExtensionType) |
| 134 | props := new(Properties) |
| 135 | props.Init(et, "unknown_name", e.desc.Tag, 0) |
| 136 | |
| 137 | p := NewBuffer(nil) |
| 138 | // The encoder must be passed a pointer to e.value. |
| 139 | // Allocate a copy of value so that we can use its address. |
| 140 | x := reflect.New(et) |
| 141 | x.Elem().Set(reflect.ValueOf(e.value)) |
| 142 | if err := props.enc(p, props, x.Pointer()); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | e.enc = p.buf |
| 146 | m[k] = e |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 151 | // HasExtension returns whether the given extension is present in pb. |
| 152 | func HasExtension(pb extendableProto, extension *ExtensionDesc) bool { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 153 | // TODO: Check types, field numbers, etc.? |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 154 | _, ok := pb.ExtensionMap()[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 155 | return ok |
| 156 | } |
| 157 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 158 | // ClearExtension removes the given extension from pb. |
| 159 | func ClearExtension(pb extendableProto, extension *ExtensionDesc) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 160 | // TODO: Check types, field numbers, etc.? |
| Rob Pike | f48475f | 2011-10-28 12:07:40 -0700 | [diff] [blame] | 161 | delete(pb.ExtensionMap(), extension.Field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 164 | // GetExtension parses and returns the given extension of pb. |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 165 | // If the extension is not present it returns ErrMissingExtension. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 166 | func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 167 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 168 | return nil, err |
| 169 | } |
| 170 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 171 | e, ok := pb.ExtensionMap()[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | if !ok { |
| David Symonds | a7e9ef9 | 2012-01-18 18:27:58 +1100 | [diff] [blame] | 173 | return nil, ErrMissingExtension |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 174 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 175 | if e.value != nil { |
| 176 | // Already decoded. Check the descriptor, though. |
| 177 | if e.desc != extension { |
| 178 | // This shouldn't happen. If it does, it means that |
| 179 | // GetExtension was called twice with two different |
| 180 | // descriptors with the same field number. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 181 | return nil, errors.New("proto: descriptor conflict") |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 182 | } |
| 183 | return e.value, nil |
| 184 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 185 | |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 186 | v, err := decodeExtension(e.enc, extension) |
| 187 | if err != nil { |
| 188 | return nil, err |
| 189 | } |
| 190 | |
| 191 | // Remember the decoded version and drop the encoded version. |
| 192 | // That way it is safe to mutate what we return. |
| 193 | e.value = v |
| 194 | e.desc = extension |
| 195 | e.enc = nil |
| 196 | return e.value, nil |
| 197 | } |
| 198 | |
| 199 | // decodeExtension decodes an extension encoded in b. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 200 | func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) { |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 201 | o := NewBuffer(b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 202 | |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 203 | t := reflect.TypeOf(extension.ExtensionType) |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 204 | rep := extension.repeated() |
| 205 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | props := &Properties{} |
| 207 | props.Init(t, "irrelevant_name", extension.Tag, 0) |
| 208 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 209 | // t is a pointer to a struct, pointer to basic type or a slice. |
| 210 | // Allocate a "field" to store the pointer/slice itself; the |
| 211 | // pointer/slice will be stored here. We pass |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 212 | // the address of this field to props.dec. |
| 213 | value := reflect.New(t).Elem() |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 214 | |
| 215 | for { |
| 216 | // Discard wire type and field number varint. It isn't needed. |
| 217 | if _, err := o.DecodeVarint(); err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | |
| 221 | if err := props.dec(o, props, value.UnsafeAddr()); err != nil { |
| 222 | return nil, err |
| 223 | } |
| 224 | |
| 225 | if !rep || o.index >= len(o.buf) { |
| 226 | break |
| 227 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 228 | } |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 229 | return value.Interface(), nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 232 | // GetExtensions returns a slice of the extensions present in pb that are also listed in es. |
| 233 | // 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^] | 234 | func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 235 | epb, ok := pb.(extendableProto) |
| 236 | if !ok { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 237 | err = errors.New("not an extendable proto") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 238 | return |
| 239 | } |
| 240 | extensions = make([]interface{}, len(es)) |
| 241 | for i, e := range es { |
| 242 | extensions[i], err = GetExtension(epb, e) |
| 243 | if err != nil { |
| 244 | return |
| 245 | } |
| 246 | } |
| 247 | return |
| 248 | } |
| 249 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 250 | // SetExtension sets the specified extension of pb to the specified value. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 251 | func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error { |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 252 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 253 | return err |
| 254 | } |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 255 | typ := reflect.TypeOf(extension.ExtensionType) |
| 256 | if typ != reflect.TypeOf(value) { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 257 | return errors.New("bad extension value type") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 260 | pb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 261 | return nil |
| 262 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 263 | |
| 264 | // A global registry of extensions. |
| 265 | // The generated code will register the generated descriptors by calling RegisterExtension. |
| 266 | |
| 267 | var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc) |
| 268 | |
| 269 | // RegisterExtension is called from the generated code. |
| 270 | func RegisterExtension(desc *ExtensionDesc) { |
| 271 | st := reflect.TypeOf(desc.ExtendedType).Elem() |
| 272 | m := extensionMaps[st] |
| 273 | if m == nil { |
| 274 | m = make(map[int32]*ExtensionDesc) |
| 275 | extensionMaps[st] = m |
| 276 | } |
| 277 | if _, ok := m[desc.Field]; ok { |
| 278 | panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field))) |
| 279 | } |
| 280 | m[desc.Field] = desc |
| 281 | } |
| David Symonds | c7cb3fd | 2011-08-29 16:49:58 +1000 | [diff] [blame] | 282 | |
| 283 | // RegisteredExtensions returns a map of the registered extensions of a |
| 284 | // protocol buffer struct, indexed by the extension number. |
| 285 | // The argument pb should be a nil pointer to the struct type. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 286 | func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc { |
| David Symonds | c7cb3fd | 2011-08-29 16:49:58 +1000 | [diff] [blame] | 287 | return extensionMaps[reflect.TypeOf(pb).Elem()] |
| 288 | } |