Skip to content

Commit

Permalink
Add converter and provider settings to confmap.ResolverSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-bradley committed Apr 8, 2024
1 parent c6d1482 commit 7948b77
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 122 deletions.
25 changes: 25 additions & 0 deletions .chloggen/pass-confmap-settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confmap

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `ProviderSettings`, `ConverterSettings`, `ProviderFactories`, and `ConverterFactories` fields to `confmap.ResolverSettings`

# One or more tracking issues or pull requests related to the change
issues: [9516]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: This allows configuring providers and converters, which are instantiated by `NewResolver` using the given factories.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
22 changes: 11 additions & 11 deletions confmap/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestResolverExpandEnvVars(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
resolver, err := NewResolver(ResolverSettings{URIs: []string{filepath.Join("testdata", test.name)}, Providers: makeMapProvidersMap(fileProvider, envProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{filepath.Join("testdata", test.name)}, ProviderFactories: []ProviderFactory{fileProvider, envProvider}, ConverterFactories: nil})
require.NoError(t, err)

// Test that expanded configs are the same with the simple config with no env vars.
Expand All @@ -65,7 +65,7 @@ func TestResolverDoneNotExpandOldEnvVars(t *testing.T) {
return NewRetrieved("some string")
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"test:"}, Providers: makeMapProvidersMap(fileProvider, envProvider, emptySchemeProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"test:"}, ProviderFactories: []ProviderFactory{fileProvider, envProvider, emptySchemeProvider}, ConverterFactories: nil})
require.NoError(t, err)

// Test that expanded configs are the same with the simple config with no env vars.
Expand All @@ -86,7 +86,7 @@ func TestResolverExpandMapAndSliceValues(t *testing.T) {
return NewRetrieved(receiverExtraMapValue)
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

cfgMap, err := resolver.Resolve(context.Background())
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestResolverExpandStringValues(t *testing.T) {
return NewRetrieved(uri[5:])
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, newEnvProvider(), testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, newEnvProvider(), testProvider}, ConverterFactories: nil})
require.NoError(t, err)

cfgMap, err := resolver.Resolve(context.Background())
Expand All @@ -305,7 +305,7 @@ func TestResolverExpandStringValues(t *testing.T) {
}
}

func newEnvProvider() Provider {
func newEnvProvider() ProviderFactory {
return newFakeProvider("env", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {
switch uri {
case "env:COMPLEX_VALUE":
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestResolverExpandReturnError(t *testing.T) {
return nil, myErr
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand All @@ -388,7 +388,7 @@ func TestResolverInfiniteExpand(t *testing.T) {
return NewRetrieved(receiverValue)
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand All @@ -404,7 +404,7 @@ func TestResolverExpandInvalidScheme(t *testing.T) {
panic("must not be called")
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand All @@ -421,7 +421,7 @@ func TestResolverExpandInvalidOpaqueValue(t *testing.T) {
panic("must not be called")
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand All @@ -437,7 +437,7 @@ func TestResolverExpandUnsupportedScheme(t *testing.T) {
panic("must not be called")
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand All @@ -453,7 +453,7 @@ func TestResolverExpandStringValueInvalidReturnValue(t *testing.T) {
return NewRetrieved([]any{1243})
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, Providers: makeMapProvidersMap(provider, testProvider), Converters: nil})
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil})
require.NoError(t, err)

_, err = resolver.Resolve(context.Background())
Expand Down
62 changes: 51 additions & 11 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,36 @@ type Resolver struct {
watcher chan error
}

type ConverterFactory = func(ConverterSettings) Converter

type ProviderFactory = func(ProviderSettings) Provider

// ResolverSettings are the settings to configure the behavior of the Resolver.
type ResolverSettings struct {
// URIs locations from where the Conf is retrieved, and merged in the given order.
// It is required to have at least one location.
URIs []string

// ProviderFactories is a list of Provider creation functions.
// It is required to have at least one ProviderFactory
// if a Provider is not given.
ProviderFactories []ProviderFactory

// Providers is a map of pairs <scheme, Provider>.
// It is required to have at least one Provider.
// Deprecated: [v0.96.0] Use ProviderFactories instead
Providers map[string]Provider

// MapConverters is a slice of Converter.
ProviderSettings ProviderSettings

// ConverterFactories is a slice of Converter creation functions.
ConverterFactories []ConverterFactory

// Converters is a slice of Converters.
// Deprecated: [v0.96.0] Use ConverterFactories instead
Converters []Converter

ConverterSettings ConverterSettings
}

// NewResolver returns a new Resolver that resolves configuration from multiple URIs.
Expand All @@ -65,10 +83,38 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
return nil, errors.New("invalid map resolver config: no URIs")
}

if len(set.Providers) == 0 {
if len(set.ProviderFactories) == 0 && len(set.Providers) == 0 {
return nil, errors.New("invalid map resolver config: no Providers")
}

var providers map[string]Provider
var converters []Converter

if len(set.Providers) != 0 {
if len(set.ProviderFactories) != 0 {
return nil, errors.New("only one of ResolverSettings.Providers and ResolverSettings.ProviderFactories can be used")
}
providers = set.Providers
} else {
providers = make(map[string]Provider, len(set.ProviderFactories))
for _, newProvider := range set.ProviderFactories {
provider := newProvider(set.ProviderSettings)
providers[provider.Scheme()] = provider
}
}

if len(set.Converters) != 0 {
if len(set.ConverterFactories) != 0 {
return nil, errors.New("only one of ResolverSettings.Converters and ResolverSettings.ConverterFactories can be used")
}
converters = set.Converters
} else {
converters = make([]Converter, len(set.ConverterFactories))
for i, newConverter := range set.ConverterFactories {
converters[i] = newConverter(set.ConverterSettings)
}
}

// Safe copy, ensures the slices and maps cannot be changed from the caller.
uris := make([]location, len(set.URIs))
for i, uri := range set.URIs {
Expand All @@ -83,22 +129,16 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
if err != nil {
return nil, err
}
if _, ok := set.Providers[lURI.scheme]; !ok {
if _, ok := providers[lURI.scheme]; !ok {
return nil, fmt.Errorf("unsupported scheme on URI %q", uri)
}
uris[i] = lURI
}
providersCopy := make(map[string]Provider, len(set.Providers))
for k, v := range set.Providers {
providersCopy[k] = v
}
convertersCopy := make([]Converter, len(set.Converters))
copy(convertersCopy, set.Converters)

return &Resolver{
uris: uris,
providers: providersCopy,
converters: convertersCopy,
providers: providers,
converters: converters,
watcher: make(chan error, 1),
}, nil
}
Expand Down
Loading

0 comments on commit 7948b77

Please sign in to comment.