Skip to content

Commit

Permalink
fix: always return *Diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhartstonge committed Nov 28, 2024
1 parent e043d1b commit b2483f7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions _example/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions _example/fileconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions _example/flagconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion config_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion config_typeable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 13 additions & 13 deletions configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand All @@ -208,15 +208,15 @@ 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,
diag.ComponentLocalFile: processLocalFilePaths,
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" {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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, "").
Expand Down

0 comments on commit b2483f7

Please sign in to comment.