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