Skip to content

Commit

Permalink
Fix corner case for IsEmpty + up dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Mar 29, 2024
1 parent bac4904 commit 5ae00de
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ bench: ## Run benchmarks
lint: ## Run linter
golangci-lint run -v ./...

.PHONY: tidy
tidy: ## Run go mod tidy
go mod tidy

.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module github.com/demdxx/gocast/v2

go 1.18

require github.com/stretchr/testify v1.8.4
require (
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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/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/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
9 changes: 7 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func IsEmpty[T any](v T) bool {
return len(tv) == 0
case []bool:
return len(tv) == 0
case []string:
return len(tv) == 0
}
return IsEmptyByReflection(reflect.ValueOf(v))
}
Expand All @@ -76,8 +78,11 @@ func IsEmptyByReflection(v reflect.Value) bool {
return true
}
switch v.Kind() {
case reflect.Interface, reflect.Pointer:
return v.IsNil()
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return true
}
return IsEmptyByReflection(v.Elem())
case reflect.Bool:
return !v.Bool()
case reflect.Slice, reflect.Array, reflect.Map, reflect.String:
Expand Down
4 changes: 4 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func TestEmpty(t *testing.T) {
{src: "123", target: false},
{src: "", target: true},
{src: nil, target: true},
{src: func() *struct{ s string } { return nil }(), target: true},
{src: any(nil), target: true},
{src: any(func() *struct{ s string } { return nil }()), target: true},
{src: []byte("125."), target: false},
{src: []byte(""), target: true},
{src: true, target: false},
Expand All @@ -46,6 +49,7 @@ func TestEmpty(t *testing.T) {
{src: []float32{1}, target: false},
{src: []float64{1}, target: false},
{src: []bool{}, target: true},
{src: []string{}, target: true},
}

t.Run("IsEmpty", func(t *testing.T) {
Expand Down

0 comments on commit 5ae00de

Please sign in to comment.