From b2483f76d7bf06e27a88809f4c758bf27894ba44 Mon Sep 17 00:00:00 2001 From: Matthew Hartstonge Date: Thu, 28 Nov 2024 23:19:01 +1300 Subject: [PATCH] fix: always return `*Diagnostics`. --- _example/envconfig.go | 4 ++-- _example/fileconfig.go | 4 ++-- _example/flagconfig.go | 4 ++-- config_type.go | 2 +- config_typeable.go | 2 +- configurator.go | 26 +++++++++++++------------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/_example/envconfig.go b/_example/envconfig.go index fd29e01..ecb0680 100644 --- a/_example/envconfig.go +++ b/_example/envconfig.go @@ -14,8 +14,8 @@ type ExampleEnvConfig struct { Port int `envconfig:"PORT" default:"9090"` } -func (e *ExampleEnvConfig) Validate(_ diag.Component) diag.Diagnostics { - var diags diag.Diagnostics +func (e *ExampleEnvConfig) Validate(_ diag.Component) *diag.Diagnostics { + diags := new(diag.Diagnostics) if e.Port < 0 || e.Port > 65535 { diags.Env("PORT").Error("Unable to parse port", "Port must be between 0 and 65535, but instead got "+strconv.Itoa(e.Port)) diff --git a/_example/fileconfig.go b/_example/fileconfig.go index 5ece9bf..15a69a0 100644 --- a/_example/fileconfig.go +++ b/_example/fileconfig.go @@ -19,8 +19,8 @@ type ExampleFileConfig struct { } `hcl:"app,block" json:"myapp" toml:"MyApp" yaml:"myapp"` } -func (e *ExampleFileConfig) Validate(component diag.Component) diag.Diagnostics { - var diags diag.Diagnostics +func (e *ExampleFileConfig) Validate(component diag.Component) *diag.Diagnostics { + diags := new(diag.Diagnostics) if e.MyApp.Port < 0 || e.MyApp.Port > 65535 { diags.FromComponent(component, "myapp.port"). Error("Unable to parse port", diff --git a/_example/flagconfig.go b/_example/flagconfig.go index 9fd0a6b..4fa4823 100644 --- a/_example/flagconfig.go +++ b/_example/flagconfig.go @@ -26,8 +26,8 @@ func (f *ExampleFlagConfig) Init() { flag.IntVar(&f.BackupFrequency, "backup-frequency", 0, "path to config file") } -func (f *ExampleFlagConfig) Validate(component diag.Component) diag.Diagnostics { - var diags diag.Diagnostics +func (f *ExampleFlagConfig) Validate(component diag.Component) *diag.Diagnostics { + diags := new(diag.Diagnostics) if f.Port < 0 || f.Port > 65535 { diags.FromComponent(component, "-port"). Error("Unable to parse port", diff --git a/config_type.go b/config_type.go index f71e8bb..80a49a3 100644 --- a/config_type.go +++ b/config_type.go @@ -25,7 +25,7 @@ func (c *ConfigType) Values() any { // fixing it. // A component is provided so that file configurators can pass through if it is // global or local configuration. -func (c *ConfigType) Validate(component diag.Component) diag.Diagnostics { +func (c *ConfigType) Validate(component diag.Component) *diag.Diagnostics { return c.Config.Validate(component) } diff --git a/config_typeable.go b/config_typeable.go index 4e14499..7a7d77b 100644 --- a/config_typeable.go +++ b/config_typeable.go @@ -20,6 +20,6 @@ type ConfigParser interface { } type ConfigImplementer interface { - Validate(component diag.Component) diag.Diagnostics + Validate(component diag.Component) *diag.Diagnostics Merge(config any) any } diff --git a/configurator.go b/configurator.go index 869919c..cc99098 100644 --- a/configurator.go +++ b/configurator.go @@ -21,7 +21,7 @@ import ( // To be clear, this means config files are searched for and read first, then // environment variables are merged in over the top, then command line flags as // the highest priority. -func New(config *Config) (*Config, diag.Diagnostics) { +func New(config *Config) (*Config, *diag.Diagnostics) { return config.Parse() } @@ -77,8 +77,8 @@ type ParsedConfig struct { } // Parse processes the -func (c *Config) Parse() (*Config, diag.Diagnostics) { - var diags diag.Diagnostics +func (c *Config) Parse() (*Config, *diag.Diagnostics) { + diags := new(diag.Diagnostics) // default filename to 'config' if not provided. if c.FileName == "" { @@ -110,7 +110,7 @@ func (c *Config) Parse() (*Config, diag.Diagnostics) { // processFileFlagConfig extracts the path to a config file, if specified via // the customisable `-config-file` flag. -func (c *Config) processFileFlagConfig(diags diag.Diagnostics) diag.Diagnostics { +func (c *Config) processFileFlagConfig(diags *diag.Diagnostics) *diag.Diagnostics { if c.FileFlag == "" { c.FileFlag = DEFAULT_CONFIG_FILEFLAG } @@ -173,12 +173,12 @@ func removeFlagFromArgs(name string) { } // processFileConfig iterates through the provided file type parsers, stating the file. -func (c *Config) processFileConfig(diags diag.Diagnostics, component diag.Component) diag.Diagnostics { +func (c *Config) processFileConfig(diags *diag.Diagnostics, component diag.Component) *diag.Diagnostics { paths, diags := getConfigPaths(diags, component, c) for _, path := range paths { for _, fileConfig := range c.File { - if !fileConfig.Stat(&diags, component, c, path) { + if !fileConfig.Stat(diags, component, c, path) { // If we can't find the file, skip it. continue } @@ -192,7 +192,7 @@ func (c *Config) processFileConfig(diags diag.Diagnostics, component diag.Compon } // getConfigPaths returns file paths to the configuration directory. -func getConfigPaths(diags diag.Diagnostics, component diag.Component, cfg *Config) ([]string, diag.Diagnostics) { +func getConfigPaths(diags *diag.Diagnostics, component diag.Component, cfg *Config) ([]string, *diag.Diagnostics) { if pathStrategy, ok := configFilePathStrategies[component]; ok { return pathStrategy(diags, cfg) } @@ -208,7 +208,7 @@ func getConfigPaths(diags diag.Diagnostics, component diag.Component, cfg *Confi ) } -type configFilePathStrategy func(diags diag.Diagnostics, cfg *Config) ([]string, diag.Diagnostics) +type configFilePathStrategy func(diags *diag.Diagnostics, cfg *Config) ([]string, *diag.Diagnostics) var configFilePathStrategies = map[diag.Component]configFilePathStrategy{ diag.ComponentGlobalFile: processGlobalFilePaths, @@ -216,7 +216,7 @@ var configFilePathStrategies = map[diag.Component]configFilePathStrategy{ diag.ComponentFlagFile: processFlagFilePath, } -func processGlobalFilePaths(diags diag.Diagnostics, cfg *Config) ([]string, diag.Diagnostics) { +func processGlobalFilePaths(diags *diag.Diagnostics, cfg *Config) ([]string, *diag.Diagnostics) { var paths []string if runtime.GOOS == "linux" { @@ -241,7 +241,7 @@ func processGlobalFilePaths(diags diag.Diagnostics, cfg *Config) ([]string, diag return paths, diags } -func processLocalFilePaths(diags diag.Diagnostics, cfg *Config) ([]string, diag.Diagnostics) { +func processLocalFilePaths(diags *diag.Diagnostics, cfg *Config) ([]string, *diag.Diagnostics) { var paths []string if dir, err := os.UserHomeDir(); err != nil { @@ -269,7 +269,7 @@ func processLocalFilePaths(diags diag.Diagnostics, cfg *Config) ([]string, diag. return paths, diags } -func processFlagFilePath(diags diag.Diagnostics, cfg *Config) ([]string, diag.Diagnostics) { +func processFlagFilePath(diags *diag.Diagnostics, cfg *Config) ([]string, *diag.Diagnostics) { if cfg.ConfigFilePath == "" { return []string{}, diags } @@ -293,7 +293,7 @@ func configFP(cfg *Config, dir string) string { } // processFlagConfig processes and merges in any provided flag configuration. -func (c *Config) processFlagConfig(diags diag.Diagnostics, component diag.Component) diag.Diagnostics { +func (c *Config) processFlagConfig(diags *diag.Diagnostics, component diag.Component) *diag.Diagnostics { if c.Flag == nil { return diags } @@ -305,7 +305,7 @@ func (c *Config) processFlagConfig(diags diag.Diagnostics, component diag.Compon // processConfig does the heavy lifting of parsing, validating and merging the // config together returning diagnostic information at the end of the process. -func (c *Config) processConfig(diags diag.Diagnostics, component diag.Component, configurer ConfigTypeable) diag.Diagnostics { +func (c *Config) processConfig(diags *diag.Diagnostics, component diag.Component, configurer ConfigTypeable) *diag.Diagnostics { if configurer == nil { // no parser provided, may be expected, for example, if CLI flags aren't implemented. diags.FromComponent(component, "").