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