registry.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bsoncodec
  7. import (
  8. "errors"
  9. "fmt"
  10. "reflect"
  11. "sync"
  12. "go.mongodb.org/mongo-driver/bson/bsontype"
  13. )
  14. // ErrNilType is returned when nil is passed to either LookupEncoder or LookupDecoder.
  15. //
  16. // Deprecated: ErrNilType will not be supported in Go Driver 2.0.
  17. var ErrNilType = errors.New("cannot perform a decoder lookup on <nil>")
  18. // ErrNotPointer is returned when a non-pointer type is provided to LookupDecoder.
  19. //
  20. // Deprecated: ErrNotPointer will not be supported in Go Driver 2.0.
  21. var ErrNotPointer = errors.New("non-pointer provided to LookupDecoder")
  22. // ErrNoEncoder is returned when there wasn't an encoder available for a type.
  23. //
  24. // Deprecated: ErrNoEncoder will not be supported in Go Driver 2.0.
  25. type ErrNoEncoder struct {
  26. Type reflect.Type
  27. }
  28. func (ene ErrNoEncoder) Error() string {
  29. if ene.Type == nil {
  30. return "no encoder found for <nil>"
  31. }
  32. return "no encoder found for " + ene.Type.String()
  33. }
  34. // ErrNoDecoder is returned when there wasn't a decoder available for a type.
  35. //
  36. // Deprecated: ErrNoDecoder will not be supported in Go Driver 2.0.
  37. type ErrNoDecoder struct {
  38. Type reflect.Type
  39. }
  40. func (end ErrNoDecoder) Error() string {
  41. return "no decoder found for " + end.Type.String()
  42. }
  43. // ErrNoTypeMapEntry is returned when there wasn't a type available for the provided BSON type.
  44. //
  45. // Deprecated: ErrNoTypeMapEntry will not be supported in Go Driver 2.0.
  46. type ErrNoTypeMapEntry struct {
  47. Type bsontype.Type
  48. }
  49. func (entme ErrNoTypeMapEntry) Error() string {
  50. return "no type map entry found for " + entme.Type.String()
  51. }
  52. // ErrNotInterface is returned when the provided type is not an interface.
  53. //
  54. // Deprecated: ErrNotInterface will not be supported in Go Driver 2.0.
  55. var ErrNotInterface = errors.New("The provided type is not an interface")
  56. // A RegistryBuilder is used to build a Registry. This type is not goroutine
  57. // safe.
  58. //
  59. // Deprecated: Use Registry instead.
  60. type RegistryBuilder struct {
  61. registry *Registry
  62. }
  63. // NewRegistryBuilder creates a new empty RegistryBuilder.
  64. //
  65. // Deprecated: Use NewRegistry instead.
  66. func NewRegistryBuilder() *RegistryBuilder {
  67. return &RegistryBuilder{
  68. registry: NewRegistry(),
  69. }
  70. }
  71. // RegisterCodec will register the provided ValueCodec for the provided type.
  72. //
  73. // Deprecated: Use Registry.RegisterTypeEncoder and Registry.RegisterTypeDecoder instead.
  74. func (rb *RegistryBuilder) RegisterCodec(t reflect.Type, codec ValueCodec) *RegistryBuilder {
  75. rb.RegisterTypeEncoder(t, codec)
  76. rb.RegisterTypeDecoder(t, codec)
  77. return rb
  78. }
  79. // RegisterTypeEncoder will register the provided ValueEncoder for the provided type.
  80. //
  81. // The type will be used directly, so an encoder can be registered for a type and a different encoder can be registered
  82. // for a pointer to that type.
  83. //
  84. // If the given type is an interface, the encoder will be called when marshaling a type that is that interface. It
  85. // will not be called when marshaling a non-interface type that implements the interface.
  86. //
  87. // Deprecated: Use Registry.RegisterTypeEncoder instead.
  88. func (rb *RegistryBuilder) RegisterTypeEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder {
  89. rb.registry.RegisterTypeEncoder(t, enc)
  90. return rb
  91. }
  92. // RegisterHookEncoder will register an encoder for the provided interface type t. This encoder will be called when
  93. // marshaling a type if the type implements t or a pointer to the type implements t. If the provided type is not
  94. // an interface (i.e. t.Kind() != reflect.Interface), this method will panic.
  95. //
  96. // Deprecated: Use Registry.RegisterInterfaceEncoder instead.
  97. func (rb *RegistryBuilder) RegisterHookEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder {
  98. rb.registry.RegisterInterfaceEncoder(t, enc)
  99. return rb
  100. }
  101. // RegisterTypeDecoder will register the provided ValueDecoder for the provided type.
  102. //
  103. // The type will be used directly, so a decoder can be registered for a type and a different decoder can be registered
  104. // for a pointer to that type.
  105. //
  106. // If the given type is an interface, the decoder will be called when unmarshaling into a type that is that interface.
  107. // It will not be called when unmarshaling into a non-interface type that implements the interface.
  108. //
  109. // Deprecated: Use Registry.RegisterTypeDecoder instead.
  110. func (rb *RegistryBuilder) RegisterTypeDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder {
  111. rb.registry.RegisterTypeDecoder(t, dec)
  112. return rb
  113. }
  114. // RegisterHookDecoder will register an decoder for the provided interface type t. This decoder will be called when
  115. // unmarshaling into a type if the type implements t or a pointer to the type implements t. If the provided type is not
  116. // an interface (i.e. t.Kind() != reflect.Interface), this method will panic.
  117. //
  118. // Deprecated: Use Registry.RegisterInterfaceDecoder instead.
  119. func (rb *RegistryBuilder) RegisterHookDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder {
  120. rb.registry.RegisterInterfaceDecoder(t, dec)
  121. return rb
  122. }
  123. // RegisterEncoder registers the provided type and encoder pair.
  124. //
  125. // Deprecated: Use Registry.RegisterTypeEncoder or Registry.RegisterInterfaceEncoder instead.
  126. func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder {
  127. if t == tEmpty {
  128. rb.registry.RegisterTypeEncoder(t, enc)
  129. return rb
  130. }
  131. switch t.Kind() {
  132. case reflect.Interface:
  133. rb.registry.RegisterInterfaceEncoder(t, enc)
  134. default:
  135. rb.registry.RegisterTypeEncoder(t, enc)
  136. }
  137. return rb
  138. }
  139. // RegisterDecoder registers the provided type and decoder pair.
  140. //
  141. // Deprecated: Use Registry.RegisterTypeDecoder or Registry.RegisterInterfaceDecoder instead.
  142. func (rb *RegistryBuilder) RegisterDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder {
  143. if t == nil {
  144. rb.registry.RegisterTypeDecoder(t, dec)
  145. return rb
  146. }
  147. if t == tEmpty {
  148. rb.registry.RegisterTypeDecoder(t, dec)
  149. return rb
  150. }
  151. switch t.Kind() {
  152. case reflect.Interface:
  153. rb.registry.RegisterInterfaceDecoder(t, dec)
  154. default:
  155. rb.registry.RegisterTypeDecoder(t, dec)
  156. }
  157. return rb
  158. }
  159. // RegisterDefaultEncoder will register the provided ValueEncoder to the provided
  160. // kind.
  161. //
  162. // Deprecated: Use Registry.RegisterKindEncoder instead.
  163. func (rb *RegistryBuilder) RegisterDefaultEncoder(kind reflect.Kind, enc ValueEncoder) *RegistryBuilder {
  164. rb.registry.RegisterKindEncoder(kind, enc)
  165. return rb
  166. }
  167. // RegisterDefaultDecoder will register the provided ValueDecoder to the
  168. // provided kind.
  169. //
  170. // Deprecated: Use Registry.RegisterKindDecoder instead.
  171. func (rb *RegistryBuilder) RegisterDefaultDecoder(kind reflect.Kind, dec ValueDecoder) *RegistryBuilder {
  172. rb.registry.RegisterKindDecoder(kind, dec)
  173. return rb
  174. }
  175. // RegisterTypeMapEntry will register the provided type to the BSON type. The primary usage for this
  176. // mapping is decoding situations where an empty interface is used and a default type needs to be
  177. // created and decoded into.
  178. //
  179. // By default, BSON documents will decode into interface{} values as bson.D. To change the default type for BSON
  180. // documents, a type map entry for bsontype.EmbeddedDocument should be registered. For example, to force BSON documents
  181. // to decode to bson.Raw, use the following code:
  182. //
  183. // rb.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.Raw{}))
  184. //
  185. // Deprecated: Use Registry.RegisterTypeMapEntry instead.
  186. func (rb *RegistryBuilder) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) *RegistryBuilder {
  187. rb.registry.RegisterTypeMapEntry(bt, rt)
  188. return rb
  189. }
  190. // Build creates a Registry from the current state of this RegistryBuilder.
  191. //
  192. // Deprecated: Use NewRegistry instead.
  193. func (rb *RegistryBuilder) Build() *Registry {
  194. registry := new(Registry)
  195. registry.typeEncoders = make(map[reflect.Type]ValueEncoder, len(rb.registry.typeEncoders))
  196. for t, enc := range rb.registry.typeEncoders {
  197. registry.typeEncoders[t] = enc
  198. }
  199. registry.typeDecoders = make(map[reflect.Type]ValueDecoder, len(rb.registry.typeDecoders))
  200. for t, dec := range rb.registry.typeDecoders {
  201. registry.typeDecoders[t] = dec
  202. }
  203. registry.interfaceEncoders = make([]interfaceValueEncoder, len(rb.registry.interfaceEncoders))
  204. copy(registry.interfaceEncoders, rb.registry.interfaceEncoders)
  205. registry.interfaceDecoders = make([]interfaceValueDecoder, len(rb.registry.interfaceDecoders))
  206. copy(registry.interfaceDecoders, rb.registry.interfaceDecoders)
  207. registry.kindEncoders = make(map[reflect.Kind]ValueEncoder)
  208. for kind, enc := range rb.registry.kindEncoders {
  209. registry.kindEncoders[kind] = enc
  210. }
  211. registry.kindDecoders = make(map[reflect.Kind]ValueDecoder)
  212. for kind, dec := range rb.registry.kindDecoders {
  213. registry.kindDecoders[kind] = dec
  214. }
  215. registry.typeMap = make(map[bsontype.Type]reflect.Type)
  216. for bt, rt := range rb.registry.typeMap {
  217. registry.typeMap[bt] = rt
  218. }
  219. return registry
  220. }
  221. // A Registry is used to store and retrieve codecs for types and interfaces. This type is the main
  222. // typed passed around and Encoders and Decoders are constructed from it.
  223. type Registry struct {
  224. typeEncoders map[reflect.Type]ValueEncoder
  225. typeDecoders map[reflect.Type]ValueDecoder
  226. interfaceEncoders []interfaceValueEncoder
  227. interfaceDecoders []interfaceValueDecoder
  228. kindEncoders map[reflect.Kind]ValueEncoder
  229. kindDecoders map[reflect.Kind]ValueDecoder
  230. typeMap map[bsontype.Type]reflect.Type
  231. mu sync.RWMutex
  232. }
  233. // NewRegistry creates a new empty Registry.
  234. func NewRegistry() *Registry {
  235. return &Registry{
  236. typeEncoders: make(map[reflect.Type]ValueEncoder),
  237. typeDecoders: make(map[reflect.Type]ValueDecoder),
  238. interfaceEncoders: make([]interfaceValueEncoder, 0),
  239. interfaceDecoders: make([]interfaceValueDecoder, 0),
  240. kindEncoders: make(map[reflect.Kind]ValueEncoder),
  241. kindDecoders: make(map[reflect.Kind]ValueDecoder),
  242. typeMap: make(map[bsontype.Type]reflect.Type),
  243. }
  244. }
  245. // RegisterTypeEncoder registers the provided ValueEncoder for the provided type.
  246. //
  247. // The type will be used as provided, so an encoder can be registered for a type and a different
  248. // encoder can be registered for a pointer to that type.
  249. //
  250. // If the given type is an interface, the encoder will be called when marshaling a type that is
  251. // that interface. It will not be called when marshaling a non-interface type that implements the
  252. // interface. To get the latter behavior, call RegisterHookEncoder instead.
  253. //
  254. // RegisterTypeEncoder should not be called concurrently with any other Registry method.
  255. func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder) {
  256. r.typeEncoders[valueType] = enc
  257. }
  258. // RegisterTypeDecoder registers the provided ValueDecoder for the provided type.
  259. //
  260. // The type will be used as provided, so a decoder can be registered for a type and a different
  261. // decoder can be registered for a pointer to that type.
  262. //
  263. // If the given type is an interface, the decoder will be called when unmarshaling into a type that
  264. // is that interface. It will not be called when unmarshaling into a non-interface type that
  265. // implements the interface. To get the latter behavior, call RegisterHookDecoder instead.
  266. //
  267. // RegisterTypeDecoder should not be called concurrently with any other Registry method.
  268. func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder) {
  269. r.typeDecoders[valueType] = dec
  270. }
  271. // RegisterKindEncoder registers the provided ValueEncoder for the provided kind.
  272. //
  273. // Use RegisterKindEncoder to register an encoder for any type with the same underlying kind. For
  274. // example, consider the type MyInt defined as
  275. //
  276. // type MyInt int32
  277. //
  278. // To define an encoder for MyInt and int32, use RegisterKindEncoder like
  279. //
  280. // reg.RegisterKindEncoder(reflect.Int32, myEncoder)
  281. //
  282. // RegisterKindEncoder should not be called concurrently with any other Registry method.
  283. func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder) {
  284. r.kindEncoders[kind] = enc
  285. }
  286. // RegisterKindDecoder registers the provided ValueDecoder for the provided kind.
  287. //
  288. // Use RegisterKindDecoder to register a decoder for any type with the same underlying kind. For
  289. // example, consider the type MyInt defined as
  290. //
  291. // type MyInt int32
  292. //
  293. // To define an decoder for MyInt and int32, use RegisterKindDecoder like
  294. //
  295. // reg.RegisterKindDecoder(reflect.Int32, myDecoder)
  296. //
  297. // RegisterKindDecoder should not be called concurrently with any other Registry method.
  298. func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder) {
  299. r.kindDecoders[kind] = dec
  300. }
  301. // RegisterInterfaceEncoder registers an encoder for the provided interface type iface. This encoder will
  302. // be called when marshaling a type if the type implements iface or a pointer to the type
  303. // implements iface. If the provided type is not an interface
  304. // (i.e. iface.Kind() != reflect.Interface), this method will panic.
  305. //
  306. // RegisterInterfaceEncoder should not be called concurrently with any other Registry method.
  307. func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc ValueEncoder) {
  308. if iface.Kind() != reflect.Interface {
  309. panicStr := fmt.Errorf("RegisterInterfaceEncoder expects a type with kind reflect.Interface, "+
  310. "got type %s with kind %s", iface, iface.Kind())
  311. panic(panicStr)
  312. }
  313. for idx, encoder := range r.interfaceEncoders {
  314. if encoder.i == iface {
  315. r.interfaceEncoders[idx].ve = enc
  316. return
  317. }
  318. }
  319. r.interfaceEncoders = append(r.interfaceEncoders, interfaceValueEncoder{i: iface, ve: enc})
  320. }
  321. // RegisterInterfaceDecoder registers an decoder for the provided interface type iface. This decoder will
  322. // be called when unmarshaling into a type if the type implements iface or a pointer to the type
  323. // implements iface. If the provided type is not an interface (i.e. iface.Kind() != reflect.Interface),
  324. // this method will panic.
  325. //
  326. // RegisterInterfaceDecoder should not be called concurrently with any other Registry method.
  327. func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec ValueDecoder) {
  328. if iface.Kind() != reflect.Interface {
  329. panicStr := fmt.Errorf("RegisterInterfaceDecoder expects a type with kind reflect.Interface, "+
  330. "got type %s with kind %s", iface, iface.Kind())
  331. panic(panicStr)
  332. }
  333. for idx, decoder := range r.interfaceDecoders {
  334. if decoder.i == iface {
  335. r.interfaceDecoders[idx].vd = dec
  336. return
  337. }
  338. }
  339. r.interfaceDecoders = append(r.interfaceDecoders, interfaceValueDecoder{i: iface, vd: dec})
  340. }
  341. // RegisterTypeMapEntry will register the provided type to the BSON type. The primary usage for this
  342. // mapping is decoding situations where an empty interface is used and a default type needs to be
  343. // created and decoded into.
  344. //
  345. // By default, BSON documents will decode into interface{} values as bson.D. To change the default type for BSON
  346. // documents, a type map entry for bsontype.EmbeddedDocument should be registered. For example, to force BSON documents
  347. // to decode to bson.Raw, use the following code:
  348. //
  349. // reg.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.Raw{}))
  350. func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) {
  351. r.typeMap[bt] = rt
  352. }
  353. // LookupEncoder returns the first matching encoder in the Registry. It uses the following lookup
  354. // order:
  355. //
  356. // 1. An encoder registered for the exact type. If the given type is an interface, an encoder
  357. // registered using RegisterTypeEncoder for that interface will be selected.
  358. //
  359. // 2. An encoder registered using RegisterInterfaceEncoder for an interface implemented by the type
  360. // or by a pointer to the type.
  361. //
  362. // 3. An encoder registered using RegisterKindEncoder for the kind of value.
  363. //
  364. // If no encoder is found, an error of type ErrNoEncoder is returned. LookupEncoder is safe for
  365. // concurrent use by multiple goroutines after all codecs and encoders are registered.
  366. func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
  367. r.mu.RLock()
  368. enc, found := r.lookupTypeEncoder(valueType)
  369. r.mu.RUnlock()
  370. if found {
  371. if enc == nil {
  372. return nil, ErrNoEncoder{Type: valueType}
  373. }
  374. return enc, nil
  375. }
  376. enc, found = r.lookupInterfaceEncoder(valueType, true)
  377. if found {
  378. r.mu.Lock()
  379. r.typeEncoders[valueType] = enc
  380. r.mu.Unlock()
  381. return enc, nil
  382. }
  383. if valueType == nil {
  384. r.mu.Lock()
  385. r.typeEncoders[valueType] = nil
  386. r.mu.Unlock()
  387. return nil, ErrNoEncoder{Type: valueType}
  388. }
  389. enc, found = r.kindEncoders[valueType.Kind()]
  390. if !found {
  391. r.mu.Lock()
  392. r.typeEncoders[valueType] = nil
  393. r.mu.Unlock()
  394. return nil, ErrNoEncoder{Type: valueType}
  395. }
  396. r.mu.Lock()
  397. r.typeEncoders[valueType] = enc
  398. r.mu.Unlock()
  399. return enc, nil
  400. }
  401. func (r *Registry) lookupTypeEncoder(valueType reflect.Type) (ValueEncoder, bool) {
  402. enc, found := r.typeEncoders[valueType]
  403. return enc, found
  404. }
  405. func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool) (ValueEncoder, bool) {
  406. if valueType == nil {
  407. return nil, false
  408. }
  409. for _, ienc := range r.interfaceEncoders {
  410. if valueType.Implements(ienc.i) {
  411. return ienc.ve, true
  412. }
  413. if allowAddr && valueType.Kind() != reflect.Ptr && reflect.PtrTo(valueType).Implements(ienc.i) {
  414. // if *t implements an interface, this will catch if t implements an interface further
  415. // ahead in interfaceEncoders
  416. defaultEnc, found := r.lookupInterfaceEncoder(valueType, false)
  417. if !found {
  418. defaultEnc = r.kindEncoders[valueType.Kind()]
  419. }
  420. return newCondAddrEncoder(ienc.ve, defaultEnc), true
  421. }
  422. }
  423. return nil, false
  424. }
  425. // LookupDecoder returns the first matching decoder in the Registry. It uses the following lookup
  426. // order:
  427. //
  428. // 1. A decoder registered for the exact type. If the given type is an interface, a decoder
  429. // registered using RegisterTypeDecoder for that interface will be selected.
  430. //
  431. // 2. A decoder registered using RegisterInterfaceDecoder for an interface implemented by the type or by
  432. // a pointer to the type.
  433. //
  434. // 3. A decoder registered using RegisterKindDecoder for the kind of value.
  435. //
  436. // If no decoder is found, an error of type ErrNoDecoder is returned. LookupDecoder is safe for
  437. // concurrent use by multiple goroutines after all codecs and decoders are registered.
  438. func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) {
  439. if valueType == nil {
  440. return nil, ErrNilType
  441. }
  442. decodererr := ErrNoDecoder{Type: valueType}
  443. r.mu.RLock()
  444. dec, found := r.lookupTypeDecoder(valueType)
  445. r.mu.RUnlock()
  446. if found {
  447. if dec == nil {
  448. return nil, ErrNoDecoder{Type: valueType}
  449. }
  450. return dec, nil
  451. }
  452. dec, found = r.lookupInterfaceDecoder(valueType, true)
  453. if found {
  454. r.mu.Lock()
  455. r.typeDecoders[valueType] = dec
  456. r.mu.Unlock()
  457. return dec, nil
  458. }
  459. dec, found = r.kindDecoders[valueType.Kind()]
  460. if !found {
  461. r.mu.Lock()
  462. r.typeDecoders[valueType] = nil
  463. r.mu.Unlock()
  464. return nil, decodererr
  465. }
  466. r.mu.Lock()
  467. r.typeDecoders[valueType] = dec
  468. r.mu.Unlock()
  469. return dec, nil
  470. }
  471. func (r *Registry) lookupTypeDecoder(valueType reflect.Type) (ValueDecoder, bool) {
  472. dec, found := r.typeDecoders[valueType]
  473. return dec, found
  474. }
  475. func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool) (ValueDecoder, bool) {
  476. for _, idec := range r.interfaceDecoders {
  477. if valueType.Implements(idec.i) {
  478. return idec.vd, true
  479. }
  480. if allowAddr && valueType.Kind() != reflect.Ptr && reflect.PtrTo(valueType).Implements(idec.i) {
  481. // if *t implements an interface, this will catch if t implements an interface further
  482. // ahead in interfaceDecoders
  483. defaultDec, found := r.lookupInterfaceDecoder(valueType, false)
  484. if !found {
  485. defaultDec = r.kindDecoders[valueType.Kind()]
  486. }
  487. return newCondAddrDecoder(idec.vd, defaultDec), true
  488. }
  489. }
  490. return nil, false
  491. }
  492. // LookupTypeMapEntry inspects the registry's type map for a Go type for the corresponding BSON
  493. // type. If no type is found, ErrNoTypeMapEntry is returned.
  494. //
  495. // LookupTypeMapEntry should not be called concurrently with any other Registry method.
  496. func (r *Registry) LookupTypeMapEntry(bt bsontype.Type) (reflect.Type, error) {
  497. t, ok := r.typeMap[bt]
  498. if !ok || t == nil {
  499. return nil, ErrNoTypeMapEntry{Type: bt}
  500. }
  501. return t, nil
  502. }
  503. type interfaceValueEncoder struct {
  504. i reflect.Type
  505. ve ValueEncoder
  506. }
  507. type interfaceValueDecoder struct {
  508. i reflect.Type
  509. vd ValueDecoder
  510. }