From 48da8b8e71336645ada7ea555d2dc53b8e5bbfa3 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 16 Dec 2024 12:15:08 -0700 Subject: [PATCH 1/3] GODRIVER-3443 Remove reference to clientOptions builder --- docs/migration-2.0.md | 55 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index aa11b57f92..b813c6c70b 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -535,10 +535,10 @@ function MergeOptions(target, optionsList): Currently, the driver maintains this logic for every options type, e.g. [MergeClientOptions](https://github.com/mongodb/mongo-go-driver/blob/2e7cb372b05cba29facd58aac7e715c3cec4e377/mongo/options/clientoptions.go#L1065). For v2, we’ve decided to abstract the merge functions by changing the options builder pattern to maintain a slice of setter functions, rather than setting data directly to an options object. Typical usage of options should not change, for example the following is still honored: ```go -opts1 := options.Client().SetAppName("appName") -opts2 := options.Client().SetConnectTimeout(math.MaxInt64) +opts1 := options.Find().SetBatchSize(1) +opts2 := options.Find().SetComment("foo") -_, err := mongo.Connect(opts1, opts2) +_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts1, opts2) if err != nil { panic(err) } @@ -553,29 +553,27 @@ The options builder is now a slice of setters, rather than a single options obje ```go // v1 -opts := options.Client().ApplyURI("mongodb://x:y@localhost:27017") +opts := options.Find().SetBatchSize(1) -if opts.Auth.Username == "x" { - opts.Auth.Password = "z" +if opts.MaxAwaitTime == nil { + opts.MaxAwaitTime = &defaultMaxAwaitTime } ``` ```go - // v2 -opts := options.Client().ApplyURI("mongodb://x:y@localhost:27017") +opts := options.Find().SetBatchSize(1) -// If the username is "x", use password "z" -pwSetter := func(opts *options.ClientOptions) error { - if opts.Auth.Username == "x" { - opts.Auth.Password = "z" +maxAwaitTimeSetter := func(opts *options.FindOptions) error { + if opts.MaxAwaitTime == nil { + opts.MaxAwaitTime = &defaultMaxAwaitTime } return nil } -opts.Opts = append(opts.Opts, pwSetter) +opts.Opts = append(opts.Opts, maxAwiatTimeSetter) ``` #### Creating a Slice of Options @@ -585,22 +583,21 @@ Using options created with the builder pattern as elements in a slice: ```go // v1 -opts1 := options.Client().SetAppName("appName") -opts2 := options.Client().SetConnectTimeout(math.MaxInt64) +opts1 := options.Find().SetBatchSize(1) +opts2 := options.Find().SetComment("foo") -opts := []*options.ClientOptions{opts1, opts2} -_, err := mongo.Connect(opts...) +opts := []*options.FindOptions{opts1, opts2} +_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts...) ``` ```go // v2 -opts1 := options.Client().SetAppName("appName") -opts2 := options.Client().SetConnectTimeout(math.MaxInt64) +opts1 := options.Find().SetBatchSize(1) +opts2 := options.Find().SetComment("foo") -// Option objects are "Listers" in v2, objects that hold a list of setters -opts := []options.Lister[options.ClientOptions]{opts1, opts2} -_, err := mongo.Connect(opts...) +opts := []options.Lister[options.FindOptions]{opts1, opts2} +_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts...) ``` #### Creating Options from Builder @@ -610,21 +607,21 @@ Since a builder is just a slice of option setters, users can create options dire ```go // v1 -opt := &options.ClientOptions{} -opt.ApplyURI(uri) +opt := &options.FindOptions{} +opt.SetBatchSize(1) -return clientOptionAdder{option: opt} +return findOptionAdder{option: opt} ``` ```go // v2 -var opts options.ClientOptions -for _, set := range options.Client().ApplyURI(uri).Opts { +var opts options.FindOptions +for _, set := range options.Find().SetBatchSize(1).Opts { _ = set(&opts) } -return clientOptionAdder{option: &opts} +return findOptionAdder{option: &opts} ``` ### DeleteManyOptions / DeleteOneOptions @@ -719,7 +716,7 @@ The `WTimeout` field has been removed from the `WriteConcern` struct. Instead, u ## Bsoncodecs / Bsonoptions Package -`*Codec` structs and `New*Codec` methods have been removed. Additionally, the correlated `bson/bsonoptions` package has been removed, so codecs are not directly configurable using `*CodecOptions` structs in Go Driver 2.0. To configure the encode and decode behavior, use the configuration methods on a `bson.Encoder` or `bson.Decoder`. To configure the encode and decode behavior for a `mongo.Client`, use `options.ClientOptionsBuilder.SetBSONOptions` with `BSONOptions`. +`*Codec` structs and `New*Codec` methods have been removed. Additionally, the correlated `bson/bsonoptions` package has been removed, so codecs are not directly configurable using `*CodecOptions` structs in Go Driver 2.0. To configure the encode and decode behavior, use the configuration methods on a `bson.Encoder` or `bson.Decoder`. To configure the encode and decode behavior for a `mongo.Client`, use `options.ClientOptions.SetBSONOptions` with `BSONOptions`. This example shows how to set `ObjectIDAsHex`. From 593d7efb184bcd2d096a8ca2ee833ea29d21e44b Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 16 Dec 2024 12:17:43 -0700 Subject: [PATCH 2/3] GODRIVER-3443 Change MergeClientOptions link to MergeFindOptions --- docs/migration-2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index b813c6c70b..bc7ec1862a 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -532,7 +532,7 @@ function MergeOptions(target, optionsList): return target ``` -Currently, the driver maintains this logic for every options type, e.g. [MergeClientOptions](https://github.com/mongodb/mongo-go-driver/blob/2e7cb372b05cba29facd58aac7e715c3cec4e377/mongo/options/clientoptions.go#L1065). For v2, we’ve decided to abstract the merge functions by changing the options builder pattern to maintain a slice of setter functions, rather than setting data directly to an options object. Typical usage of options should not change, for example the following is still honored: +Currently, the driver maintains this logic for every options type, e.g. [MergeFindOptions](https://github.com/mongodb/mongo-go-driver/blob/2e7cb372b05cba29facd58aac7e715c3cec4e377/mongo/options/findoptions.go#L257). For v2, we’ve decided to abstract the merge functions by changing the options builder pattern to maintain a slice of setter functions, rather than setting data directly to an options object. Typical usage of options should not change, for example the following is still honored: ```go opts1 := options.Find().SetBatchSize(1) From e344a770cf2f4dc9c9485025dd4769f7834b03bc Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 16 Dec 2024 12:20:20 -0700 Subject: [PATCH 3/3] GODRIVER-3443 Fix await typo --- docs/migration-2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index bc7ec1862a..c32d353e2c 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -573,7 +573,7 @@ maxAwaitTimeSetter := func(opts *options.FindOptions) error { return nil } -opts.Opts = append(opts.Opts, maxAwiatTimeSetter) +opts.Opts = append(opts.Opts, maxAwaitTimeSetter) ``` #### Creating a Slice of Options