From dd8c39a959d531ab6e16a186daa203935dc621f0 Mon Sep 17 00:00:00 2001 From: Kirill Malikov Date: Thu, 15 Feb 2024 12:01:52 +0300 Subject: [PATCH] minor fixes --- .github/workflows/go.yml | 40 +++++++++++++--------------------------- basic.go | 2 ++ callback.go | 6 ++++-- error.go | 2 +- go.mod | 3 ++- go.sum | 4 ++++ result.go | 10 +++++++--- set/struct.go | 11 ++++------- 8 files changed, 37 insertions(+), 41 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f6bebdd..15a5ae4 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -14,35 +14,21 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: '1.21' - - # - name: Cache - # uses: actions/cache@v4.0.0 - # with: - # # A list of files, directories, and wildcard patterns to cache and restore - # path: - # # An explicit key for restoring and saving the cache - # key: - # # An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note `cache-hit` returns false in this case. - # restore-keys: # optional - # # The chunk size used to split up large files during upload, in bytes - # upload-chunk-size: # optional - # # An optional boolean when enabled, allows windows runners to save or restore caches that can be restored or saved respectively on other platforms - # enableCrossOsArchive: # optional, default is false - # # Fail the workflow if cache entry is not found - # fail-on-cache-miss: # optional, default is false - # # Check if a cache entry exists for the given input(s) (key, restore-keys) without downloading the cache - # lookup-only: # optional, default is false - # # Run the post step to save the cache even if another step before fails - # save-always: # optional, default is false - - - name: Build - run: go build -v ./... + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Cache Go modules + uses: actions/cache@v4 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- - name: Test - run: go test -v ./... + run: go test -v -covermode=count -coverprofile=coverage.out ./... \ No newline at end of file diff --git a/basic.go b/basic.go index 9afff3a..3bd6826 100644 --- a/basic.go +++ b/basic.go @@ -39,6 +39,8 @@ func valueIsEmpty(value reflect.Value, allowZeroValue bool) bool { if value.IsNil() { return true } + + return valueIsEmpty(value.Elem(), allowZeroValue) } return false diff --git a/callback.go b/callback.go index 286fb49..f439ffb 100644 --- a/callback.go +++ b/callback.go @@ -2,7 +2,8 @@ package validator import ( "context" - "errors" + + "github.com/pkg/errors" ) type CallbackFunc[T any] func(ctx context.Context, value T) error @@ -19,7 +20,8 @@ func NewCallback[T any](f CallbackFunc[T]) Callback[T] { func (c Callback[T]) ValidateValue(ctx context.Context, value any) error { v, ok := value.(T) if !ok { - return UnexpectedValueTypeError + var v T + return errors.Wrapf(CallbackUnexpectedValueTypeError, "got %T want %T", value, v) } if err := c.f(ctx, v); err != nil { diff --git a/error.go b/error.go index f0983c4..10fcd96 100644 --- a/error.go +++ b/error.go @@ -7,7 +7,7 @@ import ( var ( NotExistsDataSetIntoContextError = errors.New("not exists data set into context") UnknownOperatorError = errors.New("unknown operator") - UnexpectedValueTypeError = errors.New("unexpected value type") + CallbackUnexpectedValueTypeError = errors.New("callback unexpected value type") ) type ValidationError struct { diff --git a/go.mod b/go.mod index 8048166..d5f4bc3 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,12 @@ go 1.21 require ( github.com/gofrs/uuid v4.4.0+incompatible github.com/stretchr/testify v1.8.4 - golang.org/x/net v0.20.0 + golang.org/x/net v0.21.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 1f120e9..9ba7fb4 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/result.go b/result.go index de38c51..cf03e1e 100644 --- a/result.go +++ b/result.go @@ -24,10 +24,14 @@ func (s Result) Error() string { return s.errors[0].Message } - summary := strings.Builder{} + var summary strings.Builder for _, v := range s.errors { - m := strings.TrimRight(v.Message, ".") - summary.WriteString(m) + if len(v.ValuePath) > 0 { + summary.WriteString(strings.Join(v.ValuePath, ".")) + summary.WriteString(": ") + } + + summary.WriteString(strings.TrimRight(v.Message, ".")) summary.WriteString(". ") } diff --git a/set/struct.go b/set/struct.go index 3bcf715..75f303c 100644 --- a/set/struct.go +++ b/set/struct.go @@ -37,7 +37,7 @@ func NewDataSetStruct(data any) (*DataSetStruct, error) { } // -//func (ds *DataSetStruct) Map() map[string]any { +// func (ds *DataSetStruct) Map() map[string]any { // l := ds.dataType.NumField() // data := make(map[string]any, l) // for i := 0; i < l; i++ { @@ -47,7 +47,7 @@ func NewDataSetStruct(data any) (*DataSetStruct, error) { // } // // return data -//} +// } func (ds *DataSetStruct) FieldValue(name string) (any, error) { fieldValue := ds.dataStruct.FieldByName(name) @@ -55,11 +55,8 @@ func (ds *DataSetStruct) FieldValue(name string) (any, error) { return nil, NewUndefinedFieldError(ds.dataStruct.Interface(), name) } - if fieldValue.Kind() == reflect.Pointer { - if fieldValue.IsNil() { - return nil, nil - } - fieldValue = fieldValue.Elem() + if fieldValue.Kind() == reflect.Pointer && fieldValue.IsNil() { + return nil, nil } return fieldValue.Interface(), nil