Skip to content

Commit

Permalink
provide a helper to manage multiple connections and create them on de…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
ftl committed Dec 10, 2023
1 parent 958d1dc commit 2df34be
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/hamdeck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
const (
ConfigDefaultFilename = "hamdeck.json"
ConfigMainKey = "hamdeck"
ConfigConnections = "connections"
ConfigStartPageID = "start_page"
ConfigPages = "pages"
ConfigButtons = "buttons"
ConfigType = "type"
ConfigIndex = "index"
ConfigConnections = "connections"
ConfigConnection = "connection"
)

func (d *HamDeck) ReadConfig(r io.Reader) error {
Expand Down
40 changes: 40 additions & 0 deletions pkg/hamdeck/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,43 @@ func TestReadConfig_Connections(t *testing.T) {
assert.False(t, ok)
})
}

func TestConnectionManager(t *testing.T) {
provider := &testConnectionProvider{
name: "blah",
config: ConnectionConfig{
"type": "test",
"some_config": "some_value",
},
}
manager := NewConnectionManager[*testConnection]("test", provider, provider.CreateConnection)

connection, err := manager.Get("blah")
assert.NoError(t, err)
assert.Equal(t, "some_value", connection.config["some_config"])

_, err = manager.Get("undefined")
assert.Error(t, err)
}

type testConnection struct {
config ConnectionConfig
}

type testConnectionProvider struct {
name string
config ConnectionConfig
}

func (p *testConnectionProvider) GetConnection(name string, connectionType string) (ConnectionConfig, bool) {
if name != p.name || connectionType != "test" {
return ConnectionConfig{}, false
}
return p.config, true
}

func (p *testConnectionProvider) CreateConnection(config ConnectionConfig) (*testConnection, error) {
return &testConnection{
config: config,
}, nil
}
47 changes: 45 additions & 2 deletions pkg/hamdeck/hamdeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ type connectionKey struct {
connectionType string
}

type ConnectionConfig map[string]any

const legacyPageID = ""

type HamDeck struct {
Expand Down Expand Up @@ -314,3 +312,48 @@ func (h *LongpressHandler) Released() {
}
h.timer.Stop()
}

type ConnectionConfig map[string]any

type ConnectionConfigProvider interface {
GetConnection(string, string) (ConnectionConfig, bool)
}

type ConnectionFactory[T any] func(ConnectionConfig) (T, error)

type ConnectionManager[T any] struct {
connectionType string
provider ConnectionConfigProvider
factory ConnectionFactory[T]
connections map[string]T
}

func NewConnectionManager[T any](connectionType string, provider ConnectionConfigProvider, factory ConnectionFactory[T]) *ConnectionManager[T] {
return &ConnectionManager[T]{
connectionType: connectionType,
provider: provider,
factory: factory,
connections: make(map[string]T),
}
}

func (m *ConnectionManager[T]) Get(name string) (T, error) {
connection, ok := m.connections[name]
if ok {
return connection, nil
}

config, ok := m.provider.GetConnection(name, m.connectionType)
if !ok {
return connection, fmt.Errorf("no %s connection defined with name %s", m.connectionType, name)
}

connection, err := m.factory(config)
if err != nil {
return connection, err
}

m.connections[name] = connection

return connection, nil
}

0 comments on commit 2df34be

Please sign in to comment.