| 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 ( |
| 39 | "os" |
| 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 | |
| 44 | // ExtensionRange represents a range of message extensions for a protocol buffer. |
| 45 | // Used in code generated by the protocol compiler. |
| 46 | type ExtensionRange struct { |
| 47 | Start, End int32 // both inclusive |
| 48 | } |
| 49 | |
| 50 | // extendableProto is an interface implemented by any protocol buffer that may be extended. |
| 51 | type extendableProto interface { |
| 52 | ExtensionRangeArray() []ExtensionRange |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 53 | ExtensionMap() map[int32]Extension |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // ExtensionDesc represents an extension specification. |
| 57 | // Used in generated code from the protocol compiler. |
| 58 | type ExtensionDesc struct { |
| 59 | ExtendedType interface{} // nil pointer to the type that is being extended |
| 60 | ExtensionType interface{} // nil pointer to the extension type |
| 61 | Field int32 // field number |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 62 | Name string // fully-qualified name of extension |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 63 | Tag string // protobuf tag style |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 66 | /* |
| 67 | Extension represents an extension in a message. |
| 68 | |
| 69 | When an extension is stored in a message using SetExtension |
| 70 | only desc and value are set. When the message is marshaled |
| 71 | enc will be set to the encoded form of the message. |
| 72 | |
| 73 | When a message is unmarshaled and contains extensions, each |
| 74 | extension will have only enc set. When such an extension is |
| 75 | accessed using GetExtension (or GetExtensions) desc and value |
| 76 | will be set. |
| 77 | */ |
| 78 | type Extension struct { |
| 79 | desc *ExtensionDesc |
| 80 | value interface{} |
| 81 | enc []byte |
| 82 | } |
| 83 | |
| 84 | // SetRawExtension is for testing only. |
| 85 | func SetRawExtension(base extendableProto, id int32, b []byte) { |
| 86 | base.ExtensionMap()[id] = Extension{enc: b} |
| 87 | } |
| 88 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 89 | // isExtensionField returns true iff the given field number is in an extension range. |
| 90 | func isExtensionField(pb extendableProto, field int32) bool { |
| 91 | for _, er := range pb.ExtensionRangeArray() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 92 | if er.Start <= field && field <= er.End { |
| 93 | return true |
| 94 | } |
| 95 | } |
| 96 | return false |
| 97 | } |
| 98 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 99 | // checkExtensionTypes checks that the given extension is valid for pb. |
| 100 | func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 101 | // Check the extended type. |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 102 | if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 103 | return os.NewError("bad extended type; " + b.String() + " does not extend " + a.String()) |
| 104 | } |
| 105 | // Check the range. |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 106 | if !isExtensionField(pb, extension.Field) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 107 | return os.NewError("bad extension number; not in declared ranges") |
| 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 112 | // encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m. |
| 113 | func encodeExtensionMap(m map[int32]Extension) os.Error { |
| 114 | for k, e := range m { |
| 115 | if e.value == nil || e.desc == nil { |
| 116 | // Extension is only in its encoded form. |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | // We don't skip extensions that have an encoded form set, |
| 121 | // because the extension value may have been mutated after |
| 122 | // the last time this function was called. |
| 123 | |
| 124 | et := reflect.TypeOf(e.desc.ExtensionType) |
| 125 | props := new(Properties) |
| 126 | props.Init(et, "unknown_name", e.desc.Tag, 0) |
| 127 | |
| 128 | p := NewBuffer(nil) |
| 129 | // The encoder must be passed a pointer to e.value. |
| 130 | // Allocate a copy of value so that we can use its address. |
| 131 | x := reflect.New(et) |
| 132 | x.Elem().Set(reflect.ValueOf(e.value)) |
| 133 | if err := props.enc(p, props, x.Pointer()); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | e.enc = p.buf |
| 137 | m[k] = e |
| 138 | } |
| 139 | return nil |
| 140 | } |
| 141 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 142 | // HasExtension returns whether the given extension is present in pb. |
| 143 | func HasExtension(pb extendableProto, extension *ExtensionDesc) bool { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 144 | // TODO: Check types, field numbers, etc.? |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 145 | _, ok := pb.ExtensionMap()[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 146 | return ok |
| 147 | } |
| 148 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 149 | // ClearExtension removes the given extension from pb. |
| 150 | func ClearExtension(pb extendableProto, extension *ExtensionDesc) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 151 | // TODO: Check types, field numbers, etc.? |
| Rob Pike | f48475f | 2011-10-28 12:07:40 -0700 | [diff] [blame^] | 152 | delete(pb.ExtensionMap(), extension.Field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 155 | // GetExtension parses and returns the given extension of pb. |
| 156 | // If the extension is not present it returns (nil, nil). |
| 157 | func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, os.Error) { |
| 158 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 159 | return nil, err |
| 160 | } |
| 161 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 162 | e, ok := pb.ExtensionMap()[extension.Field] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 163 | if !ok { |
| 164 | return nil, nil // not an error |
| 165 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 166 | if e.value != nil { |
| 167 | // Already decoded. Check the descriptor, though. |
| 168 | if e.desc != extension { |
| 169 | // This shouldn't happen. If it does, it means that |
| 170 | // GetExtension was called twice with two different |
| 171 | // descriptors with the same field number. |
| 172 | return nil, os.NewError("proto: descriptor conflict") |
| 173 | } |
| 174 | return e.value, nil |
| 175 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 177 | v, err := decodeExtension(e.enc, extension) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | // Remember the decoded version and drop the encoded version. |
| 183 | // That way it is safe to mutate what we return. |
| 184 | e.value = v |
| 185 | e.desc = extension |
| 186 | e.enc = nil |
| 187 | return e.value, nil |
| 188 | } |
| 189 | |
| 190 | // decodeExtension decodes an extension encoded in b. |
| 191 | func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, os.Error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 192 | // Discard wire type and field number varint. It isn't needed. |
| David Symonds | a4b61c0 | 2011-09-07 14:06:00 +1000 | [diff] [blame] | 193 | _, n := DecodeVarint(b) |
| 194 | o := NewBuffer(b[n:]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 195 | |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 196 | t := reflect.TypeOf(extension.ExtensionType) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 197 | props := &Properties{} |
| 198 | props.Init(t, "irrelevant_name", extension.Tag, 0) |
| 199 | |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 200 | // t is a pointer, likely to a struct. |
| 201 | // Allocate a "field" to store the pointer itself; the |
| 202 | // struct pointer will be stored here. We pass |
| 203 | // the address of this field to props.dec. |
| 204 | value := reflect.New(t).Elem() |
| 205 | if err := props.dec(o, props, value.UnsafeAddr()); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | return nil, err |
| 207 | } |
| Rob Pike | b0447c0 | 2011-10-20 15:20:04 -0700 | [diff] [blame] | 208 | return value.Interface(), nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 211 | // GetExtensions returns a slice of the extensions present in pb that are also listed in es. |
| 212 | // The returned slice has the same length as es; missing extensions will appear as nil elements. |
| 213 | func GetExtensions(pb interface{}, es []*ExtensionDesc) (extensions []interface{}, err os.Error) { |
| 214 | epb, ok := pb.(extendableProto) |
| 215 | if !ok { |
| 216 | err = os.NewError("not an extendable proto") |
| 217 | return |
| 218 | } |
| 219 | extensions = make([]interface{}, len(es)) |
| 220 | for i, e := range es { |
| 221 | extensions[i], err = GetExtension(epb, e) |
| 222 | if err != nil { |
| 223 | return |
| 224 | } |
| 225 | } |
| 226 | return |
| 227 | } |
| 228 | |
| David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 229 | // TODO: (needed for repeated extensions) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 230 | // - ExtensionSize |
| 231 | // - AddExtension |
| 232 | |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 233 | // SetExtension sets the specified extension of pb to the specified value. |
| 234 | func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) os.Error { |
| 235 | if err := checkExtensionTypes(pb, extension); err != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 236 | return err |
| 237 | } |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 238 | typ := reflect.TypeOf(extension.ExtensionType) |
| 239 | if typ != reflect.TypeOf(value) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 240 | return os.NewError("bad extension value type") |
| 241 | } |
| 242 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 243 | pb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 244 | return nil |
| 245 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 246 | |
| 247 | // A global registry of extensions. |
| 248 | // The generated code will register the generated descriptors by calling RegisterExtension. |
| 249 | |
| 250 | var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc) |
| 251 | |
| 252 | // RegisterExtension is called from the generated code. |
| 253 | func RegisterExtension(desc *ExtensionDesc) { |
| 254 | st := reflect.TypeOf(desc.ExtendedType).Elem() |
| 255 | m := extensionMaps[st] |
| 256 | if m == nil { |
| 257 | m = make(map[int32]*ExtensionDesc) |
| 258 | extensionMaps[st] = m |
| 259 | } |
| 260 | if _, ok := m[desc.Field]; ok { |
| 261 | panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field))) |
| 262 | } |
| 263 | m[desc.Field] = desc |
| 264 | } |
| David Symonds | c7cb3fd | 2011-08-29 16:49:58 +1000 | [diff] [blame] | 265 | |
| 266 | // RegisteredExtensions returns a map of the registered extensions of a |
| 267 | // protocol buffer struct, indexed by the extension number. |
| 268 | // The argument pb should be a nil pointer to the struct type. |
| 269 | func RegisteredExtensions(pb interface{}) map[int32]*ExtensionDesc { |
| 270 | return extensionMaps[reflect.TypeOf(pb).Elem()] |
| 271 | } |