vendor
This commit is contained in:
parent
07ec5529ac
commit
f501abe660
532 changed files with 271781 additions and 0 deletions
22
vendor/github.com/samber/mo/do.go
generated
vendored
Normal file
22
vendor/github.com/samber/mo/do.go
generated
vendored
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package mo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Do executes a function within a monadic context, capturing any errors that occur.
|
||||
// If the function executes successfully, its result is wrapped in a successful Result.
|
||||
// If the function panics (indicating a failure), the panic is caught and converted into an error Result.
|
||||
func Do[T any](fn func() T) (result Result[T]) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if err, ok := r.(error); ok {
|
||||
result = Err[T](err)
|
||||
} else {
|
||||
result = Err[T](errors.New(fmt.Sprint(r)))
|
||||
}
|
||||
}
|
||||
}()
|
||||
return Ok(fn())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue