Skip to content

Commit

Permalink
Making lib/backend/client.getFactory public
Browse files Browse the repository at this point in the history
Mainly to allow writing "wrapper" custom backends, see eg
#278 (comment)

Signed-off-by: Jean Rouge <[email protected]>
  • Loading branch information
wk8 committed Oct 21, 2020
1 parent e435b83 commit d4b0fca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/backend/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func Register(name string, factory ClientFactory) {
_factories[name] = factory
}

// getFactory returns backend client factory given client name.
func getFactory(name string) (ClientFactory, error) {
// GetFactory returns backend client factory given client name.
// This function should stay public to allow for wrapper custom backends.
func GetFactory(name string) (ClientFactory, error) {
factory, ok := _factories[name]
if !ok {
return nil, fmt.Errorf("no backend client defined with name %s", name)
Expand Down
32 changes: 32 additions & 0 deletions lib/backend/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package backend

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRegisterAndGetFactory(t *testing.T) {
t.Run("round-trip", func(t *testing.T) {
name := "dummy"
factory := &dummyFactory{}

Register(name, factory)
roundTrip, err := GetFactory(name)

assert.NoError(t, err)
assert.Equal(t, factory, roundTrip)
})

t.Run("GetFactory errors out on missing factory", func(t *testing.T) {
_, err := GetFactory("i_dont_exist")

assert.Error(t, err)
})
}

type dummyFactory struct{}

func (f *dummyFactory) Create(config interface{}, authConfig interface{}) (Client, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion lib/backend/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewManager(configs []Config, auth AuthConfig) (*Manager, error) {
var backendConfig interface{}
for name, backendConfig = range config.Backend { // Pull the only key/value out of map
}
factory, err := getFactory(name)
factory, err := GetFactory(name)
if err != nil {
return nil, fmt.Errorf("get backend client factory: %s", err)
}
Expand Down

0 comments on commit d4b0fca

Please sign in to comment.