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