From e601222237341149b2866e5fdd5b54869aa9f1d7 Mon Sep 17 00:00:00 2001 From: Xin Hao Date: Tue, 20 Aug 2024 16:25:47 +0800 Subject: [PATCH 1/2] Add IsNotNil --- README.md | 27 ++++++++++++++++++++++++++- type_manipulation.go | 5 +++++ type_manipulation_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 836e608b..93995ad9 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ Conditional helpers: Type manipulation helpers: - [IsNil](#isnil) +- [IsNotNil](#isnotnil) - [ToPtr](#toptr) - [Nil](#nil) - [EmptyableToPtr](#emptyabletoptr) @@ -1062,7 +1063,7 @@ keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 3}) ### UniqKeys -Creates an array of unique map keys. +Creates an array of unique map keys. ```go keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3}) @@ -2661,6 +2662,30 @@ ifaceWithNilValue == nil // false ``` +### IsNotNil + +Checks if a value is not nil or if it's not a reference type with a nil underlying value. + +```go +var x int +IsNotNil(x) +// true + +var k struct{} +IsNotNil(k) +// true + +var i *int +IsNotNil(i) +// false + +var ifaceWithNilValue any = (*string)(nil) +IsNotNil(ifaceWithNilValue) +// false +ifaceWithNilValue == nil +// true +``` + ### ToPtr Returns a pointer copy of the value. diff --git a/type_manipulation.go b/type_manipulation.go index ef070281..fb8bba48 100644 --- a/type_manipulation.go +++ b/type_manipulation.go @@ -8,6 +8,11 @@ func IsNil(x any) bool { return x == nil || reflect.ValueOf(x).IsNil() } +// IsNotNil checks if a value is not nil or if it's not a reference type with a nil underlying value. +func IsNotNil(x any) bool { + return !IsNil(x) +} + // ToPtr returns a pointer copy of value. func ToPtr[T any](x T) *T { return &x diff --git a/type_manipulation_test.go b/type_manipulation_test.go index 3a63013d..d7a2ee1d 100644 --- a/type_manipulation_test.go +++ b/type_manipulation_test.go @@ -30,6 +30,30 @@ func TestIsNil(t *testing.T) { is.False(ifaceWithNilValue == nil) //nolint:staticcheck } +func TestIsNotNil(t *testing.T) { + t.Parallel() + is := assert.New(t) + + var x int + is.True(IsNotNil(x)) + + var k struct{} + is.True(IsNotNil(k)) + + var s *string + is.False(IsNotNil(s)) + + var i *int + is.False(IsNotNil(i)) + + var b *bool + is.False(IsNotNil(b)) + + var ifaceWithNilValue any = (*string)(nil) //nolint:staticcheck + is.False(IsNotNil(ifaceWithNilValue)) + is.True(ifaceWithNilValue != nil) //nolint:staticcheck +} + func TestToPtr(t *testing.T) { t.Parallel() is := assert.New(t) From 33e5749a5c9d889f56a6118ac254a28f03c3b386 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sun, 26 Jan 2025 16:17:57 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 93995ad9..b4c9ded0 100644 --- a/README.md +++ b/README.md @@ -2668,19 +2668,19 @@ Checks if a value is not nil or if it's not a reference type with a nil underlyi ```go var x int -IsNotNil(x) +lo.IsNotNil(x) // true var k struct{} -IsNotNil(k) +lo.IsNotNil(k) // true var i *int -IsNotNil(i) +lo.IsNotNil(i) // false var ifaceWithNilValue any = (*string)(nil) -IsNotNil(ifaceWithNilValue) +lo.IsNotNil(ifaceWithNilValue) // false ifaceWithNilValue == nil // true