-
Notifications
You must be signed in to change notification settings - Fork 222
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
refactor(config): use viper.Unmarshal capabilities #1079
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1079 +/- ##
==========================================
- Coverage 81.16% 80.80% -0.37%
==========================================
Files 26 26
Lines 1954 1870 -84
==========================================
- Hits 1586 1511 -75
+ Misses 285 279 -6
+ Partials 83 80 -3
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
Amazing work! I wonder in the future if we could/should separate out 'warnings' into it's own step/interface so that it doesn't fall on the responsibility of the defaults
'step'? Not necessary at the moment I don't think given we only have 2 warnings.
Thank you for doing this!!! 🥇
This change is in reaction to a comment that went by in the last configuration refactor PR.
Currently, we do lots of
viper.IsSet
andviper.Get
interrogation and extraction.This PR changes the strategy to lean into
viper.Unmarshal
capabilities on the entireconfig.Config
structure.Along with a little sugar to aid in keeping the configuration files focussed.
To add a field to an existing configuration, you now simply amend the structure:
type CorsConfig struct { Enabled bool `json:"enabled" mapstructure:"enabled"` AllowedOrigins []string `json:"allowedOrigins,omitempty" mapstructure:"allowed_origins"` + AllowedMethods []string `json:"allowedMethods,omitempty" mapstructure:"allowed_methods"` }
To set a default, you ensure the sub-configuration implements the
defaulter
interface.In the
setDefaults
method you add your default like so:func (c *CorsConfig) setDefaults(v *viper.Viper) []string { v.SetDefault("cors", map[string]any{ "allowed_origins": "*", + "allowed_methods": "GET,PUT,POST", }) return nil }
To validate the resulting configuration is appropriate, the sub-configuration can implement the
validator
interface.