Skip to content

Commit

Permalink
[knf] Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 20, 2024
1 parent d72e00d commit cf91d56
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ package knf

import (
"bytes"
"errors"
"fmt"
"os"
"path"
"strings"
"sync"
"time"

"github.com/essentialkaos/ek/v13/errors"
"github.com/essentialkaos/ek/v13/knf/value"
"github.com/essentialkaos/ek/v13/sliceutil"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -850,26 +850,26 @@ 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()

for _, v := range validators {
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
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
6 changes: 4 additions & 2 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"testing"
"time"

"github.com/essentialkaos/ek/v13/errors"

check "github.com/essentialkaos/check"
)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cf91d56

Please sign in to comment.