PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/scale-demo/Godeps/_workspace/src/github.com/golang/protobuf/proto/extensions.go

https://gitlab.com/unofficial-mirrors/kubernetes-contrib
Go | 353 lines | 209 code | 47 blank | 97 comment | 53 complexity | e86e2fd9ae1ef438b36c5ff40e631cdf MD5 | raw file
  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. package proto
  32. /*
  33. * Types and routines for supporting protocol buffer extensions.
  34. */
  35. import (
  36. "errors"
  37. "reflect"
  38. "strconv"
  39. "sync"
  40. )
  41. // ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
  42. var ErrMissingExtension = errors.New("proto: missing extension")
  43. // ExtensionRange represents a range of message extensions for a protocol buffer.
  44. // Used in code generated by the protocol compiler.
  45. type ExtensionRange struct {
  46. Start, End int32 // both inclusive
  47. }
  48. // extendableProto is an interface implemented by any protocol buffer that may be extended.
  49. type extendableProto interface {
  50. Message
  51. ExtensionRangeArray() []ExtensionRange
  52. ExtensionMap() map[int32]Extension
  53. }
  54. var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
  55. // ExtensionDesc represents an extension specification.
  56. // Used in generated code from the protocol compiler.
  57. type ExtensionDesc struct {
  58. ExtendedType Message // nil pointer to the type that is being extended
  59. ExtensionType interface{} // nil pointer to the extension type
  60. Field int32 // field number
  61. Name string // fully-qualified name of extension, for text formatting
  62. Tag string // protobuf tag style
  63. }
  64. func (ed *ExtensionDesc) repeated() bool {
  65. t := reflect.TypeOf(ed.ExtensionType)
  66. return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
  67. }
  68. // Extension represents an extension in a message.
  69. type Extension struct {
  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. desc *ExtensionDesc
  79. value interface{}
  80. enc []byte
  81. }
  82. // SetRawExtension is for testing only.
  83. func SetRawExtension(base extendableProto, id int32, b []byte) {
  84. base.ExtensionMap()[id] = Extension{enc: b}
  85. }
  86. // isExtensionField returns true iff the given field number is in an extension range.
  87. func isExtensionField(pb extendableProto, field int32) bool {
  88. for _, er := range pb.ExtensionRangeArray() {
  89. if er.Start <= field && field <= er.End {
  90. return true
  91. }
  92. }
  93. return false
  94. }
  95. // checkExtensionTypes checks that the given extension is valid for pb.
  96. func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
  97. // Check the extended type.
  98. if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b {
  99. return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
  100. }
  101. // Check the range.
  102. if !isExtensionField(pb, extension.Field) {
  103. return errors.New("proto: bad extension number; not in declared ranges")
  104. }
  105. return nil
  106. }
  107. // extPropKey is sufficient to uniquely identify an extension.
  108. type extPropKey struct {
  109. base reflect.Type
  110. field int32
  111. }
  112. var extProp = struct {
  113. sync.RWMutex
  114. m map[extPropKey]*Properties
  115. }{
  116. m: make(map[extPropKey]*Properties),
  117. }
  118. func extensionProperties(ed *ExtensionDesc) *Properties {
  119. key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
  120. extProp.RLock()
  121. if prop, ok := extProp.m[key]; ok {
  122. extProp.RUnlock()
  123. return prop
  124. }
  125. extProp.RUnlock()
  126. extProp.Lock()
  127. defer extProp.Unlock()
  128. // Check again.
  129. if prop, ok := extProp.m[key]; ok {
  130. return prop
  131. }
  132. prop := new(Properties)
  133. prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
  134. extProp.m[key] = prop
  135. return prop
  136. }
  137. // encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m.
  138. func encodeExtensionMap(m map[int32]Extension) error {
  139. for k, e := range m {
  140. if e.value == nil || e.desc == nil {
  141. // Extension is only in its encoded form.
  142. continue
  143. }
  144. // We don't skip extensions that have an encoded form set,
  145. // because the extension value may have been mutated after
  146. // the last time this function was called.
  147. et := reflect.TypeOf(e.desc.ExtensionType)
  148. props := extensionProperties(e.desc)
  149. p := NewBuffer(nil)
  150. // If e.value has type T, the encoder expects a *struct{ X T }.
  151. // Pass a *T with a zero field and hope it all works out.
  152. x := reflect.New(et)
  153. x.Elem().Set(reflect.ValueOf(e.value))
  154. if err := props.enc(p, props, toStructPointer(x)); err != nil {
  155. return err
  156. }
  157. e.enc = p.buf
  158. m[k] = e
  159. }
  160. return nil
  161. }
  162. func sizeExtensionMap(m map[int32]Extension) (n int) {
  163. for _, e := range m {
  164. if e.value == nil || e.desc == nil {
  165. // Extension is only in its encoded form.
  166. n += len(e.enc)
  167. continue
  168. }
  169. // We don't skip extensions that have an encoded form set,
  170. // because the extension value may have been mutated after
  171. // the last time this function was called.
  172. et := reflect.TypeOf(e.desc.ExtensionType)
  173. props := extensionProperties(e.desc)
  174. // If e.value has type T, the encoder expects a *struct{ X T }.
  175. // Pass a *T with a zero field and hope it all works out.
  176. x := reflect.New(et)
  177. x.Elem().Set(reflect.ValueOf(e.value))
  178. n += props.size(props, toStructPointer(x))
  179. }
  180. return
  181. }
  182. // HasExtension returns whether the given extension is present in pb.
  183. func HasExtension(pb extendableProto, extension *ExtensionDesc) bool {
  184. // TODO: Check types, field numbers, etc.?
  185. _, ok := pb.ExtensionMap()[extension.Field]
  186. return ok
  187. }
  188. // ClearExtension removes the given extension from pb.
  189. func ClearExtension(pb extendableProto, extension *ExtensionDesc) {
  190. // TODO: Check types, field numbers, etc.?
  191. delete(pb.ExtensionMap(), extension.Field)
  192. }
  193. // GetExtension parses and returns the given extension of pb.
  194. // If the extension is not present it returns ErrMissingExtension.
  195. func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) {
  196. if err := checkExtensionTypes(pb, extension); err != nil {
  197. return nil, err
  198. }
  199. emap := pb.ExtensionMap()
  200. e, ok := emap[extension.Field]
  201. if !ok {
  202. return nil, ErrMissingExtension
  203. }
  204. if e.value != nil {
  205. // Already decoded. Check the descriptor, though.
  206. if e.desc != extension {
  207. // This shouldn't happen. If it does, it means that
  208. // GetExtension was called twice with two different
  209. // descriptors with the same field number.
  210. return nil, errors.New("proto: descriptor conflict")
  211. }
  212. return e.value, nil
  213. }
  214. v, err := decodeExtension(e.enc, extension)
  215. if err != nil {
  216. return nil, err
  217. }
  218. // Remember the decoded version and drop the encoded version.
  219. // That way it is safe to mutate what we return.
  220. e.value = v
  221. e.desc = extension
  222. e.enc = nil
  223. emap[extension.Field] = e
  224. return e.value, nil
  225. }
  226. // decodeExtension decodes an extension encoded in b.
  227. func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
  228. o := NewBuffer(b)
  229. t := reflect.TypeOf(extension.ExtensionType)
  230. rep := extension.repeated()
  231. props := extensionProperties(extension)
  232. // t is a pointer to a struct, pointer to basic type or a slice.
  233. // Allocate a "field" to store the pointer/slice itself; the
  234. // pointer/slice will be stored here. We pass
  235. // the address of this field to props.dec.
  236. // This passes a zero field and a *t and lets props.dec
  237. // interpret it as a *struct{ x t }.
  238. value := reflect.New(t).Elem()
  239. for {
  240. // Discard wire type and field number varint. It isn't needed.
  241. if _, err := o.DecodeVarint(); err != nil {
  242. return nil, err
  243. }
  244. if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil {
  245. return nil, err
  246. }
  247. if !rep || o.index >= len(o.buf) {
  248. break
  249. }
  250. }
  251. return value.Interface(), nil
  252. }
  253. // GetExtensions returns a slice of the extensions present in pb that are also listed in es.
  254. // The returned slice has the same length as es; missing extensions will appear as nil elements.
  255. func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
  256. epb, ok := pb.(extendableProto)
  257. if !ok {
  258. err = errors.New("proto: not an extendable proto")
  259. return
  260. }
  261. extensions = make([]interface{}, len(es))
  262. for i, e := range es {
  263. extensions[i], err = GetExtension(epb, e)
  264. if err == ErrMissingExtension {
  265. err = nil
  266. }
  267. if err != nil {
  268. return
  269. }
  270. }
  271. return
  272. }
  273. // SetExtension sets the specified extension of pb to the specified value.
  274. func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error {
  275. if err := checkExtensionTypes(pb, extension); err != nil {
  276. return err
  277. }
  278. typ := reflect.TypeOf(extension.ExtensionType)
  279. if typ != reflect.TypeOf(value) {
  280. return errors.New("proto: bad extension value type")
  281. }
  282. pb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value}
  283. return nil
  284. }
  285. // A global registry of extensions.
  286. // The generated code will register the generated descriptors by calling RegisterExtension.
  287. var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
  288. // RegisterExtension is called from the generated code.
  289. func RegisterExtension(desc *ExtensionDesc) {
  290. st := reflect.TypeOf(desc.ExtendedType).Elem()
  291. m := extensionMaps[st]
  292. if m == nil {
  293. m = make(map[int32]*ExtensionDesc)
  294. extensionMaps[st] = m
  295. }
  296. if _, ok := m[desc.Field]; ok {
  297. panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
  298. }
  299. m[desc.Field] = desc
  300. }
  301. // RegisteredExtensions returns a map of the registered extensions of a
  302. // protocol buffer struct, indexed by the extension number.
  303. // The argument pb should be a nil pointer to the struct type.
  304. func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
  305. return extensionMaps[reflect.TypeOf(pb).Elem()]
  306. }