|
135 | 135 | //
|
136 | 136 | // Manually marshaling and unmarshaling can be done with the Marshal and Unmarshal family of functions.
|
137 | 137 | //
|
138 |
| -// --- |
139 |
| -// |
140 | 138 | // bsoncodec code provides a system for encoding values to BSON representations and decoding
|
141 | 139 | // values from BSON representations. This package considers both binary BSON and ExtendedJSON as
|
142 | 140 | // BSON representations. The types in this package enable a flexible system for handling this
|
|
150 | 148 | // 2) A Registry that holds these ValueEncoders and ValueDecoders and provides methods for
|
151 | 149 | // retrieving them.
|
152 | 150 | //
|
153 |
| -// # ValueEncoders and ValueDecoders |
154 |
| -// |
155 |
| -// The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON. |
156 |
| -// The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the |
157 |
| -// EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc |
158 |
| -// is provided to allow use of a function with the correct signature as a ValueEncoder. An |
159 |
| -// EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and |
160 |
| -// to provide configuration information. |
161 |
| -// |
162 |
| -// The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that |
163 |
| -// the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to |
164 |
| -// allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext |
165 |
| -// instance is provided and serves similar functionality to the EncodeContext. |
166 |
| -// |
167 |
| -// # Registry |
168 |
| -// |
169 |
| -// A Registry is a store for ValueEncoders, ValueDecoders, and a type map. See the Registry type |
170 |
| -// documentation for examples of registering various custom encoders and decoders. A Registry can |
171 |
| -// have three main types of codecs: |
172 |
| -// |
173 |
| -// 1. Type encoders/decoders - These can be registered using the RegisterTypeEncoder and |
174 |
| -// RegisterTypeDecoder methods. The registered codec will be invoked when encoding/decoding a value |
175 |
| -// whose type matches the registered type exactly. |
176 |
| -// If the registered type is an interface, the codec will be invoked when encoding or decoding |
177 |
| -// values whose type is the interface, but not for values with concrete types that implement the |
178 |
| -// interface. |
179 |
| -// |
180 |
| -// 2. Hook encoders/decoders - These can be registered using the RegisterHookEncoder and |
181 |
| -// RegisterHookDecoder methods. These methods only accept interface types and the registered codecs |
182 |
| -// will be invoked when encoding or decoding values whose types implement the interface. An example |
183 |
| -// of a hook defined by the driver is bson.Marshaler. The driver will call the MarshalBSON method |
184 |
| -// for any value whose type implements bson.Marshaler, regardless of the value's concrete type. |
185 |
| -// |
186 |
| -// 3. Type map entries - This can be used to associate a BSON type with a Go type. These type |
187 |
| -// associations are used when decoding into a bson.D/bson.M or a struct field of type interface{}. |
188 |
| -// For example, by default, BSON int32 and int64 values decode as Go int32 and int64 instances, |
189 |
| -// respectively, when decoding into a bson.D. The following code would change the behavior so these |
190 |
| -// values decode as Go int instances instead: |
191 |
| -// |
192 |
| -// intType := reflect.TypeOf(int(0)) |
193 |
| -// registry.RegisterTypeMapEntry(bson.TypeInt32, intType).RegisterTypeMapEntry(bson.TypeInt64, intType) |
194 |
| -// |
195 |
| -// 4. Kind encoder/decoders - These can be registered using the RegisterDefaultEncoder and |
196 |
| -// RegisterDefaultDecoder methods. The registered codec will be invoked when encoding or decoding |
197 |
| -// values whose reflect.Kind matches the registered reflect.Kind as long as the value's type doesn't |
198 |
| -// match a registered type or hook encoder/decoder first. These methods should be used to change the |
199 |
| -// behavior for all values for a specific kind. |
200 |
| -// |
201 |
| -// # Registry Lookup Procedure |
202 |
| -// |
203 |
| -// When looking up an encoder in a Registry, the precedence rules are as follows: |
204 |
| -// |
205 |
| -// 1. A type encoder registered for the exact type of the value. |
206 |
| -// |
207 |
| -// 2. A hook encoder registered for an interface that is implemented by the value or by a pointer to |
208 |
| -// the value. If the value matches multiple hooks (e.g. the type implements bsoncodec.Marshaler and |
209 |
| -// bsoncodec.ValueMarshaler), the first one registered will be selected. Note that registries |
210 |
| -// constructed using bson.NewRegistry have driver-defined hooks registered for the |
211 |
| -// bsoncodec.Marshaler, bsoncodec.ValueMarshaler, and bsoncodec.Proxy interfaces, so those will take |
212 |
| -// precedence over any new hooks. |
213 |
| -// |
214 |
| -// 3. A kind encoder registered for the value's kind. |
215 |
| -// |
216 |
| -// If all of these lookups fail to find an encoder, an error of type ErrNoEncoder is returned. The |
217 |
| -// same precedence rules apply for decoders, with the exception that an error of type ErrNoDecoder |
218 |
| -// will be returned if no decoder is found. |
219 |
| -// |
220 |
| -// # DefaultValueEncoders and DefaultValueDecoders |
221 |
| -// |
222 |
| -// The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and |
223 |
| -// ValueDecoders for handling a wide range of Go types, including all of the types within the |
224 |
| -// primitive package. To make registering these codecs easier, a helper method on each type is |
225 |
| -// provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for |
226 |
| -// the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also |
227 |
| -// handles registering type map entries for each BSON type. |
228 |
| -// |
229 | 151 | // [Work with BSON]: https://www.mongodb.com/docs/drivers/go/current/fundamentals/bson/
|
230 | 152 | package bson
|
0 commit comments