forked from samber/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
59 lines (53 loc) · 1.67 KB
/
utils.go
File metadata and controls
59 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package oops
import (
"context"
"github.com/samber/lo"
)
// coalesceOrEmpty returns the first non-zero value from the provided arguments,
// or the zero value of the type if all arguments are zero values.
//
// This function is a wrapper around lo.Coalesce that simplifies the return
// value handling by always returning a value (either the first non-zero value
// or the zero value of the type).
//
// Example usage:
//
// result := coalesceOrEmpty("", "default", "fallback") // returns "default"
// result := coalesceOrEmpty(0, 42, 100) // returns 42
// result := coalesceOrEmpty("", "", "") // returns ""
func coalesceOrEmpty[T comparable](v ...T) T {
result, _ := lo.Coalesce(v...)
return result
}
// contextValueOrNil safely extracts a value from a Go context, handling
// nil contexts and nil values appropriately.
//
// This function provides a safe way to extract values from contexts without
// causing panics. It handles the case where the context is nil and also
// properly handles nil values stored in the context.
//
// The function is particularly useful when working with context values that
// might be nil or when the context itself might be nil, which can happen
// in certain edge cases in web applications or when contexts are not
// properly initialized.
//
// Example usage:
//
// value := contextValueOrNil(ctx, "user_id")
// if value != nil {
// userID := value.(string)
// // Use userID safely
// }
//
// // Safe even with nil context
// value := contextValueOrNil(nil, "key") // returns nil
func contextValueOrNil(ctx context.Context, k any) any {
if ctx == nil {
return nil
}
v := ctx.Value(k)
if v == nil {
return nil
}
return v
}