Skip to content

Commit

Permalink
feat: add mongo driver options constructor (#5)
Browse files Browse the repository at this point in the history
* feat: add mongo driver options constructor

* fix: error on empty client
  • Loading branch information
danteay authored Jun 1, 2023
1 parent f23e3a7 commit f1ecb33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
28 changes: 28 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ type Driver struct {
dbName string
}

type Option func(d *Driver)

func WithClient(client *mongo.Client) Option {
return func(d *Driver) {
d.client = client
}
}

func WithDbName(dbName string) Option {
return func(d *Driver) {
d.dbName = dbName
}
}

func NewWithOptions(options ...Option) (*Driver, error) {
d := &Driver{}

for _, opt := range options {
opt(d)
}

if d.client == nil {
return nil, ErrEmptyClient
}

return d, nil
}

func New(ctx context.Context) (*Driver, error) {
return NewWithConfig(ctx, DefaultConfig())
}
Expand Down
7 changes: 7 additions & 0 deletions driver/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package driver

import "errors"

var (
ErrEmptyClient = errors.New("mgorepo-driver: client is nil")
)

0 comments on commit f1ecb33

Please sign in to comment.