Skip to content

Commit

Permalink
fixes #1701
Browse files Browse the repository at this point in the history
selects the sole provider when no key is supplied and there is exactly 1 provider.
  • Loading branch information
jonathanj-square committed Jul 2, 2024
1 parent 423ae6a commit 41bf2c3
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions common/configuration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ func (m *Manager[R]) SetJSON(ctx context.Context, pkey string, ref Ref, value js
if err := checkJSON(value); err != nil {
return fmt.Errorf("invalid value for %s, must be JSON: %w", m.router.Role(), err)
}
provider, ok := m.providers[pkey]
if !ok {
pkeys := strings.Join(m.availableProviderKeys(), ", ")
return fmt.Errorf("no provider for key %q, specify one of: %s", pkey, pkeys)
provider, err := m.selectProvider(pkey)
if err != nil {
return err
}
key, err := provider.Store(ctx, ref, value)
if err != nil {
Expand All @@ -139,6 +138,20 @@ func (m *Manager[R]) SetJSON(ctx context.Context, pkey string, ref Ref, value js
return m.router.Set(ctx, ref, key)
}

// selectProvider selects the Provider using the supplied key or defaults to the sole provider
// when no key is supplied and there exists exactly one provider.
func (m *Manager[R]) selectProvider(key string) (Provider[R], error) {
if len(key) == 0 && len(m.availableProviderKeys()) == 1 {
key = m.availableProviderKeys()[0]
}
provider, ok := m.providers[key]
if !ok {
keys := strings.Join(m.availableProviderKeys(), ", ")
return nil, fmt.Errorf("no provider for key %q, specify one of: %s", key, keys)
}
return provider, nil
}

// MapForModule combines all configuration values visible to the module. Local
// values take precedence.
func (m *Manager[R]) MapForModule(ctx context.Context, module string) (map[string][]byte, error) {
Expand Down

0 comments on commit 41bf2c3

Please sign in to comment.