Skip to content
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
5 changes: 5 additions & 0 deletions gptr/gptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func IsNotNil[T any](p *T) bool {
return p != nil
}

// IsNilOrZero returns whether the given pointer p is nil or the value it points to is zero.
func IsNilOrZero[T comparable](p *T) bool {
return p == nil || gvalue.IsZero(*p)
}

// Clone returns a shallow copy of the slice.
// If the given pointer is nil, nil is returned.
//
Expand Down
8 changes: 8 additions & 0 deletions gptr/gptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func TestIsNil(t *testing.T) {
assert.True(t, IsNil[int](nil))
}

func TestIsNilOrZero(t *testing.T) {
assert.False(t, IsNilOrZero(Of(1)))
assert.False(t, IsNilOrZero(Of("Alice")))
assert.True(t, IsNilOrZero(Of(0)))
assert.True(t, IsNilOrZero(Of("")))
assert.True(t, IsNilOrZero[int](nil))
}

func TestEqual(t *testing.T) {
ptr := Of(1)
assert.True(t, Equal(ptr, ptr))
Expand Down
Loading