|
Currently some of the deprecation notices in the bsoncodec package say something like:
Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the StructCodec registered.
However, frequently what users want to understand is how to translate code like:
import (
|
"reflect"
|
|
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
"go.mongodb.org/mongo-driver/bson/bsonoptions"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
)
|
|
func NewMongoClientOptions(mongoDBURI string) (*options.ClientOptions, error) {
|
// options copied from
|
// https://github.com/mongodb/mongo-go-driver/blob/master/bson/mgocompat/registry.go#L50-L55
|
opts := bsonoptions.StructCodec().
|
SetDecodeZeroStruct(true).
|
SetEncodeOmitDefaultStruct(true).
|
SetOverwriteDuplicatedInlinedFields(false).
|
SetAllowUnexportedFields(true)
|
|
structCodec, err := bsoncodec.NewStructCodec(bsoncodec.JSONFallbackStructTagParser, opts)
|
if err != nil {
|
return nil, err
|
}
|
|
// use default registry builder settings except for
|
// default encoder and decoder
|
registry := bson.NewRegistryBuilder().
|
RegisterDefaultDecoder(reflect.Struct, structCodec).
|
RegisterDefaultEncoder(reflect.Struct, structCodec).
|
Build()
|
|
return options.Client().ApplyURI(mongoDBURI).SetRegistry(registry), nil
|
}
|
into code using the new BSONOptions configuration, like:
import "go.mongodb.org/mongo-driver/mongo/options"
|
|
func NewMongoClientOptions(mongoDBURI string) (*options.ClientOptions, error) {
|
bsonOpts := &options.BSONOptions{
|
ZeroStructs: true,
|
OmitZeroStruct: true,
|
ErrorOnInlineDuplicates: true,
|
}
|
|
return options.Client().ApplyURI(mongoDBURI).SetBSONOptions(bsonOpts), nil
|
}
|
We should update the deprecation notices to more clearly indicate how to configure a Client with similar non-default encode/decode behavior.
See https://github.com/mongodb/mongo-go-driver/pull/1269 for the discussion about the bsoncodec and bsonoptions deprecation notice.
Definition of done:
- Update deprecation notices in the bsoncodec and bsonoptions packages to reference the replacement BSONOptions configuration options where relevant.
|