-
Notifications
You must be signed in to change notification settings - Fork 180
feat(config)!: consolidate custom config parsing into config.Extended #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manusa
wants to merge
2
commits into
containers:main
Choose a base branch
from
marcnuri-forks:feat/extended-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−164
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package config | ||
|
|
||
| import "context" | ||
|
|
||
| type configDirPathKey struct{} | ||
|
|
||
| func withConfigDirPath(ctx context.Context, dirPath string) context.Context { | ||
| return context.WithValue(ctx, configDirPathKey{}, dirPath) | ||
| } | ||
|
|
||
| func ConfigDirPathFromContext(ctx context.Context) string { | ||
| val := ctx.Value(configDirPathKey{}) | ||
|
|
||
| if val == nil { | ||
| return "" | ||
| } | ||
|
|
||
| if strVal, ok := val.(string); ok { | ||
| return strVal | ||
| } | ||
|
|
||
| return "" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/BurntSushi/toml" | ||
| ) | ||
|
|
||
| // Extended is the interface that all configuration extensions must implement. | ||
| // Each extended config manager registers a factory function to parse its config from TOML primitives | ||
| type Extended interface { | ||
| Validate() error | ||
| } | ||
|
|
||
| type ExtendedConfigParser func(ctx context.Context, primitive toml.Primitive, md toml.MetaData) (Extended, error) | ||
|
|
||
| type extendedConfigRegistry struct { | ||
| parsers map[string]ExtendedConfigParser | ||
| } | ||
|
|
||
| func newExtendedConfigRegistry() *extendedConfigRegistry { | ||
| return &extendedConfigRegistry{ | ||
| parsers: make(map[string]ExtendedConfigParser), | ||
| } | ||
| } | ||
|
|
||
| func (r *extendedConfigRegistry) register(name string, parser ExtendedConfigParser) { | ||
| if _, exists := r.parsers[name]; exists { | ||
| panic("extended config parser already registered for name: " + name) | ||
| } | ||
|
|
||
| r.parsers[name] = parser | ||
| } | ||
|
|
||
| func (r *extendedConfigRegistry) parse(ctx context.Context, metaData toml.MetaData, configs map[string]toml.Primitive) (map[string]Extended, error) { | ||
| if len(configs) == 0 { | ||
| return make(map[string]Extended), nil | ||
| } | ||
| parsedConfigs := make(map[string]Extended, len(configs)) | ||
|
|
||
| for name, primitive := range configs { | ||
| parser, ok := r.parsers[name] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| extendedConfig, err := parser(ctx, primitive, metaData) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse extended config for '%s': %w", name, err) | ||
| } | ||
|
|
||
| if err = extendedConfig.Validate(); err != nil { | ||
| return nil, fmt.Errorf("failed to validate extended config for '%s': %w", name, err) | ||
| } | ||
|
|
||
| parsedConfigs[name] = extendedConfig | ||
| } | ||
|
|
||
| return parsedConfigs, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,7 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| var providerConfigRegistry = newExtendedConfigRegistry() | ||
|
|
||
| "github.com/BurntSushi/toml" | ||
| ) | ||
|
|
||
| // ProviderConfig is the interface that all provider-specific configurations must implement. | ||
| // Each provider registers a factory function to parse its config from TOML primitives | ||
| type ProviderConfig interface { | ||
| Validate() error | ||
| } | ||
|
|
||
| type ProviderConfigParser func(ctx context.Context, primitive toml.Primitive, md toml.MetaData) (ProviderConfig, error) | ||
|
|
||
| type configDirPathKey struct{} | ||
|
|
||
| func withConfigDirPath(ctx context.Context, dirPath string) context.Context { | ||
| return context.WithValue(ctx, configDirPathKey{}, dirPath) | ||
| } | ||
|
|
||
| func ConfigDirPathFromContext(ctx context.Context) string { | ||
| val := ctx.Value(configDirPathKey{}) | ||
|
|
||
| if val == nil { | ||
| return "" | ||
| } | ||
|
|
||
| if strVal, ok := val.(string); ok { | ||
| return strVal | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| var ( | ||
| providerConfigParsers = make(map[string]ProviderConfigParser) | ||
| ) | ||
|
|
||
| func RegisterProviderConfig(strategy string, parser ProviderConfigParser) { | ||
| if _, exists := providerConfigParsers[strategy]; exists { | ||
| panic(fmt.Sprintf("provider config parser already registered for strategy '%s'", strategy)) | ||
| } | ||
|
|
||
| providerConfigParsers[strategy] = parser | ||
| } | ||
|
|
||
| func getProviderConfigParser(strategy string) (ProviderConfigParser, bool) { | ||
| provider, ok := providerConfigParsers[strategy] | ||
|
|
||
| return provider, ok | ||
| func RegisterProviderConfig(name string, parser ExtendedConfigParser) { | ||
| providerConfigRegistry.register(name, parser) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,7 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| var toolsetConfigRegistry = newExtendedConfigRegistry() | ||
|
|
||
| "github.com/BurntSushi/toml" | ||
| ) | ||
|
|
||
| // ToolsetConfig is the interface that all toolset-specific configurations must implement. | ||
| // Each toolset registers a factory function to parse its config from TOML primitives | ||
| type ToolsetConfig interface { | ||
| Validate() error | ||
| } | ||
|
|
||
| type ToolsetConfigParser func(ctx context.Context, primitive toml.Primitive, md toml.MetaData) (ToolsetConfig, error) | ||
|
|
||
| var ( | ||
| toolsetConfigParsers = make(map[string]ToolsetConfigParser) | ||
| ) | ||
|
|
||
| func RegisterToolsetConfig(name string, parser ToolsetConfigParser) { | ||
| if _, exists := toolsetConfigParsers[name]; exists { | ||
| panic(fmt.Sprintf("toolset config parser already registered for toolset '%s'", name)) | ||
| } | ||
|
|
||
| toolsetConfigParsers[name] = parser | ||
| } | ||
|
|
||
| func getToolsetConfigParser(name string) (ToolsetConfigParser, bool) { | ||
| parser, ok := toolsetConfigParsers[name] | ||
|
|
||
| return parser, ok | ||
| func RegisterToolsetConfig(name string, parser ExtendedConfigParser) { | ||
| toolsetConfigRegistry.register(name, parser) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I feel like
ExtendedConfigis a bit clearer hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExtendedConfig is explicitly marked as problematic (redundant) by some linters since it contains the package name, you'd usually refer to this as
config.Extended