Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2886

Link to new "BSONOptions" config from BSON registry deprecation notices

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • 1.13.2
    • 1.12.0
    • BSON
    • None

    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.

      Attachments

        Activity

          People

            matt.dale@mongodb.com Matt Dale
            matt.dale@mongodb.com Matt Dale
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: