extensions.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. "fmt"
  38. "io"
  39. "reflect"
  40. "strconv"
  41. "sync"
  42. )
  43. // ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
  44. var ErrMissingExtension = errors.New("proto: missing extension")
  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. // extendableProto is an interface implemented by any protocol buffer generated by the current
  51. // proto compiler that may be extended.
  52. type extendableProto interface {
  53. Message
  54. ExtensionRangeArray() []ExtensionRange
  55. extensionsWrite() map[int32]Extension
  56. extensionsRead() (map[int32]Extension, sync.Locker)
  57. }
  58. // extendableProtoV1 is an interface implemented by a protocol buffer generated by the previous
  59. // version of the proto compiler that may be extended.
  60. type extendableProtoV1 interface {
  61. Message
  62. ExtensionRangeArray() []ExtensionRange
  63. ExtensionMap() map[int32]Extension
  64. }
  65. // extensionAdapter is a wrapper around extendableProtoV1 that implements extendableProto.
  66. type extensionAdapter struct {
  67. extendableProtoV1
  68. }
  69. func (e extensionAdapter) extensionsWrite() map[int32]Extension {
  70. return e.ExtensionMap()
  71. }
  72. func (e extensionAdapter) extensionsRead() (map[int32]Extension, sync.Locker) {
  73. return e.ExtensionMap(), notLocker{}
  74. }
  75. // notLocker is a sync.Locker whose Lock and Unlock methods are nops.
  76. type notLocker struct{}
  77. func (n notLocker) Lock() {}
  78. func (n notLocker) Unlock() {}
  79. // extendable returns the extendableProto interface for the given generated proto message.
  80. // If the proto message has the old extension format, it returns a wrapper that implements
  81. // the extendableProto interface.
  82. func extendable(p interface{}) (extendableProto, error) {
  83. switch p := p.(type) {
  84. case extendableProto:
  85. if isNilPtr(p) {
  86. return nil, fmt.Errorf("proto: nil %T is not extendable", p)
  87. }
  88. return p, nil
  89. case extendableProtoV1:
  90. if isNilPtr(p) {
  91. return nil, fmt.Errorf("proto: nil %T is not extendable", p)
  92. }
  93. return extensionAdapter{p}, nil
  94. case extensionsBytes:
  95. return slowExtensionAdapter{p}, nil
  96. }
  97. // Don't allocate a specific error containing %T:
  98. // this is the hot path for Clone and MarshalText.
  99. return nil, errNotExtendable
  100. }
  101. var errNotExtendable = errors.New("proto: not an extendable proto.Message")
  102. func isNilPtr(x interface{}) bool {
  103. v := reflect.ValueOf(x)
  104. return v.Kind() == reflect.Ptr && v.IsNil()
  105. }
  106. // XXX_InternalExtensions is an internal representation of proto extensions.
  107. //
  108. // Each generated message struct type embeds an anonymous XXX_InternalExtensions field,
  109. // thus gaining the unexported 'extensions' method, which can be called only from the proto package.
  110. //
  111. // The methods of XXX_InternalExtensions are not concurrency safe in general,
  112. // but calls to logically read-only methods such as has and get may be executed concurrently.
  113. type XXX_InternalExtensions struct {
  114. // The struct must be indirect so that if a user inadvertently copies a
  115. // generated message and its embedded XXX_InternalExtensions, they
  116. // avoid the mayhem of a copied mutex.
  117. //
  118. // The mutex serializes all logically read-only operations to p.extensionMap.
  119. // It is up to the client to ensure that write operations to p.extensionMap are
  120. // mutually exclusive with other accesses.
  121. p *struct {
  122. mu sync.Mutex
  123. extensionMap map[int32]Extension
  124. }
  125. }
  126. // extensionsWrite returns the extension map, creating it on first use.
  127. func (e *XXX_InternalExtensions) extensionsWrite() map[int32]Extension {
  128. if e.p == nil {
  129. e.p = new(struct {
  130. mu sync.Mutex
  131. extensionMap map[int32]Extension
  132. })
  133. e.p.extensionMap = make(map[int32]Extension)
  134. }
  135. return e.p.extensionMap
  136. }
  137. // extensionsRead returns the extensions map for read-only use. It may be nil.
  138. // The caller must hold the returned mutex's lock when accessing Elements within the map.
  139. func (e *XXX_InternalExtensions) extensionsRead() (map[int32]Extension, sync.Locker) {
  140. if e.p == nil {
  141. return nil, nil
  142. }
  143. return e.p.extensionMap, &e.p.mu
  144. }
  145. // ExtensionDesc represents an extension specification.
  146. // Used in generated code from the protocol compiler.
  147. type ExtensionDesc struct {
  148. ExtendedType Message // nil pointer to the type that is being extended
  149. ExtensionType interface{} // nil pointer to the extension type
  150. Field int32 // field number
  151. Name string // fully-qualified name of extension, for text formatting
  152. Tag string // protobuf tag style
  153. Filename string // name of the file in which the extension is defined
  154. }
  155. func (ed *ExtensionDesc) repeated() bool {
  156. t := reflect.TypeOf(ed.ExtensionType)
  157. return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
  158. }
  159. // Extension represents an extension in a message.
  160. type Extension struct {
  161. // When an extension is stored in a message using SetExtension
  162. // only desc and value are set. When the message is marshaled
  163. // enc will be set to the encoded form of the message.
  164. //
  165. // When a message is unmarshaled and contains extensions, each
  166. // extension will have only enc set. When such an extension is
  167. // accessed using GetExtension (or GetExtensions) desc and value
  168. // will be set.
  169. desc *ExtensionDesc
  170. value interface{}
  171. enc []byte
  172. }
  173. // SetRawExtension is for testing only.
  174. func SetRawExtension(base Message, id int32, b []byte) {
  175. if ebase, ok := base.(extensionsBytes); ok {
  176. clearExtension(base, id)
  177. ext := ebase.GetExtensions()
  178. *ext = append(*ext, b...)
  179. return
  180. }
  181. epb, err := extendable(base)
  182. if err != nil {
  183. return
  184. }
  185. extmap := epb.extensionsWrite()
  186. extmap[id] = Extension{enc: b}
  187. }
  188. // isExtensionField returns true iff the given field number is in an extension range.
  189. func isExtensionField(pb extendableProto, field int32) bool {
  190. for _, er := range pb.ExtensionRangeArray() {
  191. if er.Start <= field && field <= er.End {
  192. return true
  193. }
  194. }
  195. return false
  196. }
  197. // checkExtensionTypes checks that the given extension is valid for pb.
  198. func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
  199. var pbi interface{} = pb
  200. // Check the extended type.
  201. if ea, ok := pbi.(extensionAdapter); ok {
  202. pbi = ea.extendableProtoV1
  203. }
  204. if ea, ok := pbi.(slowExtensionAdapter); ok {
  205. pbi = ea.extensionsBytes
  206. }
  207. if a, b := reflect.TypeOf(pbi), reflect.TypeOf(extension.ExtendedType); a != b {
  208. return fmt.Errorf("proto: bad extended type; %v does not extend %v", b, a)
  209. }
  210. // Check the range.
  211. if !isExtensionField(pb, extension.Field) {
  212. return errors.New("proto: bad extension number; not in declared ranges")
  213. }
  214. return nil
  215. }
  216. // extPropKey is sufficient to uniquely identify an extension.
  217. type extPropKey struct {
  218. base reflect.Type
  219. field int32
  220. }
  221. var extProp = struct {
  222. sync.RWMutex
  223. m map[extPropKey]*Properties
  224. }{
  225. m: make(map[extPropKey]*Properties),
  226. }
  227. func extensionProperties(ed *ExtensionDesc) *Properties {
  228. key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
  229. extProp.RLock()
  230. if prop, ok := extProp.m[key]; ok {
  231. extProp.RUnlock()
  232. return prop
  233. }
  234. extProp.RUnlock()
  235. extProp.Lock()
  236. defer extProp.Unlock()
  237. // Check again.
  238. if prop, ok := extProp.m[key]; ok {
  239. return prop
  240. }
  241. prop := new(Properties)
  242. prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
  243. extProp.m[key] = prop
  244. return prop
  245. }
  246. // HasExtension returns whether the given extension is present in pb.
  247. func HasExtension(pb Message, extension *ExtensionDesc) bool {
  248. if epb, doki := pb.(extensionsBytes); doki {
  249. ext := epb.GetExtensions()
  250. buf := *ext
  251. o := 0
  252. for o < len(buf) {
  253. tag, n := DecodeVarint(buf[o:])
  254. fieldNum := int32(tag >> 3)
  255. if int32(fieldNum) == extension.Field {
  256. return true
  257. }
  258. wireType := int(tag & 0x7)
  259. o += n
  260. l, err := size(buf[o:], wireType)
  261. if err != nil {
  262. return false
  263. }
  264. o += l
  265. }
  266. return false
  267. }
  268. // TODO: Check types, field numbers, etc.?
  269. epb, err := extendable(pb)
  270. if err != nil {
  271. return false
  272. }
  273. extmap, mu := epb.extensionsRead()
  274. if extmap == nil {
  275. return false
  276. }
  277. mu.Lock()
  278. _, ok := extmap[extension.Field]
  279. mu.Unlock()
  280. return ok
  281. }
  282. // ClearExtension removes the given extension from pb.
  283. func ClearExtension(pb Message, extension *ExtensionDesc) {
  284. clearExtension(pb, extension.Field)
  285. }
  286. func clearExtension(pb Message, fieldNum int32) {
  287. if epb, ok := pb.(extensionsBytes); ok {
  288. offset := 0
  289. for offset != -1 {
  290. offset = deleteExtension(epb, fieldNum, offset)
  291. }
  292. return
  293. }
  294. epb, err := extendable(pb)
  295. if err != nil {
  296. return
  297. }
  298. // TODO: Check types, field numbers, etc.?
  299. extmap := epb.extensionsWrite()
  300. delete(extmap, fieldNum)
  301. }
  302. // GetExtension retrieves a proto2 extended field from pb.
  303. //
  304. // If the descriptor is type complete (i.e., ExtensionDesc.ExtensionType is non-nil),
  305. // then GetExtension parses the encoded field and returns a Go value of the specified type.
  306. // If the field is not present, then the default value is returned (if one is specified),
  307. // otherwise ErrMissingExtension is reported.
  308. //
  309. // If the descriptor is not type complete (i.e., ExtensionDesc.ExtensionType is nil),
  310. // then GetExtension returns the raw encoded bytes of the field extension.
  311. func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
  312. if epb, doki := pb.(extensionsBytes); doki {
  313. ext := epb.GetExtensions()
  314. return decodeExtensionFromBytes(extension, *ext)
  315. }
  316. epb, err := extendable(pb)
  317. if err != nil {
  318. return nil, err
  319. }
  320. if extension.ExtendedType != nil {
  321. // can only check type if this is a complete descriptor
  322. if cerr := checkExtensionTypes(epb, extension); cerr != nil {
  323. return nil, cerr
  324. }
  325. }
  326. emap, mu := epb.extensionsRead()
  327. if emap == nil {
  328. return defaultExtensionValue(extension)
  329. }
  330. mu.Lock()
  331. defer mu.Unlock()
  332. e, ok := emap[extension.Field]
  333. if !ok {
  334. // defaultExtensionValue returns the default value or
  335. // ErrMissingExtension if there is no default.
  336. return defaultExtensionValue(extension)
  337. }
  338. if e.value != nil {
  339. // Already decoded. Check the descriptor, though.
  340. if e.desc != extension {
  341. // This shouldn't happen. If it does, it means that
  342. // GetExtension was called twice with two different
  343. // descriptors with the same field number.
  344. return nil, errors.New("proto: descriptor conflict")
  345. }
  346. return e.value, nil
  347. }
  348. if extension.ExtensionType == nil {
  349. // incomplete descriptor
  350. return e.enc, nil
  351. }
  352. v, err := decodeExtension(e.enc, extension)
  353. if err != nil {
  354. return nil, err
  355. }
  356. // Remember the decoded version and drop the encoded version.
  357. // That way it is safe to mutate what we return.
  358. e.value = v
  359. e.desc = extension
  360. e.enc = nil
  361. emap[extension.Field] = e
  362. return e.value, nil
  363. }
  364. // defaultExtensionValue returns the default value for extension.
  365. // If no default for an extension is defined ErrMissingExtension is returned.
  366. func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
  367. if extension.ExtensionType == nil {
  368. // incomplete descriptor, so no default
  369. return nil, ErrMissingExtension
  370. }
  371. t := reflect.TypeOf(extension.ExtensionType)
  372. props := extensionProperties(extension)
  373. sf, _, err := fieldDefault(t, props)
  374. if err != nil {
  375. return nil, err
  376. }
  377. if sf == nil || sf.value == nil {
  378. // There is no default value.
  379. return nil, ErrMissingExtension
  380. }
  381. if t.Kind() != reflect.Ptr {
  382. // We do not need to return a Ptr, we can directly return sf.value.
  383. return sf.value, nil
  384. }
  385. // We need to return an interface{} that is a pointer to sf.value.
  386. value := reflect.New(t).Elem()
  387. value.Set(reflect.New(value.Type().Elem()))
  388. if sf.kind == reflect.Int32 {
  389. // We may have an int32 or an enum, but the underlying data is int32.
  390. // Since we can't set an int32 into a non int32 reflect.value directly
  391. // set it as a int32.
  392. value.Elem().SetInt(int64(sf.value.(int32)))
  393. } else {
  394. value.Elem().Set(reflect.ValueOf(sf.value))
  395. }
  396. return value.Interface(), nil
  397. }
  398. // decodeExtension decodes an extension encoded in b.
  399. func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
  400. t := reflect.TypeOf(extension.ExtensionType)
  401. unmarshal := typeUnmarshaler(t, extension.Tag)
  402. // t is a pointer to a struct, pointer to basic type or a slice.
  403. // Allocate space to store the pointer/slice.
  404. value := reflect.New(t).Elem()
  405. var err error
  406. for {
  407. x, n := decodeVarint(b)
  408. if n == 0 {
  409. return nil, io.ErrUnexpectedEOF
  410. }
  411. b = b[n:]
  412. wire := int(x) & 7
  413. b, err = unmarshal(b, valToPointer(value.Addr()), wire)
  414. if err != nil {
  415. return nil, err
  416. }
  417. if len(b) == 0 {
  418. break
  419. }
  420. }
  421. return value.Interface(), nil
  422. }
  423. // GetExtensions returns a slice of the extensions present in pb that are also listed in es.
  424. // The returned slice has the same length as es; missing extensions will appear as nil elements.
  425. func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
  426. epb, err := extendable(pb)
  427. if err != nil {
  428. return nil, err
  429. }
  430. extensions = make([]interface{}, len(es))
  431. for i, e := range es {
  432. extensions[i], err = GetExtension(epb, e)
  433. if err == ErrMissingExtension {
  434. err = nil
  435. }
  436. if err != nil {
  437. return
  438. }
  439. }
  440. return
  441. }
  442. // ExtensionDescs returns a new slice containing pb's extension descriptors, in undefined order.
  443. // For non-registered extensions, ExtensionDescs returns an incomplete descriptor containing
  444. // just the Field field, which defines the extension's field number.
  445. func ExtensionDescs(pb Message) ([]*ExtensionDesc, error) {
  446. epb, err := extendable(pb)
  447. if err != nil {
  448. return nil, err
  449. }
  450. registeredExtensions := RegisteredExtensions(pb)
  451. emap, mu := epb.extensionsRead()
  452. if emap == nil {
  453. return nil, nil
  454. }
  455. mu.Lock()
  456. defer mu.Unlock()
  457. extensions := make([]*ExtensionDesc, 0, len(emap))
  458. for extid, e := range emap {
  459. desc := e.desc
  460. if desc == nil {
  461. desc = registeredExtensions[extid]
  462. if desc == nil {
  463. desc = &ExtensionDesc{Field: extid}
  464. }
  465. }
  466. extensions = append(extensions, desc)
  467. }
  468. return extensions, nil
  469. }
  470. // SetExtension sets the specified extension of pb to the specified value.
  471. func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error {
  472. if epb, ok := pb.(extensionsBytes); ok {
  473. newb, err := encodeExtension(extension, value)
  474. if err != nil {
  475. return err
  476. }
  477. bb := epb.GetExtensions()
  478. *bb = append(*bb, newb...)
  479. return nil
  480. }
  481. epb, err := extendable(pb)
  482. if err != nil {
  483. return err
  484. }
  485. if err := checkExtensionTypes(epb, extension); err != nil {
  486. return err
  487. }
  488. typ := reflect.TypeOf(extension.ExtensionType)
  489. if typ != reflect.TypeOf(value) {
  490. return errors.New("proto: bad extension value type")
  491. }
  492. // nil extension values need to be caught early, because the
  493. // encoder can't distinguish an ErrNil due to a nil extension
  494. // from an ErrNil due to a missing field. Extensions are
  495. // always optional, so the encoder would just swallow the error
  496. // and drop all the extensions from the encoded message.
  497. if reflect.ValueOf(value).IsNil() {
  498. return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
  499. }
  500. extmap := epb.extensionsWrite()
  501. extmap[extension.Field] = Extension{desc: extension, value: value}
  502. return nil
  503. }
  504. // ClearAllExtensions clears all extensions from pb.
  505. func ClearAllExtensions(pb Message) {
  506. if epb, doki := pb.(extensionsBytes); doki {
  507. ext := epb.GetExtensions()
  508. *ext = []byte{}
  509. return
  510. }
  511. epb, err := extendable(pb)
  512. if err != nil {
  513. return
  514. }
  515. m := epb.extensionsWrite()
  516. for k := range m {
  517. delete(m, k)
  518. }
  519. }
  520. // A global registry of extensions.
  521. // The generated code will register the generated descriptors by calling RegisterExtension.
  522. var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
  523. // RegisterExtension is called from the generated code.
  524. func RegisterExtension(desc *ExtensionDesc) {
  525. st := reflect.TypeOf(desc.ExtendedType).Elem()
  526. m := extensionMaps[st]
  527. if m == nil {
  528. m = make(map[int32]*ExtensionDesc)
  529. extensionMaps[st] = m
  530. }
  531. if _, ok := m[desc.Field]; ok {
  532. panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
  533. }
  534. m[desc.Field] = desc
  535. }
  536. // RegisteredExtensions returns a map of the registered extensions of a
  537. // protocol buffer struct, indexed by the extension number.
  538. // The argument pb should be a nil pointer to the struct type.
  539. func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
  540. return extensionMaps[reflect.TypeOf(pb).Elem()]
  541. }