Skip to content

Commit

Permalink
Tweak injectables initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Feb 10, 2024
1 parent 764ebc4 commit eb09173
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ type Connection struct {
// NewConnection returns a new Connection object with the given options
func NewConnection(opts ...Option) *Connection {
options := NewOptions(opts...)
return &Connection{ConnectionInjectables: options.ConnectionInjectables}
conn := &Connection{ConnectionInjectables: options.ConnectionInjectables}
conn.initClient()

Check failure on line 71 in connection.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `conn.initClient` is not checked (errcheck)
return conn
}

// DefaultClientConfigurer is a function that returns a new ClientConfigurer. You can override this to provide your own
Expand Down
25 changes: 18 additions & 7 deletions connectioninjectables.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func DefaultConnectionRepositories() ConnectionRepositories {
// DefaultConnectionInjectables returns a set of default injectables for a connection
func DefaultConnectionInjectables() *ConnectionInjectables {
return &ConnectionInjectables{
repositories: DefaultConnectionRepositories(),
clientConfigurer: &ClientConfig{},
repositories: DefaultConnectionRepositories(),
}
}

Expand All @@ -107,18 +108,28 @@ func (c *ConnectionInjectables) Clone(opts ...Option) *ConnectionInjectables {

// ErrConfiguratorNotSet is returned when a client configurator is not set when trying to connect
var ErrConfiguratorNotSet = errors.New("client configurator not set")

Check failure on line 110 in connectioninjectables.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
var ErrClientNotSet = errors.New("client not set")

func (c *ConnectionInjectables) initClient() error {
var err error
c.clientOnce.Do(func() {
if c.clientConfigurer == nil {
err = ErrConfiguratorNotSet
if c.client != nil {
c.clientConfigurer = nil
}
if c.client == nil && c.clientConfigurer == nil {
err = errors.Join(ErrClientNotSet, ErrConfiguratorNotSet)
return
}
c.injectLogger(c.clientConfigurer)
c.client, err = c.clientConfigurer.Client()
if err != nil {
err = fmt.Errorf("configure client (%v): %w", c.clientConfigurer, err)
if c.clientConfigurer != nil {
c.injectLogger(c.clientConfigurer)
c.client, err = c.clientConfigurer.Client()
if err != nil {
err = fmt.Errorf("configure client (%v): %w", c.clientConfigurer, err)
return
}
}
if c.client == nil {
err = ErrClientNotSet
return
}
if !c.HasLogger() {
Expand Down

0 comments on commit eb09173

Please sign in to comment.