本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
3.6KB

  1. // Package bsoncodec provides a system for encoding values to BSON representations and decoding
  2. // values from BSON representations. This package considers both binary BSON and ExtendedJSON as
  3. // BSON representations. The types in this package enable a flexible system for handling this
  4. // encoding and decoding.
  5. //
  6. // The codec system is composed of two parts:
  7. //
  8. // 1) ValueEncoders and ValueDecoders that handle encoding and decoding Go values to and from BSON
  9. // representations.
  10. //
  11. // 2) A Registry that holds these ValueEncoders and ValueDecoders and provides methods for
  12. // retrieving them.
  13. //
  14. // ValueEncoders and ValueDecoders
  15. //
  16. // The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON.
  17. // The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the
  18. // EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc
  19. // is provided to allow use of a function with the correct signature as a ValueEncoder. An
  20. // EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and
  21. // to provide configuration information.
  22. //
  23. // The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that
  24. // the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to
  25. // allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext
  26. // instance is provided and serves similar functionality to the EncodeContext.
  27. //
  28. // Registry and RegistryBuilder
  29. //
  30. // A Registry is an immutable store for ValueEncoders, ValueDecoders, and a type map. For looking up
  31. // ValueEncoders and Decoders the Registry first attempts to find a ValueEncoder or ValueDecoder for
  32. // the type provided; if one cannot be found it then checks to see if a registered ValueEncoder or
  33. // ValueDecoder exists for an interface the type implements. Finally, the reflect.Kind of the type
  34. // is used to lookup a default ValueEncoder or ValueDecoder for that kind. If no ValueEncoder or
  35. // ValueDecoder can be found, an error is returned.
  36. //
  37. // The Registry also holds a type map. This allows users to retrieve the Go type that should be used
  38. // when decoding a BSON value into an empty interface. This is primarily only used for the empty
  39. // interface ValueDecoder.
  40. //
  41. // A RegistryBuilder is used to construct a Registry. The Register methods are used to associate
  42. // either a reflect.Type or a reflect.Kind with a ValueEncoder or ValueDecoder. A RegistryBuilder
  43. // returned from NewRegistryBuilder contains no registered ValueEncoders nor ValueDecoders and
  44. // contains an empty type map.
  45. //
  46. // The RegisterTypeMapEntry method handles associating a BSON type with a Go type. For example, if
  47. // you want to decode BSON int64 and int32 values into Go int instances, you would do the following:
  48. //
  49. // var regbuilder *RegistryBuilder = ... intType := reflect.TypeOf(int(0))
  50. // regbuilder.RegisterTypeMapEntry(bsontype.Int64, intType).RegisterTypeMapEntry(bsontype.Int32,
  51. // intType)
  52. //
  53. // DefaultValueEncoders and DefaultValueDecoders
  54. //
  55. // The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and
  56. // ValueDecoders for handling a wide range of Go types, including all of the types within the
  57. // primitive package. To make registering these codecs easier, a helper method on each type is
  58. // provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for
  59. // the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also
  60. // handles registering type map entries for each BSON type.
  61. package bsoncodec
上海开阖软件有限公司 沪ICP备12045867号-1