[GODRIVER-2886] Link to new "BSONOptions" config from BSON registry deprecation notices Created: 30/Jun/23  Updated: 08/Feb/24

Status: In Code Review
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.12.0
Fix Version/s: 1.13.2

Type: Improvement Priority: Major - P3
Reporter: Matt Dale Assignee: Matt Dale
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.


 Comments   
Comment by Matt Dale [ 08/Feb/24 ]

PR: https://github.com/mongodb/mongo-go-driver/pull/1551

Generated at Thu Feb 08 08:39:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.