Skip to content

Commit

Permalink
Merge pull request #10 from kuchaguangjie/eric
Browse files Browse the repository at this point in the history
Added an `lambdas.Assert()` as supplement of `lambdas.Must()` for cases function only return an error
  • Loading branch information
orsinium authored Jun 17, 2022
2 parents 3738175 + f47b25f commit 7ccbcc9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
__pycache__/
.mypy_cache/

# goland
.idea

# vscode
.vscode

# linux temp files,
.swp
*~

39 changes: 20 additions & 19 deletions lambdas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ These functions are especially helpful in combination with other `genesis` packa
Available functions:

1. EqualTo
1. LessThan
1. Not
1. IsZero
1. IsNotZero
1. IsEmpty
1. IsNotEmpty
1. IsDefault
1. IsNotDefault
1. IsNaN
1. IsNotNaN
1. IsNil
1. IsNotNil
1. Must
1. Safe
1. DefaultTo
1. Abs
1. Min
1. Max
1. Default
2. LessThan
3. Not
4. IsZero
5. IsNotZero
6. IsEmpty
7. IsNotEmpty
8. IsDefault
9. IsNotDefault
10. IsNaN
11. IsNotNaN
12. IsNil
13. IsNotNil
14. Must
15. Ensure
16. Safe
17. DefaultTo
18. Abs
19. Min
20. Max
21. Default
8 changes: 8 additions & 0 deletions lambdas/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ func Must[T any](val T, err error) T {
return val
}

// Ensure wraps a function invicotaion and panic if it returned an error.
// Compared to Must, in Ensure the called function only returns an error.
func Ensure(err error) {
if err != nil {
panic(err)
}
}

// Safe wraps a function invicotaion and returns the empty value if it returned an error.
func Safe[T any](val T, err error) T {
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions lambdas/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func TestMust(t *testing.T) {
is.Equal(res, 13)
}

func TestEnsure(t *testing.T) {
f := func() error { return nil }
lambdas.Ensure(f())
}

func TestSafe(t *testing.T) {
is := is.New(t)
f1 := func() (int, error) { return 13, nil }
Expand Down

0 comments on commit 7ccbcc9

Please sign in to comment.