Skip to content

Commit bcb54cc

Browse files
authored
Adding TryOr and TryOrX (#229)
feat: adding TryOr and TryOrX
1 parent edda239 commit bcb54cc

File tree

4 files changed

+321
-1
lines changed

4 files changed

+321
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Adding:
88

99
- lo.ErrorAs
10+
- lo.TryOr
11+
- lo.TryOrX
1012

1113
## 1.28.0 (2022-09-05)
1214

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ Error handling:
209209

210210
- [Must](#must)
211211
- [Try](#try)
212+
- [Try1 -> Try6](#try0-6)
213+
- [TryOr](#tryor)
214+
- [TryOr1 -> TryOr6](#tryor0-6)
212215
- [TryCatch](#trycatch)
213216
- [TryWithErrorValue](#trywitherrorvalue)
214217
- [TryCatchWithErrorValue](#trycatchwitherrorvalue)
@@ -2026,6 +2029,45 @@ ok := lo.Try2(func() (string, error) {
20262029
// false
20272030
```
20282031

2032+
### TryOr
2033+
2034+
Calls the function and return a default value in case of error and on panic.
2035+
2036+
```go
2037+
str, ok := lo.TryOr(func() (string, error) {
2038+
panic("error")
2039+
return "hello", nil
2040+
}, "world")
2041+
// world
2042+
// false
2043+
2044+
ok := lo.TryOr(func() error {
2045+
return "hello", nil
2046+
}, "world")
2047+
// hello
2048+
// true
2049+
2050+
ok := lo.TryOr(func() error {
2051+
return "hello", fmt.Errorf("error")
2052+
}, "world")
2053+
// world
2054+
// false
2055+
```
2056+
2057+
### TryOr{0->6}
2058+
2059+
The same behavior than `TryOr`, but callback returns 2 variables.
2060+
2061+
```go
2062+
str, nbr, ok := lo.TryOr2(func() (string, int, error) {
2063+
panic("error")
2064+
return "hello", 42, nil
2065+
}, "world", 21)
2066+
// world
2067+
// 21
2068+
// false
2069+
```
2070+
20292071
### TryWithErrorValue
20302072

20312073
The same behavior than `Try`, but also returns value passed to panic.

errors.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,116 @@ func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
167167
})
168168
}
169169

170+
// TryOr has the same behavior than Must, but returns a default value in case of error.
171+
func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool) {
172+
return TryOr1(callback, fallbackA)
173+
}
174+
175+
// TryOr1 has the same behavior than Must, but returns a default value in case of error.
176+
func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool) {
177+
ok := false
178+
179+
Try0(func() {
180+
a, err := callback()
181+
if err == nil {
182+
fallbackA = a
183+
ok = true
184+
}
185+
})
186+
187+
return fallbackA, ok
188+
}
189+
190+
// TryOr2 has the same behavior than Must, but returns a default value in case of error.
191+
func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool) {
192+
ok := false
193+
194+
Try0(func() {
195+
a, b, err := callback()
196+
if err == nil {
197+
fallbackA = a
198+
fallbackB = b
199+
ok = true
200+
}
201+
})
202+
203+
return fallbackA, fallbackB, ok
204+
}
205+
206+
// TryOr3 has the same behavior than Must, but returns a default value in case of error.
207+
func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool) {
208+
ok := false
209+
210+
Try0(func() {
211+
a, b, c, err := callback()
212+
if err == nil {
213+
fallbackA = a
214+
fallbackB = b
215+
fallbackC = c
216+
ok = true
217+
}
218+
})
219+
220+
return fallbackA, fallbackB, fallbackC, ok
221+
}
222+
223+
// TryOr4 has the same behavior than Must, but returns a default value in case of error.
224+
func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool) {
225+
ok := false
226+
227+
Try0(func() {
228+
a, b, c, d, err := callback()
229+
if err == nil {
230+
fallbackA = a
231+
fallbackB = b
232+
fallbackC = c
233+
fallbackD = d
234+
ok = true
235+
}
236+
})
237+
238+
return fallbackA, fallbackB, fallbackC, fallbackD, ok
239+
}
240+
241+
// TryOr5 has the same behavior than Must, but returns a default value in case of error.
242+
func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool) {
243+
ok := false
244+
245+
Try0(func() {
246+
a, b, c, d, e, err := callback()
247+
if err == nil {
248+
fallbackA = a
249+
fallbackB = b
250+
fallbackC = c
251+
fallbackD = d
252+
fallbackE = e
253+
ok = true
254+
}
255+
})
256+
257+
return fallbackA, fallbackB, fallbackC, fallbackD, fallbackE, ok
258+
}
259+
260+
// TryOr6 has the same behavior than Must, but returns a default value in case of error.
261+
func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool) {
262+
ok := false
263+
264+
Try0(func() {
265+
a, b, c, d, e, f, err := callback()
266+
if err == nil {
267+
fallbackA = a
268+
fallbackB = b
269+
fallbackC = c
270+
fallbackD = d
271+
fallbackE = e
272+
fallbackF = f
273+
ok = true
274+
}
275+
})
276+
277+
return fallbackA, fallbackB, fallbackC, fallbackD, fallbackE, fallbackF, ok
278+
}
279+
170280
// TryWithErrorValue has the same behavior than Try, but also returns value passed to panic.
171281
func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
172282
ok = true

0 commit comments

Comments
 (0)