diff --git a/api.go b/api.go index b8638a53270667b0f2c1232a049161bc9dfc51bc..c89bfa3e81955cdcf3c1ffb7c856718d78ce0717 100644 --- a/api.go +++ b/api.go @@ -13,11 +13,8 @@ func New[C any](defaults C) *Settings[C] { s := &Settings[C]{} - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() s.initDefaults() @@ -43,11 +40,8 @@ func New[C any](defaults C) *Settings[C] { // The mnemonic is used to identify the configuration in the configuration file func (s *Settings[C]) SetMnemonic(mnemonic string) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if mnemonic == "" { diff --git a/change.go b/change.go index df2b1bd2cc96382fef42b436671e66697a6028c4..221c4552fb389c33e299e73a05820af2bfc7a650 100644 --- a/change.go +++ b/change.go @@ -23,11 +23,8 @@ func (s *Settings[C]) setConfigInternal(config C) (*Settings[C], diff.Changelog) err error ) - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if err := validateConfig[C](config); err != nil { diff --git a/copyable.go b/copyable.go index 7a64e4b624df4de57d4f456fc5ae2b5469df1346..4e30abb0125f0f5f1830e10b7cf7ef5b96f14928 100644 --- a/copyable.go +++ b/copyable.go @@ -16,11 +16,8 @@ func (s *Settings[C]) Copy(m map[string]any) { s.Lock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() copyOf := func(s C) C { diff --git a/env.go b/env.go index 050a92092307a970a1cfe688843514824c261918..a809a23dc7dc54e70194671cd745e71e348c4bd4 100644 --- a/env.go +++ b/env.go @@ -15,11 +15,8 @@ func (s *Settings[C]) InitFromEnv(prefix string) *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if prefix != "" { diff --git a/error-handler.go b/error-handler.go index fee0f7cd73e802faf16da24168876e1c2ffa67d0..92af915e1cf0f3fe236ea150f4ebf74c52865aa6 100644 --- a/error-handler.go +++ b/error-handler.go @@ -4,6 +4,7 @@ package configuration type ErrorEvent struct { + Errors []error } type ErrorHook interface { diff --git a/export.go b/export.go index e1d9aba2f0fb3252e2f4ac1278de0519b68867d1..9f2424d729bbb89a45c73d4ae9b3d510b8639d08 100644 --- a/export.go +++ b/export.go @@ -34,11 +34,8 @@ func (s *Settings[C]) writeProperties(writer io.Writer) error { m, errors := getMapForProperties[C](s.config) - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if len(errors) > 0 { @@ -61,11 +58,8 @@ func (s *Settings[C]) WriteFile(fn string, format Format) *Settings[C] { var file *os.File - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if fn == "" { @@ -89,11 +83,8 @@ func (s *Settings[C]) Write(writer io.Writer, format Format) *Settings[C] { var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() switch format { diff --git a/file.go b/file.go index b934f0e0edf4d28fc150c1190986f0aa43ea3f92..62e1c5b2168c540d80262f63c8d355f999a8886e 100644 --- a/file.go +++ b/file.go @@ -39,11 +39,8 @@ func (s *Settings[C]) AddFile(file string, format ...Format) *Settings[C] { var f Format - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if format == nil || len(format) == 0 { @@ -112,11 +109,8 @@ func (s *Settings[C]) AddDirectory(d string) *Settings[C] { func (s *Settings[C]) sanitizeDirectories() { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() wd, err := os.Getwd() @@ -163,11 +157,8 @@ func (s *Settings[C]) AddWorkingDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() current, err := os.Getwd() @@ -186,11 +177,8 @@ func (s *Settings[C]) AddEtcDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.mnemonic == "" { @@ -218,11 +206,8 @@ func (s *Settings[C]) AddUserConfigDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.mnemonic == "" { @@ -253,11 +238,8 @@ func (s *Settings[C]) SetDefaultDirectories() *Settings[C] { func (s *Settings[C]) SetFileFormat(format Format) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if slices.Contains(availableFormats, format) { @@ -272,11 +254,8 @@ func (s *Settings[C]) SetFileFormat(format Format) *Settings[C] { // SetFileName sets the name of the configuration file func (s *Settings[C]) SetFileName(name string) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if name == "" { diff --git a/flags.go b/flags.go index 0fb13a8a3d9b50dfcf0a7836bf488c61918ddb75..f53b989c5838d51399c256a5c0645e4d3ae343af 100644 --- a/flags.go +++ b/flags.go @@ -13,20 +13,17 @@ import ( // The file is read from the flag specified by the name func (s *Settings[C]) AddFileFromFlagSet(flagset *flag.FlagSet, name string, format Format) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() - flag := flagset.Lookup(name) - if flag == nil { + flg := flagset.Lookup(name) + if flg == nil { s.errors = append(s.errors, newFlagDoesNotExistError(name)) return s } - path := flag.Value.String() + path := flg.Value.String() if path == "" { s.errors = append(s.errors, FileNameEmptyError) return s @@ -40,22 +37,19 @@ func (s *Settings[C]) InitFromFlagSet(flagset *flag.FlagSet) *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err := runOnTags(&s.config, []string{flagTagKey}, func(k string, field reflect.Value) { - flag := flagset.Lookup(k) - if flag == nil { + flg := flagset.Lookup(k) + if flg == nil { s.errors = append(s.errors, newFlagNotFoundError(k)) return // flag not found } - v := flag.Value + v := flg.Value t := reflect.TypeOf(v) if !field.CanSet() { diff --git a/import.go b/import.go index e277c60bc5c156088831cb82f7a1f6c52daa59e3..28bc4657d5640fa1bdb6a4e98e17397eaa711a0f 100644 --- a/import.go +++ b/import.go @@ -51,11 +51,8 @@ func (s *Settings[C]) importStream(r reader, f ...func(n *C)) { var c C var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err = nil // reset error @@ -141,11 +138,8 @@ func (s *Settings[C]) importFiles() { s.fileWatch.Unlock() }() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() for _, d := range s.files.directories { @@ -198,11 +192,8 @@ func (s *Settings[C]) Import() *Settings[C] { s.importFiles() s.importStreams() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if err := mergo.Merge(&s.config, defaults); err != nil { diff --git a/settings.go b/settings.go index a76b270811f4a5866a7acdafeddf92955b07c370..2014632fb19d9685ffcd25371d688adc6ef32e98 100644 --- a/settings.go +++ b/settings.go @@ -39,11 +39,8 @@ type Settings[C any] struct { func (s *Settings[C]) initDefaults() *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err := runOnTags(&s.config, []string{"default"}, func(v string, field reflect.Value) { diff --git a/watch.go b/watch.go index 4d19b1916e5724f6b7bc9fa63b610ca0e527c597..5b90b6e3501703c852e01fb1036d84d87890bc80 100644 --- a/watch.go +++ b/watch.go @@ -13,11 +13,8 @@ func (s *Settings[C]) initWatch() *Settings[C] { var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher != nil { @@ -40,11 +37,8 @@ func (s *Settings[C]) StopWatching() *Settings[C] { s.fileWatch.Lock() defer s.fileWatch.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher == nil { @@ -89,11 +83,8 @@ func (s *Settings[C]) Watch() *Settings[C] { s.fileWatch.Lock() defer s.fileWatch.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher == nil {