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

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

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.13.2
    • Affects Version/s: 1.12.0
    • Component/s: BSON
    • None
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

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

              Created:
              Updated:
              Resolved: