mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
doc(try): adding documentation for Try****
This commit is contained in:
53
CHANGELOG.md
53
CHANGELOG.md
@@ -1,5 +1,58 @@
|
||||
# Changelog
|
||||
|
||||
## 1.10.0 (2022-03-11)
|
||||
|
||||
Adding:
|
||||
|
||||
- Range
|
||||
- RangeFrom
|
||||
- RangeWithSteps
|
||||
|
||||
## 1.9.0 (2022-03-10)
|
||||
|
||||
Added
|
||||
|
||||
- Drop
|
||||
- DropRight
|
||||
- DropWhile
|
||||
- DropRightWhile
|
||||
|
||||
## 1.8.0 (2022-03-10)
|
||||
|
||||
Adding Union.
|
||||
|
||||
## 1.7.0 (2022-03-09)
|
||||
|
||||
Adding ContainBy
|
||||
|
||||
Adding MapValues
|
||||
|
||||
Adding FlatMap
|
||||
|
||||
## 1.6.0 (2022-03-07)
|
||||
|
||||
Fixed PartitionBy.
|
||||
|
||||
Adding Sample
|
||||
|
||||
Adding Samples
|
||||
|
||||
## 1.5.0 (2022-03-07)
|
||||
|
||||
Adding Times
|
||||
|
||||
Adding Attempt
|
||||
|
||||
Adding Repeat
|
||||
|
||||
## 1.4.0 (2022-03-07)
|
||||
|
||||
- adding tuple types (2->9)
|
||||
- adding Zip + Unzip
|
||||
- adding lo.PartitionBy + lop.PartitionBy
|
||||
- adding lop.GroupBy
|
||||
- fixing Nth
|
||||
|
||||
## 1.3.0 (2022-03-03)
|
||||
|
||||
Last and Nth return errors
|
||||
|
89
README.md
89
README.md
@@ -116,6 +116,13 @@ Other functional programming helpers:
|
||||
- Attempt
|
||||
- Range / RangeFrom / RangeWithSteps
|
||||
|
||||
Error handling:
|
||||
|
||||
- Try
|
||||
- TryCatch
|
||||
- TryWithErrorValue
|
||||
- TryCatchWithErrorValue
|
||||
|
||||
Constraints:
|
||||
|
||||
- Clonable
|
||||
@@ -861,6 +868,8 @@ iter, err := lo.Attempt(0, func(i int) error {
|
||||
// nil
|
||||
```
|
||||
|
||||
For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff).
|
||||
|
||||
### Range / RangeFrom / RangeWithSteps
|
||||
|
||||
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.
|
||||
@@ -891,7 +900,85 @@ result := Range(0);
|
||||
// []
|
||||
```
|
||||
|
||||
For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff).
|
||||
## Try
|
||||
|
||||
Calls the function and return false in case of error and on panic.
|
||||
|
||||
```go
|
||||
ok := lo.Try(func() error {
|
||||
panic("error")
|
||||
return nil
|
||||
})
|
||||
// false
|
||||
|
||||
ok := lo.Try(func() error {
|
||||
return nil
|
||||
})
|
||||
// true
|
||||
|
||||
ok := lo.Try(func() error {
|
||||
return fmt.Errorf("error")
|
||||
})
|
||||
// false
|
||||
```
|
||||
|
||||
## Try{0->6}
|
||||
|
||||
The same behavior than `Try`, but callback returns 2 variables.
|
||||
|
||||
```go
|
||||
ok := lo.Try2(func() (string, error) {
|
||||
panic("error")
|
||||
return "", nil
|
||||
})
|
||||
// false
|
||||
```
|
||||
|
||||
## TryWithErrorValue
|
||||
|
||||
The same behavior than `Try`, but also returns value passed to panic.
|
||||
|
||||
```go
|
||||
err, ok := lo.TryWithErrorValue(func() error {
|
||||
panic("error")
|
||||
return nil
|
||||
})
|
||||
// "error", false
|
||||
```
|
||||
|
||||
## TryCatch
|
||||
|
||||
The same behavior than `Try`, but calls the catch function in case of error.
|
||||
|
||||
```go
|
||||
caught := false
|
||||
|
||||
ok := lo.TryCatch(func() error {
|
||||
panic("error")
|
||||
return nil
|
||||
}, func() {
|
||||
caught = true
|
||||
})
|
||||
// false
|
||||
// caught == true
|
||||
```
|
||||
|
||||
## TryCatchWithErrorValue
|
||||
|
||||
The same behavior than `TryWithErrorValue`, but calls the catch function in case of error.
|
||||
|
||||
```go
|
||||
caught := false
|
||||
|
||||
ok := lo.TryCatchWithErrorValue(func() error {
|
||||
panic("error")
|
||||
return nil
|
||||
}, func(val any) {
|
||||
caught = val == "error"
|
||||
})
|
||||
// false
|
||||
// caught == true
|
||||
```
|
||||
|
||||
## 🛩 Benchmark
|
||||
|
||||
|
28
try.go
28
try.go
@@ -1,5 +1,6 @@
|
||||
package lo
|
||||
|
||||
// Try calls the function and return false in case of error.
|
||||
func Try(callback func() error) (ok bool) {
|
||||
ok = true
|
||||
|
||||
@@ -17,6 +18,20 @@ func Try(callback func() error) (ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Try0 has the same behavior than Try, but callback returns no variable.
|
||||
func Try0[T any](callback func()) bool {
|
||||
return Try(func() error {
|
||||
callback()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Try1 is an alias to Try
|
||||
func Try1[T any](callback func() error) bool {
|
||||
return Try(callback)
|
||||
}
|
||||
|
||||
// Try2 has the same behavior than Try, but callback returns 2 variables.
|
||||
func Try2[T any](callback func() (T, error)) bool {
|
||||
return Try(func() error {
|
||||
_, err := callback()
|
||||
@@ -24,6 +39,7 @@ func Try2[T any](callback func() (T, error)) bool {
|
||||
})
|
||||
}
|
||||
|
||||
// Try3 has the same behavior than Try, but callback returns 3 variables.
|
||||
func Try3[T, R any](callback func() (T, R, error)) bool {
|
||||
return Try(func() error {
|
||||
_, _, err := callback()
|
||||
@@ -31,6 +47,7 @@ func Try3[T, R any](callback func() (T, R, error)) bool {
|
||||
})
|
||||
}
|
||||
|
||||
// Try4 has the same behavior than Try, but callback returns 4 variables.
|
||||
func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
|
||||
return Try(func() error {
|
||||
_, _, _, err := callback()
|
||||
@@ -38,6 +55,7 @@ func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
|
||||
})
|
||||
}
|
||||
|
||||
// Try5 has the same behavior than Try, but callback returns 5 variables.
|
||||
func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
|
||||
return Try(func() error {
|
||||
_, _, _, _, err := callback()
|
||||
@@ -45,6 +63,7 @@ func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
|
||||
})
|
||||
}
|
||||
|
||||
// Try6 has the same behavior than Try, but callback returns 6 variables.
|
||||
func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
|
||||
return Try(func() error {
|
||||
_, _, _, _, _, err := callback()
|
||||
@@ -52,6 +71,7 @@ func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
|
||||
})
|
||||
}
|
||||
|
||||
// TryWithErrorValue has the same behavior than Try, but also returns value passed to panic.
|
||||
func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
|
||||
ok = true
|
||||
|
||||
@@ -71,20 +91,16 @@ func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
//TODO: Overloads for TryWithErrorValue
|
||||
|
||||
// TryCatch has the same behavior than Try, but calls the catch function in case of error.
|
||||
func TryCatch(callback func() error, catch func()) {
|
||||
if !Try(callback) {
|
||||
catch()
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Overloads for TryCatch
|
||||
|
||||
// TryCatchWithErrorValue has the same behavior than TryWithErrorValue, but calls the catch function in case of error.
|
||||
func TryCatchWithErrorValue(callback func() error, catch func(any)) {
|
||||
if err, ok := TryWithErrorValue(callback); !ok {
|
||||
catch(err)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Overloads for TryCatchWithErrorValue
|
||||
|
@@ -2,8 +2,10 @@ package lo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTry(t *testing.T) {
|
||||
@@ -16,6 +18,9 @@ func TestTry(t *testing.T) {
|
||||
is.True(Try(func() error {
|
||||
return nil
|
||||
}))
|
||||
is.False(Try(func() error {
|
||||
return fmt.Errorf("fail")
|
||||
}))
|
||||
}
|
||||
|
||||
func TestTryFunctions(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user