1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
35 * Types and routines for supporting protocol buffer extensions.
46 // ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
47 var ErrMissingExtension = errors.New("proto: missing extension")
49 // ExtensionRange represents a range of message extensions for a protocol buffer.
50 // Used in code generated by the protocol compiler.
51 type ExtensionRange struct {
52 Start, End int32 // both inclusive
55 // extendableProto is an interface implemented by any protocol buffer that may be extended.
56 type extendableProto interface {
58 ExtensionRangeArray() []ExtensionRange
61 type extensionsMap interface {
63 ExtensionMap() map[int32]Extension
66 type extensionsBytes interface {
68 GetExtensions() *[]byte
71 var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
73 // ExtensionDesc represents an extension specification.
74 // Used in generated code from the protocol compiler.
75 type ExtensionDesc struct {
76 ExtendedType Message // nil pointer to the type that is being extended
77 ExtensionType interface{} // nil pointer to the extension type
78 Field int32 // field number
79 Name string // fully-qualified name of extension, for text formatting
80 Tag string // protobuf tag style
83 func (ed *ExtensionDesc) repeated() bool {
84 t := reflect.TypeOf(ed.ExtensionType)
85 return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
88 // Extension represents an extension in a message.
89 type Extension struct {
90 // When an extension is stored in a message using SetExtension
91 // only desc and value are set. When the message is marshaled
92 // enc will be set to the encoded form of the message.
94 // When a message is unmarshaled and contains extensions, each
95 // extension will have only enc set. When such an extension is
96 // accessed using GetExtension (or GetExtensions) desc and value
103 // SetRawExtension is for testing only.
104 func SetRawExtension(base extendableProto, id int32, b []byte) {
105 if ebase, ok := base.(extensionsMap); ok {
106 ebase.ExtensionMap()[id] = Extension{enc: b}
107 } else if ebase, ok := base.(extensionsBytes); ok {
108 clearExtension(base, id)
109 ext := ebase.GetExtensions()
110 *ext = append(*ext, b...)
116 // isExtensionField returns true iff the given field number is in an extension range.
117 func isExtensionField(pb extendableProto, field int32) bool {
118 for _, er := range pb.ExtensionRangeArray() {
119 if er.Start <= field && field <= er.End {
126 // checkExtensionTypes checks that the given extension is valid for pb.
127 func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
128 // Check the extended type.
129 if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b {
130 return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
133 if !isExtensionField(pb, extension.Field) {
134 return errors.New("proto: bad extension number; not in declared ranges")
139 // extPropKey is sufficient to uniquely identify an extension.
140 type extPropKey struct {
145 var extProp = struct {
147 m map[extPropKey]*Properties
149 m: make(map[extPropKey]*Properties),
152 func extensionProperties(ed *ExtensionDesc) *Properties {
153 key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
156 if prop, ok := extProp.m[key]; ok {
163 defer extProp.Unlock()
165 if prop, ok := extProp.m[key]; ok {
169 prop := new(Properties)
170 prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
171 extProp.m[key] = prop
175 // encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m.
176 func encodeExtensionMap(m map[int32]Extension) error {
177 for k, e := range m {
178 err := encodeExtension(&e)
187 func encodeExtension(e *Extension) error {
188 if e.value == nil || e.desc == nil {
189 // Extension is only in its encoded form.
192 // We don't skip extensions that have an encoded form set,
193 // because the extension value may have been mutated after
194 // the last time this function was called.
196 et := reflect.TypeOf(e.desc.ExtensionType)
197 props := extensionProperties(e.desc)
200 // If e.value has type T, the encoder expects a *struct{ X T }.
201 // Pass a *T with a zero field and hope it all works out.
203 x.Elem().Set(reflect.ValueOf(e.value))
204 if err := props.enc(p, props, toStructPointer(x)); err != nil {
211 func sizeExtensionMap(m map[int32]Extension) (n int) {
212 for _, e := range m {
213 if e.value == nil || e.desc == nil {
214 // Extension is only in its encoded form.
219 // We don't skip extensions that have an encoded form set,
220 // because the extension value may have been mutated after
221 // the last time this function was called.
223 et := reflect.TypeOf(e.desc.ExtensionType)
224 props := extensionProperties(e.desc)
226 // If e.value has type T, the encoder expects a *struct{ X T }.
227 // Pass a *T with a zero field and hope it all works out.
229 x.Elem().Set(reflect.ValueOf(e.value))
230 n += props.size(props, toStructPointer(x))
235 // HasExtension returns whether the given extension is present in pb.
236 func HasExtension(pb extendableProto, extension *ExtensionDesc) bool {
237 // TODO: Check types, field numbers, etc.?
238 if epb, doki := pb.(extensionsMap); doki {
239 _, ok := epb.ExtensionMap()[extension.Field]
241 } else if epb, doki := pb.(extensionsBytes); doki {
242 ext := epb.GetExtensions()
246 tag, n := DecodeVarint(buf[o:])
247 fieldNum := int32(tag >> 3)
248 if int32(fieldNum) == extension.Field {
251 wireType := int(tag & 0x7)
253 l, err := size(buf[o:], wireType)
264 func deleteExtension(pb extensionsBytes, theFieldNum int32, offset int) int {
265 ext := pb.GetExtensions()
266 for offset < len(*ext) {
267 tag, n1 := DecodeVarint((*ext)[offset:])
268 fieldNum := int32(tag >> 3)
269 wireType := int(tag & 0x7)
270 n2, err := size((*ext)[offset+n1:], wireType)
274 newOffset := offset + n1 + n2
275 if fieldNum == theFieldNum {
276 *ext = append((*ext)[:offset], (*ext)[newOffset:]...)
284 func clearExtension(pb extendableProto, fieldNum int32) {
285 if epb, doki := pb.(extensionsMap); doki {
286 delete(epb.ExtensionMap(), fieldNum)
287 } else if epb, doki := pb.(extensionsBytes); doki {
290 offset = deleteExtension(epb, fieldNum, offset)
297 // ClearExtension removes the given extension from pb.
298 func ClearExtension(pb extendableProto, extension *ExtensionDesc) {
299 // TODO: Check types, field numbers, etc.?
300 clearExtension(pb, extension.Field)
303 // GetExtension parses and returns the given extension of pb.
304 // If the extension is not present it returns ErrMissingExtension.
305 func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) {
306 if err := checkExtensionTypes(pb, extension); err != nil {
310 if epb, doki := pb.(extensionsMap); doki {
311 emap := epb.ExtensionMap()
312 e, ok := emap[extension.Field]
314 // defaultExtensionValue returns the default value or
315 // ErrMissingExtension if there is no default.
316 return defaultExtensionValue(extension)
319 // Already decoded. Check the descriptor, though.
320 if e.desc != extension {
321 // This shouldn't happen. If it does, it means that
322 // GetExtension was called twice with two different
323 // descriptors with the same field number.
324 return nil, errors.New("proto: descriptor conflict")
329 v, err := decodeExtension(e.enc, extension)
334 // Remember the decoded version and drop the encoded version.
335 // That way it is safe to mutate what we return.
339 emap[extension.Field] = e
341 } else if epb, doki := pb.(extensionsBytes); doki {
342 ext := epb.GetExtensions()
345 tag, n := DecodeVarint((*ext)[o:])
346 fieldNum := int32(tag >> 3)
347 wireType := int(tag & 0x7)
348 l, err := size((*ext)[o+n:], wireType)
352 if int32(fieldNum) == extension.Field {
353 v, err := decodeExtension((*ext)[o:o+n+l], extension)
361 return defaultExtensionValue(extension)
366 // defaultExtensionValue returns the default value for extension.
367 // If no default for an extension is defined ErrMissingExtension is returned.
368 func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
369 t := reflect.TypeOf(extension.ExtensionType)
370 props := extensionProperties(extension)
372 sf, _, err := fieldDefault(t, props)
377 if sf == nil || sf.value == nil {
378 // There is no default value.
379 return nil, ErrMissingExtension
382 if t.Kind() != reflect.Ptr {
383 // We do not need to return a Ptr, we can directly return sf.value.
387 // We need to return an interface{} that is a pointer to sf.value.
388 value := reflect.New(t).Elem()
389 value.Set(reflect.New(value.Type().Elem()))
390 if sf.kind == reflect.Int32 {
391 // We may have an int32 or an enum, but the underlying data is int32.
392 // Since we can't set an int32 into a non int32 reflect.value directly
393 // set it as a int32.
394 value.Elem().SetInt(int64(sf.value.(int32)))
396 value.Elem().Set(reflect.ValueOf(sf.value))
398 return value.Interface(), nil
401 // decodeExtension decodes an extension encoded in b.
402 func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
405 t := reflect.TypeOf(extension.ExtensionType)
406 rep := extension.repeated()
408 props := extensionProperties(extension)
410 // t is a pointer to a struct, pointer to basic type or a slice.
411 // Allocate a "field" to store the pointer/slice itself; the
412 // pointer/slice will be stored here. We pass
413 // the address of this field to props.dec.
414 // This passes a zero field and a *t and lets props.dec
415 // interpret it as a *struct{ x t }.
416 value := reflect.New(t).Elem()
419 // Discard wire type and field number varint. It isn't needed.
420 if _, err := o.DecodeVarint(); err != nil {
424 if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil {
428 if !rep || o.index >= len(o.buf) {
432 return value.Interface(), nil
435 // GetExtensions returns a slice of the extensions present in pb that are also listed in es.
436 // The returned slice has the same length as es; missing extensions will appear as nil elements.
437 func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
438 epb, ok := pb.(extendableProto)
440 err = errors.New("proto: not an extendable proto")
443 extensions = make([]interface{}, len(es))
444 for i, e := range es {
445 extensions[i], err = GetExtension(epb, e)
446 if err == ErrMissingExtension {
456 // SetExtension sets the specified extension of pb to the specified value.
457 func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error {
458 if err := checkExtensionTypes(pb, extension); err != nil {
461 typ := reflect.TypeOf(extension.ExtensionType)
462 if typ != reflect.TypeOf(value) {
463 return errors.New("proto: bad extension value type")
465 // nil extension values need to be caught early, because the
466 // encoder can't distinguish an ErrNil due to a nil extension
467 // from an ErrNil due to a missing field. Extensions are
468 // always optional, so the encoder would just swallow the error
469 // and drop all the extensions from the encoded message.
470 if reflect.ValueOf(value).IsNil() {
471 return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
473 return setExtension(pb, extension, value)
476 func setExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error {
477 if epb, doki := pb.(extensionsMap); doki {
478 epb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value}
479 } else if epb, doki := pb.(extensionsBytes); doki {
480 ClearExtension(pb, extension)
481 ext := epb.GetExtensions()
482 et := reflect.TypeOf(extension.ExtensionType)
483 props := extensionProperties(extension)
486 x.Elem().Set(reflect.ValueOf(value))
487 if err := props.enc(p, props, toStructPointer(x)); err != nil {
490 *ext = append(*ext, p.buf...)
495 // A global registry of extensions.
496 // The generated code will register the generated descriptors by calling RegisterExtension.
498 var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
500 // RegisterExtension is called from the generated code.
501 func RegisterExtension(desc *ExtensionDesc) {
502 st := reflect.TypeOf(desc.ExtendedType).Elem()
503 m := extensionMaps[st]
505 m = make(map[int32]*ExtensionDesc)
506 extensionMaps[st] = m
508 if _, ok := m[desc.Field]; ok {
509 panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
514 // RegisteredExtensions returns a map of the registered extensions of a
515 // protocol buffer struct, indexed by the extension number.
516 // The argument pb should be a nil pointer to the struct type.
517 func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
518 return extensionMaps[reflect.TypeOf(pb).Elem()]