[GODRIVER-348] Refactor Client Creation Created: 05/Apr/18  Updated: 28/Oct/23  Resolved: 27/Nov/18

Status: Closed
Project: Go Driver
Component/s: Options & Configuration
Affects Version/s: None
Fix Version/s: 0.1.0

Type: Improvement Priority: Major - P3
Reporter: Kristofer Brandow (Inactive) Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: beta, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-195 Custom Dialer option for Client Closed
Epic Link: Finalize mongo API

 Description   

Remove the NewClientFromConnString function from the mongo package. This will remove the confusion around creating connection strings for users and the inability to remake a string version of that connection string form a connstring.ConnString instance.

 


Currently there are 3 functions that can be used to create a new mongo.Client: NewClient, NewClientWithOptions, and NewClientFromConnString. While useful to be able to create a client with either a URI or a ConnString it complicates the API. For instance, if we wanted to allow users to also use ClientOptions and a ConnString we would need to add another function.

The current way of using ClientOptions is to start with mongo.ClientOpt and build the options off of that.

To help clean up the API, we should change how Clients are created.

Have a single function, NewClient, that takes a ClientOptions struct. Add two new functions, WithURI and WithConnString, each returning a ClientOptions struct.

The new API would work like this:

opts := mongo.WithURI("mongodb://localhost:27017").AppName("foo").Dialer(&net.Dialer{})
client, err := mongo.NewClient(opts)

We could change the methods on ClientOptions to be With* to make it read better as well. Alternatively, we can just have two functions, NewClient and NewClientWithConnString, with the following signatures:

func NewClient(uri string, opts *ClientOptions) (*Client, error)
func NewClientWithConnString(cs connstring.ConnString, opts *ClientOptions) (*Client, error)

But this means adding a custom dialer to the client would look like this:

opts := mongo.ClientOptions.Dialer(&net.Dialer{})
client, err := mongo.NewClient("mongodb://localhost:27017", opts)



 Comments   
Comment by Githook User [ 27/Nov/18 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@10gen.com'}

Message: GODRIVER-348 remove NewClientFromConnString

Change-Id: I6493a4be9d54e5f8f57d05527d2b150ba408c532
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/c0711bfc0510ef1df7c9546c992bfd906f4d5602

Comment by Sam Kleinman (Inactive) [ 21/May/18 ]

After talking with Kris, I think this makes sense to wait until GODRIVER-272

Comment by Kristofer Brandow (Inactive) [ 05/Apr/18 ]

cc eric.daniels jeff.yemin craiggwilson

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