add vendor package
[oom/registrator.git] / kube2msb / src / vendor / github.com / gogo / protobuf / proto / extensions.go
1 // Go support for Protocol Buffers - Google's data interchange format
2 //
3 // Copyright 2010 The Go Authors.  All rights reserved.
4 // https://github.com/golang/protobuf
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
34 /*
35  * Types and routines for supporting protocol buffer extensions.
36  */
37
38 import (
39         "errors"
40         "fmt"
41         "reflect"
42         "strconv"
43         "sync"
44 )
45
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")
48
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
53 }
54
55 // extendableProto is an interface implemented by any protocol buffer that may be extended.
56 type extendableProto interface {
57         Message
58         ExtensionRangeArray() []ExtensionRange
59 }
60
61 type extensionsMap interface {
62         extendableProto
63         ExtensionMap() map[int32]Extension
64 }
65
66 type extensionsBytes interface {
67         extendableProto
68         GetExtensions() *[]byte
69 }
70
71 var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
72
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
81 }
82
83 func (ed *ExtensionDesc) repeated() bool {
84         t := reflect.TypeOf(ed.ExtensionType)
85         return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
86 }
87
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.
93         //
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
97         // will be set.
98         desc  *ExtensionDesc
99         value interface{}
100         enc   []byte
101 }
102
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...)
111         } else {
112                 panic("unreachable")
113         }
114 }
115
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 {
120                         return true
121                 }
122         }
123         return false
124 }
125
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())
131         }
132         // Check the range.
133         if !isExtensionField(pb, extension.Field) {
134                 return errors.New("proto: bad extension number; not in declared ranges")
135         }
136         return nil
137 }
138
139 // extPropKey is sufficient to uniquely identify an extension.
140 type extPropKey struct {
141         base  reflect.Type
142         field int32
143 }
144
145 var extProp = struct {
146         sync.RWMutex
147         m map[extPropKey]*Properties
148 }{
149         m: make(map[extPropKey]*Properties),
150 }
151
152 func extensionProperties(ed *ExtensionDesc) *Properties {
153         key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
154
155         extProp.RLock()
156         if prop, ok := extProp.m[key]; ok {
157                 extProp.RUnlock()
158                 return prop
159         }
160         extProp.RUnlock()
161
162         extProp.Lock()
163         defer extProp.Unlock()
164         // Check again.
165         if prop, ok := extProp.m[key]; ok {
166                 return prop
167         }
168
169         prop := new(Properties)
170         prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
171         extProp.m[key] = prop
172         return prop
173 }
174
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)
179                 if err != nil {
180                         return err
181                 }
182                 m[k] = e
183         }
184         return nil
185 }
186
187 func encodeExtension(e *Extension) error {
188         if e.value == nil || e.desc == nil {
189                 // Extension is only in its encoded form.
190                 return nil
191         }
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.
195
196         et := reflect.TypeOf(e.desc.ExtensionType)
197         props := extensionProperties(e.desc)
198
199         p := NewBuffer(nil)
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.
202         x := reflect.New(et)
203         x.Elem().Set(reflect.ValueOf(e.value))
204         if err := props.enc(p, props, toStructPointer(x)); err != nil {
205                 return err
206         }
207         e.enc = p.buf
208         return nil
209 }
210
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.
215                         n += len(e.enc)
216                         continue
217                 }
218
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.
222
223                 et := reflect.TypeOf(e.desc.ExtensionType)
224                 props := extensionProperties(e.desc)
225
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.
228                 x := reflect.New(et)
229                 x.Elem().Set(reflect.ValueOf(e.value))
230                 n += props.size(props, toStructPointer(x))
231         }
232         return
233 }
234
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]
240                 return ok
241         } else if epb, doki := pb.(extensionsBytes); doki {
242                 ext := epb.GetExtensions()
243                 buf := *ext
244                 o := 0
245                 for o < len(buf) {
246                         tag, n := DecodeVarint(buf[o:])
247                         fieldNum := int32(tag >> 3)
248                         if int32(fieldNum) == extension.Field {
249                                 return true
250                         }
251                         wireType := int(tag & 0x7)
252                         o += n
253                         l, err := size(buf[o:], wireType)
254                         if err != nil {
255                                 return false
256                         }
257                         o += l
258                 }
259                 return false
260         }
261         panic("unreachable")
262 }
263
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)
271                 if err != nil {
272                         panic(err)
273                 }
274                 newOffset := offset + n1 + n2
275                 if fieldNum == theFieldNum {
276                         *ext = append((*ext)[:offset], (*ext)[newOffset:]...)
277                         return offset
278                 }
279                 offset = newOffset
280         }
281         return -1
282 }
283
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 {
288                 offset := 0
289                 for offset != -1 {
290                         offset = deleteExtension(epb, fieldNum, offset)
291                 }
292         } else {
293                 panic("unreachable")
294         }
295 }
296
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)
301 }
302
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 {
307                 return nil, err
308         }
309
310         if epb, doki := pb.(extensionsMap); doki {
311                 emap := epb.ExtensionMap()
312                 e, ok := emap[extension.Field]
313                 if !ok {
314                         // defaultExtensionValue returns the default value or
315                         // ErrMissingExtension if there is no default.
316                         return defaultExtensionValue(extension)
317                 }
318                 if e.value != nil {
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")
325                         }
326                         return e.value, nil
327                 }
328
329                 v, err := decodeExtension(e.enc, extension)
330                 if err != nil {
331                         return nil, err
332                 }
333
334                 // Remember the decoded version and drop the encoded version.
335                 // That way it is safe to mutate what we return.
336                 e.value = v
337                 e.desc = extension
338                 e.enc = nil
339                 emap[extension.Field] = e
340                 return e.value, nil
341         } else if epb, doki := pb.(extensionsBytes); doki {
342                 ext := epb.GetExtensions()
343                 o := 0
344                 for o < len(*ext) {
345                         tag, n := DecodeVarint((*ext)[o:])
346                         fieldNum := int32(tag >> 3)
347                         wireType := int(tag & 0x7)
348                         l, err := size((*ext)[o+n:], wireType)
349                         if err != nil {
350                                 return nil, err
351                         }
352                         if int32(fieldNum) == extension.Field {
353                                 v, err := decodeExtension((*ext)[o:o+n+l], extension)
354                                 if err != nil {
355                                         return nil, err
356                                 }
357                                 return v, nil
358                         }
359                         o += n + l
360                 }
361                 return defaultExtensionValue(extension)
362         }
363         panic("unreachable")
364 }
365
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)
371
372         sf, _, err := fieldDefault(t, props)
373         if err != nil {
374                 return nil, err
375         }
376
377         if sf == nil || sf.value == nil {
378                 // There is no default value.
379                 return nil, ErrMissingExtension
380         }
381
382         if t.Kind() != reflect.Ptr {
383                 // We do not need to return a Ptr, we can directly return sf.value.
384                 return sf.value, nil
385         }
386
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)))
395         } else {
396                 value.Elem().Set(reflect.ValueOf(sf.value))
397         }
398         return value.Interface(), nil
399 }
400
401 // decodeExtension decodes an extension encoded in b.
402 func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
403         o := NewBuffer(b)
404
405         t := reflect.TypeOf(extension.ExtensionType)
406         rep := extension.repeated()
407
408         props := extensionProperties(extension)
409
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()
417
418         for {
419                 // Discard wire type and field number varint. It isn't needed.
420                 if _, err := o.DecodeVarint(); err != nil {
421                         return nil, err
422                 }
423
424                 if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil {
425                         return nil, err
426                 }
427
428                 if !rep || o.index >= len(o.buf) {
429                         break
430                 }
431         }
432         return value.Interface(), nil
433 }
434
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)
439         if !ok {
440                 err = errors.New("proto: not an extendable proto")
441                 return
442         }
443         extensions = make([]interface{}, len(es))
444         for i, e := range es {
445                 extensions[i], err = GetExtension(epb, e)
446                 if err == ErrMissingExtension {
447                         err = nil
448                 }
449                 if err != nil {
450                         return
451                 }
452         }
453         return
454 }
455
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 {
459                 return err
460         }
461         typ := reflect.TypeOf(extension.ExtensionType)
462         if typ != reflect.TypeOf(value) {
463                 return errors.New("proto: bad extension value type")
464         }
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)
472         }
473         return setExtension(pb, extension, value)
474 }
475
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)
484                 p := NewBuffer(nil)
485                 x := reflect.New(et)
486                 x.Elem().Set(reflect.ValueOf(value))
487                 if err := props.enc(p, props, toStructPointer(x)); err != nil {
488                         return err
489                 }
490                 *ext = append(*ext, p.buf...)
491         }
492         return nil
493 }
494
495 // A global registry of extensions.
496 // The generated code will register the generated descriptors by calling RegisterExtension.
497
498 var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
499
500 // RegisterExtension is called from the generated code.
501 func RegisterExtension(desc *ExtensionDesc) {
502         st := reflect.TypeOf(desc.ExtendedType).Elem()
503         m := extensionMaps[st]
504         if m == nil {
505                 m = make(map[int32]*ExtensionDesc)
506                 extensionMaps[st] = m
507         }
508         if _, ok := m[desc.Field]; ok {
509                 panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
510         }
511         m[desc.Field] = desc
512 }
513
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()]
519 }