mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
fix: avoid recover() if possible (#685)
This commit is contained in:
@@ -5,8 +5,16 @@ import "reflect"
|
|||||||
// IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
|
// IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
|
||||||
// Play: https://go.dev/play/p/P2sD0PMXw4F
|
// Play: https://go.dev/play/p/P2sD0PMXw4F
|
||||||
func IsNil(x any) bool {
|
func IsNil(x any) bool {
|
||||||
defer func() { recover() }() // nolint:errcheck
|
if x == nil {
|
||||||
return x == nil || reflect.ValueOf(x).IsNil()
|
return true
|
||||||
|
}
|
||||||
|
v := reflect.ValueOf(x)
|
||||||
|
switch v.Kind() { // nolint:exhaustive
|
||||||
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
|
||||||
|
return v.IsNil()
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsNotNil checks if a value is not nil or if it's not a reference type with a nil underlying value.
|
// IsNotNil checks if a value is not nil or if it's not a reference type with a nil underlying value.
|
||||||
@@ -107,20 +115,16 @@ func ToAnySlice[T any](collection []T) []any {
|
|||||||
// FromAnySlice returns a slice with all elements mapped to a type.
|
// FromAnySlice returns a slice with all elements mapped to a type.
|
||||||
// Returns false in case of type conversion failure.
|
// Returns false in case of type conversion failure.
|
||||||
// Play: https://go.dev/play/p/P2sD0PMXw4F
|
// Play: https://go.dev/play/p/P2sD0PMXw4F
|
||||||
func FromAnySlice[T any](in []any) (out []T, ok bool) {
|
func FromAnySlice[T any](in []any) ([]T, bool) {
|
||||||
defer func() {
|
out := make([]T, len(in))
|
||||||
if r := recover(); r != nil {
|
|
||||||
out = []T{}
|
|
||||||
ok = false
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
out = make([]T, len(in))
|
|
||||||
ok = true
|
|
||||||
for i := range in {
|
for i := range in {
|
||||||
out[i] = in[i].(T) //nolint:errcheck,forcetypeassert
|
t, ok := in[i].(T)
|
||||||
|
if !ok {
|
||||||
|
return []T{}, false
|
||||||
|
}
|
||||||
|
out[i] = t
|
||||||
}
|
}
|
||||||
return out, ok
|
return out, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty returns the zero value (https://go.dev/ref/spec#The_zero_value).
|
// Empty returns the zero value (https://go.dev/ref/spec#The_zero_value).
|
||||||
|
Reference in New Issue
Block a user