Skip to content

feat(values): Add IsType/IsZero/IsEmpty #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions values/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,30 @@ func Val[T any](value *T) T {
var zero T
return zero
}

// IsType returns true if the value is the type.
//
// IsType[int](1) // true
// IsType[int]("foo") // false
func IsType[T any](value any) bool {
_, ok := value.(T)
return ok
}

// IsZero returns true if the value is zero.
//
// IsZero(0) // true
// IsZero("") // true
// IsZero("foo") // false
func IsZero[T comparable](value T) bool {
var zero T
return value == zero
}

// IsEmpty returns true if the value is zero.
//
// IsEmpty(0) // true
// IsEmpty("") // true
func IsEmpty[T comparable](value T) bool {
return IsZero(value)
}
64 changes: 64 additions & 0 deletions values/values_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package values

import (
"errors"
"strconv"
"testing"
"time"
Expand All @@ -13,6 +14,14 @@ type foo struct {
Age int
}

type testInterface interface {
testFunc()
}

type testStruct struct{}

func (t testStruct) testFunc() {}

func TestTap_Struct(t *testing.T) {
f := &foo{Name: "foo"}

Expand Down Expand Up @@ -245,3 +254,58 @@ func TestPtrAndVal(t *testing.T) {
got6 := Val(nilVal)
assert.Equal(t, 0, got6)
}

func TestIsType(t *testing.T) {
assert.True(t, IsType[int](10))
assert.False(t, IsType[int](int8(10)))

assert.True(t, IsType[string]("foo"))
assert.False(t, IsType[string](10))

assert.True(t, IsType[time.Time](time.Now()))
assert.False(t, IsType[time.Time](10))

assert.True(t, IsType[foo](foo{}))
assert.False(t, IsType[foo](10))

assert.True(t, IsType[*foo](&foo{}))
assert.False(t, IsType[foo](&foo{}))
assert.False(t, IsType[*foo](nil))

assert.True(t, IsType[testInterface](testStruct{}))
assert.True(t, IsType[interface{}](testStruct{}))
assert.True(t, IsType[any](testStruct{}))

assert.True(t, IsType[error](errors.New("foo")))
assert.False(t, IsType[error](nil))
}

func TestIsZero(t *testing.T) {
assert.True(t, IsZero(0))
assert.True(t, IsZero(""))
assert.True(t, IsZero(false))
assert.True(t, IsZero[*foo](nil))
assert.True(t, IsZero(0.0))
assert.True(t, IsZero(0.0+0i))

assert.False(t, IsZero(1))
assert.False(t, IsZero("foo"))
assert.False(t, IsZero(true))
assert.False(t, IsZero(1.0))
assert.False(t, IsZero(1.0+0i))
}

func TestIsEmpty(t *testing.T) {
assert.True(t, IsEmpty(""))
assert.True(t, IsEmpty[*foo](nil))
assert.True(t, IsEmpty(0))
assert.True(t, IsEmpty(false))
assert.True(t, IsEmpty(0.0))
assert.True(t, IsEmpty(0.0+0i))

assert.False(t, IsEmpty("foo"))
assert.False(t, IsEmpty(1))
assert.False(t, IsEmpty(true))
assert.False(t, IsEmpty(1.0))
assert.False(t, IsEmpty(1.0+0i))
}