From 41bf2c303e9cbaaf445044a21ebd513786b614d8 Mon Sep 17 00:00:00 2001 From: Jon Johnson <113393155+jonathanj-square@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:57:42 -0700 Subject: [PATCH] fixes #1701 selects the sole provider when no key is supplied and there is exactly 1 provider. --- common/configuration/manager.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/common/configuration/manager.go b/common/configuration/manager.go index 60ca5414c7..c8986d69b3 100644 --- a/common/configuration/manager.go +++ b/common/configuration/manager.go @@ -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 { @@ -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) {