doc.go 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) MongoDB, Inc. 2022-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 provides a system for encoding values to BSON representations and decoding
  7. // values from BSON representations. This package considers both binary BSON and ExtendedJSON as
  8. // BSON representations. The types in this package enable a flexible system for handling this
  9. // encoding and decoding.
  10. //
  11. // The codec system is composed of two parts:
  12. //
  13. // 1) ValueEncoders and ValueDecoders that handle encoding and decoding Go values to and from BSON
  14. // representations.
  15. //
  16. // 2) A Registry that holds these ValueEncoders and ValueDecoders and provides methods for
  17. // retrieving them.
  18. //
  19. // # ValueEncoders and ValueDecoders
  20. //
  21. // The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON.
  22. // The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the
  23. // EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc
  24. // is provided to allow use of a function with the correct signature as a ValueEncoder. An
  25. // EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and
  26. // to provide configuration information.
  27. //
  28. // The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that
  29. // the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to
  30. // allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext
  31. // instance is provided and serves similar functionality to the EncodeContext.
  32. //
  33. // # Registry
  34. //
  35. // A Registry is a store for ValueEncoders, ValueDecoders, and a type map. See the Registry type
  36. // documentation for examples of registering various custom encoders and decoders. A Registry can
  37. // have three main types of codecs:
  38. //
  39. // 1. Type encoders/decoders - These can be registered using the RegisterTypeEncoder and
  40. // RegisterTypeDecoder methods. The registered codec will be invoked when encoding/decoding a value
  41. // whose type matches the registered type exactly.
  42. // If the registered type is an interface, the codec will be invoked when encoding or decoding
  43. // values whose type is the interface, but not for values with concrete types that implement the
  44. // interface.
  45. //
  46. // 2. Hook encoders/decoders - These can be registered using the RegisterHookEncoder and
  47. // RegisterHookDecoder methods. These methods only accept interface types and the registered codecs
  48. // will be invoked when encoding or decoding values whose types implement the interface. An example
  49. // of a hook defined by the driver is bson.Marshaler. The driver will call the MarshalBSON method
  50. // for any value whose type implements bson.Marshaler, regardless of the value's concrete type.
  51. //
  52. // 3. Type map entries - This can be used to associate a BSON type with a Go type. These type
  53. // associations are used when decoding into a bson.D/bson.M or a struct field of type interface{}.
  54. // For example, by default, BSON int32 and int64 values decode as Go int32 and int64 instances,
  55. // respectively, when decoding into a bson.D. The following code would change the behavior so these
  56. // values decode as Go int instances instead:
  57. //
  58. // intType := reflect.TypeOf(int(0))
  59. // registry.RegisterTypeMapEntry(bsontype.Int32, intType).RegisterTypeMapEntry(bsontype.Int64, intType)
  60. //
  61. // 4. Kind encoder/decoders - These can be registered using the RegisterDefaultEncoder and
  62. // RegisterDefaultDecoder methods. The registered codec will be invoked when encoding or decoding
  63. // values whose reflect.Kind matches the registered reflect.Kind as long as the value's type doesn't
  64. // match a registered type or hook encoder/decoder first. These methods should be used to change the
  65. // behavior for all values for a specific kind.
  66. //
  67. // # Registry Lookup Procedure
  68. //
  69. // When looking up an encoder in a Registry, the precedence rules are as follows:
  70. //
  71. // 1. A type encoder registered for the exact type of the value.
  72. //
  73. // 2. A hook encoder registered for an interface that is implemented by the value or by a pointer to
  74. // the value. If the value matches multiple hooks (e.g. the type implements bsoncodec.Marshaler and
  75. // bsoncodec.ValueMarshaler), the first one registered will be selected. Note that registries
  76. // constructed using bson.NewRegistry have driver-defined hooks registered for the
  77. // bsoncodec.Marshaler, bsoncodec.ValueMarshaler, and bsoncodec.Proxy interfaces, so those will take
  78. // precedence over any new hooks.
  79. //
  80. // 3. A kind encoder registered for the value's kind.
  81. //
  82. // If all of these lookups fail to find an encoder, an error of type ErrNoEncoder is returned. The
  83. // same precedence rules apply for decoders, with the exception that an error of type ErrNoDecoder
  84. // will be returned if no decoder is found.
  85. //
  86. // # DefaultValueEncoders and DefaultValueDecoders
  87. //
  88. // The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and
  89. // ValueDecoders for handling a wide range of Go types, including all of the types within the
  90. // primitive package. To make registering these codecs easier, a helper method on each type is
  91. // provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for
  92. // the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also
  93. // handles registering type map entries for each BSON type.
  94. package bsoncodec