From cf91d56fd1adf229db10e08f2ed3184b6b8dbfeb Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 21 Oct 2024 01:36:30 +0300 Subject: [PATCH] [knf] Code refactoring --- CHANGELOG.md | 1 + knf/knf.go | 16 ++++++++-------- knf/knf_test.go | 6 ++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd5a912c..38a2d4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[errors]` Added new package of utilities for working with errors - `[knf]` Added type `Validators` for `Validator` slice +- `[knf]` Code refactoring - `[options]` Code refactoring - `[errutil]` Package deprecated diff --git a/knf/knf.go b/knf/knf.go index db218550..919053e5 100644 --- a/knf/knf.go +++ b/knf/knf.go @@ -10,7 +10,6 @@ package knf import ( "bytes" - "errors" "fmt" "os" "path" @@ -18,6 +17,7 @@ import ( "sync" "time" + "github.com/essentialkaos/ek/v13/errors" "github.com/essentialkaos/ek/v13/knf/value" "github.com/essentialkaos/ek/v13/sliceutil" ) @@ -415,9 +415,9 @@ func Props(section string) []string { // Validate executes all given validators and // returns slice with validation errors -func Validate(validators Validators) []error { +func Validate(validators Validators) errors.Errors { if global == nil { - return []error{ErrNilConfig} + return errors.Errors{ErrNilConfig} } return global.Validate(validators) @@ -850,12 +850,12 @@ func (c *Config) File() string { // Validate executes all given validators and // returns slice with validation errors -func (c *Config) Validate(validators Validators) []error { +func (c *Config) Validate(validators Validators) errors.Errors { if c == nil || c.mx == nil { - return []error{ErrNilConfig} + return errors.Errors{ErrNilConfig} } - var result []error + var errs errors.Errors c.mx.RLock() @@ -863,13 +863,13 @@ func (c *Config) Validate(validators Validators) []error { err := v.Func(c, v.Property, v.Value) if err != nil { - result = append(result, err) + errs = append(errs, err) } } defer c.mx.RUnlock() - return result + return errs } // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/knf/knf_test.go b/knf/knf_test.go index 47886855..08a33f97 100644 --- a/knf/knf_test.go +++ b/knf/knf_test.go @@ -16,6 +16,8 @@ import ( "testing" "time" + "github.com/essentialkaos/ek/v13/errors" + check "github.com/essentialkaos/check" ) @@ -231,7 +233,7 @@ func (s *KNFSuite) TestErrors(c *check.C) { c.Assert(HasProp("test:test"), check.Equals, false) c.Assert(Sections(), check.HasLen, 0) c.Assert(Props("test"), check.HasLen, 0) - c.Assert(Validate(Validators{}), check.DeepEquals, []error{ErrNilConfig}) + c.Assert(Validate(Validators{}), check.DeepEquals, errors.Errors{ErrNilConfig}) c.Assert(Alias("test:test", "test:test"), check.NotNil) c.Assert(global.Merge(nil), check.NotNil) @@ -623,7 +625,7 @@ func (s *KNFSuite) TestNil(c *check.C) { errs := nilConf.Validate(Validators{}) c.Assert(errs, check.Not(check.HasLen), 0) - c.Assert(errs, check.DeepEquals, []error{ErrNilConfig}) + c.Assert(errs, check.DeepEquals, errors.Errors{ErrNilConfig}) } func (s *KNFSuite) TestDefault(c *check.C) {