Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
slipros committed Feb 15, 2024
1 parent 49da22b commit dd8c39a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
40 changes: 13 additions & 27 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
# 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 ./...
2 changes: 2 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func valueIsEmpty(value reflect.Value, allowZeroValue bool) bool {
if value.IsNil() {
return true
}

return valueIsEmpty(value.Elem(), allowZeroValue)
}

return false
Expand Down
6 changes: 4 additions & 2 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package validator

import (
"context"
"errors"

"github.com/pkg/errors"
)

type CallbackFunc[T any] func(ctx context.Context, value T) error
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 7 additions & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(". ")
}

Expand Down
11 changes: 4 additions & 7 deletions set/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -47,19 +47,16 @@ func NewDataSetStruct(data any) (*DataSetStruct, error) {
// }
//
// return data
//}
// }

func (ds *DataSetStruct) FieldValue(name string) (any, error) {
fieldValue := ds.dataStruct.FieldByName(name)
if !fieldValue.IsValid() {
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
Expand Down

0 comments on commit dd8c39a

Please sign in to comment.