Skip to content

Commit 168c64d

Browse files
committed
Add documentation to the bsoncodec package
GODRIVER-581 Change-Id: Ie1869877f25b5596fdabaf5b55d7a7081b237f48
1 parent e247edb commit 168c64d

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

bson/bsoncodec/bsoncodec_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,44 @@
77
package bsoncodec
88

99
import (
10+
"fmt"
1011
"reflect"
1112
"testing"
1213

1314
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
15+
"github.com/mongodb/mongo-go-driver/bson/bsontype"
1416
"github.com/mongodb/mongo-go-driver/bson/primitive"
1517
)
1618

19+
func ExampleValueEncoder() {
20+
var _ ValueEncoderFunc = func(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
21+
if val.Kind() != reflect.String {
22+
return ValueEncoderError{Name: "StringEncodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
23+
}
24+
25+
return vw.WriteString(val.String())
26+
}
27+
}
28+
29+
func ExampleValueDecoder() {
30+
var _ ValueDecoderFunc = func(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
31+
if !val.CanSet() || val.Kind() != reflect.String {
32+
return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
33+
}
34+
35+
if vr.Type() != bsontype.String {
36+
return fmt.Errorf("cannot decode %v into a string type", vr.Type())
37+
}
38+
39+
str, err := vr.ReadString()
40+
if err != nil {
41+
return err
42+
}
43+
val.SetString(str)
44+
return nil
45+
}
46+
}
47+
1748
func noerr(t *testing.T, err error) {
1849
if err != nil {
1950
t.Helper()

bson/bsoncodec/doc.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

0 commit comments

Comments
 (0)