From bff27b4198dc49e3f7420d80b33d42dd31d816d0 Mon Sep 17 00:00:00 2001 From: tgallice Date: Tue, 23 Jun 2026 09:57:01 +0200 Subject: [PATCH] slices/maps: Return nil if source is nil --- slices/map.go | 4 ++++ slices/map_test.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/slices/map.go b/slices/map.go index c54de57..a1bce4e 100644 --- a/slices/map.go +++ b/slices/map.go @@ -7,6 +7,10 @@ import "context" // MapWithContextError transforms a slice of a given type to a new slice of a // different type using a context and can return an error. func MapWithContextError[T, K any](ctx context.Context, ss []T, fn func(context.Context, T) (K, error)) ([]K, error) { + if ss == nil { + return nil, nil + } + var res = make([]K, len(ss)) for i, v := range ss { diff --git a/slices/map_test.go b/slices/map_test.go index f2939e5..a686d13 100644 --- a/slices/map_test.go +++ b/slices/map_test.go @@ -41,6 +41,12 @@ func TestMapWithContextError(t *testing.T) { }, want: []int64{1, 22}, }, + { + name: "nil", + fn: func(context.Context, string) (int64, error) { + return 0, nil + }, + }, } { t.Run(tt.name, func(t *testing.T) { got, err := MapWithContextError(ctx, tt.in, tt.fn)