From bf26a2731048a7cfff0153c0594776e283136aea Mon Sep 17 00:00:00 2001 From: Masakazu Ohtsuka Date: Fri, 16 Sep 2022 15:08:31 +0300 Subject: [PATCH 1/2] feat: add FromSlicePtr --- README.md | 13 +++++++++++++ type_manipulation.go | 11 +++++++++++ type_manipulation_test.go | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 7fec142e..59575757 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ Type manipulation helpers: - [FromPtr](#fromptr) - [FromPtrOr](#fromptror) - [ToSlicePtr](#tosliceptr) +- [FromSlicePtr](#fromsliceptr) - [ToAnySlice](#toanyslice) - [FromAnySlice](#fromanyslice) - [Empty](#empty) @@ -1681,6 +1682,18 @@ ptr := lo.ToSlicePtr[string]([]string{"hello", "world"}) // []*string{"hello", "world"} ``` +### FromSlicePtr + +FromSlicePtr returns a slice with the pointer values. +Returns a zero value in case of a nil pointer element. + +```go +str1 := "hello" +str2 := "world" +ptr := lo.FromSlicePtr[string]([]*string{&str1, &str2, nil}) +// []string{"hello", "world", ""} +``` + ### ToAnySlice Returns a slice with all elements mapped to `any` type. diff --git a/type_manipulation.go b/type_manipulation.go index fe99ee1f..bfd64715 100644 --- a/type_manipulation.go +++ b/type_manipulation.go @@ -30,6 +30,17 @@ func ToSlicePtr[T any](collection []T) []*T { }) } +// FromSlicePtr returns a slice with the pointer values. +// Returns a zero value in case of a nil pointer element. +func FromSlicePtr[T any](collection []*T) []T { + return Map(collection, func(x *T, _ int) T { + if x == nil { + return Empty[T]() + } + return *x + }) +} + // ToAnySlice returns a slice with all elements mapped to `any` type func ToAnySlice[T any](collection []T) []any { result := make([]any, len(collection)) diff --git a/type_manipulation_test.go b/type_manipulation_test.go index c5553ed2..9685085e 100644 --- a/type_manipulation_test.go +++ b/type_manipulation_test.go @@ -54,6 +54,16 @@ func TestToSlicePtr(t *testing.T) { is.Equal(result1, []*string{&str1, &str2}) } +func TestFromSlicePtr(t *testing.T) { + is := assert.New(t) + + str1 := "foo" + str2 := "bar" + result1 := FromSlicePtr([]*string{&str1, &str2, nil}) + + is.Equal(result1, []string{str1, str2, ""}) +} + func TestToAnySlice(t *testing.T) { is := assert.New(t) From 0550431f8a3d04367e60bd2ecc7ad7d70472a84f Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Fri, 26 Jul 2024 07:48:23 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59575757..c862fa2a 100644 --- a/README.md +++ b/README.md @@ -1684,14 +1684,20 @@ ptr := lo.ToSlicePtr[string]([]string{"hello", "world"}) ### FromSlicePtr -FromSlicePtr returns a slice with the pointer values. +Returns a slice with the pointer values. Returns a zero value in case of a nil pointer element. ```go str1 := "hello" str2 := "world" + ptr := lo.FromSlicePtr[string]([]*string{&str1, &str2, nil}) // []string{"hello", "world", ""} + +ptr := lo.Compact( + lo.FromSlicePtr[string]([]*string{&str1, &str2, nil}), +) +// []string{"hello", "world"} ``` ### ToAnySlice