Skip to content

Commit

Permalink
feat(must): adding must0, must1, must2, must3, must4, must5 and must6
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 11, 2022
1 parent 512356c commit 986f172
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 21 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,28 @@ val := Must(time.Parse("2006-01-02", "bad-value"))
// panics
```

### Must{0->6}

Wraps a function call to return values if the error is nil, panics otherwise.

```go
func example0() (error)
func example1() (int, error)
func example2() (int, string, error)
func example3() (int, string, time.Date, error)
func example4() (int, string, time.Date, bool, error)
func example5() (int, string, time.Date, bool, float64, error)
func example6() (int, string, time.Date, bool, float64, byte, error)

Must0(example0)
val1 := Must1(example1) // alias to Must
val1, val2 := Must2(example2)
val1, val2, val3 := Must3(example3)
val1, val2, val3, val4 := Must4(example4)
val1, val2, val3, val4, val5 := Must5(example5)
val1, val2, val3, val4, val5, val6 := Must6(example6)
```

## Try

Calls the function and return false in case of error and on panic.
Expand Down
69 changes: 68 additions & 1 deletion try.go → errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
package lo

// Must is a helper that wraps a call to a function returning a value and an error
// and panics if the error is non-nil.
func Must[T any](val T, err error) T {
if err != nil {
panic(err)
}

return val
}

// Must0 has the same behavior than Must, but callback returns no variable.
func Must0(err error) {
if err != nil {
panic(err)
}
}

// Must1 is an alias to Must
func Must1[T any](val T, err error) T {
return Must(val, err)
}

// Must2 has the same behavior than Must, but callback returns 2 variable2.
func Must2[T1 any, T2 any](val1 T1, val2 T2, err error) (T1, T2) {
if err != nil {
panic(err)
}

return val1, val2
}

// Must3 has the same behavior than Must, but callback returns 2 variable2.
func Must3[T1 any, T2 any, T3 any](val1 T1, val2 T2, val3 T3, err error) (T1, T2, T3) {
if err != nil {
panic(err)
}

return val1, val2, val3
}

// Must4 has the same behavior than Must, but callback returns 2 variable2.
func Must4[T1 any, T2 any, T3 any, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err error) (T1, T2, T3, T4) {
if err != nil {
panic(err)
}

return val1, val2, val3, val4
}

// Must5 has the same behavior than Must, but callback returns 2 variable2.
func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err error) (T1, T2, T3, T4, T5) {
if err != nil {
panic(err)
}

return val1, val2, val3, val4, val5
}

// Must6 has the same behavior than Must, but callback returns 2 variable2.
func Must6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err error) (T1, T2, T3, T4, T5, T6) {
if err != nil {
panic(err)
}

return val1, val2, val3, val4, val5, val6
}

// Try calls the function and return false in case of error.
func Try(callback func() error) (ok bool) {
ok = true
Expand All @@ -26,7 +93,7 @@ func Try0[T any](callback func()) bool {
})
}

// Try1 is an alias to Try
// Try1 is an alias to Try.
func Try1[T any](callback func() error) bool {
return Try(callback)
}
Expand Down
104 changes: 103 additions & 1 deletion try_test.go → errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,108 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMust(t *testing.T) {
is := assert.New(t)
is.Equal("foo", Must("foo", nil))
is.Panics(func() {
Must("", errors.New("something went wrong"))
})
}

func TestMustX(t *testing.T) {
is := assert.New(t)

{
is.Panics(func() {
Must0(errors.New("something went wrong"))
})
is.NotPanics(func() {
Must0(nil)
})
}

{
val1 := Must1(1, nil)
is.Equal(1, val1)
is.Panics(func() {
Must1(1, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must1(1, nil)
})
}

{
val1, val2 := Must2(1, 2, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Panics(func() {
Must2(1, 2, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must2(1, 2, nil)
})
}

{
val1, val2, val3 := Must3(1, 2, 3, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Panics(func() {
Must3(1, 2, 3, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must3(1, 2, 3, nil)
})
}

{
val1, val2, val3, val4 := Must4(1, 2, 3, 4, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Panics(func() {
Must4(1, 2, 3, 4, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must4(1, 2, 3, 4, nil)
})
}

{
val1, val2, val3, val4, val5 := Must5(1, 2, 3, 4, 5, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)
is.Panics(func() {
Must5(1, 2, 3, 4, 5, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must5(1, 2, 3, 4, 5, nil)
})
}

{
val1, val2, val3, val4, val5, val6 := Must6(1, 2, 3, 4, 5, 6, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)
is.Equal(6, val6)
is.Panics(func() {
Must6(1, 2, 3, 4, 5, 6, errors.New("something went wrong"))
})
is.NotPanics(func() {
Must6(1, 2, 3, 4, 5, 6, nil)
})
}
}

func TestTry(t *testing.T) {
is := assert.New(t)

Expand All @@ -22,7 +124,7 @@ func TestTry(t *testing.T) {
}))
}

func TestTryFunctions(t *testing.T) {
func TestTryX(t *testing.T) {
is := assert.New(t)

is.True(Try2(func() (string, error) {
Expand Down
10 changes: 0 additions & 10 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,3 @@ func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step
}
return result
}

// Must is a helper that wraps a call to a function returning a value and an error
// and panics if the error is non-nil.
func Must[T any](val T, err error) T {
if err != nil {
panic(err)
}

return val
}
9 changes: 0 additions & 9 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lo

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,11 +45,3 @@ func TestRangeClose(t *testing.T) {
is.Equal([]float64{1.0, 3.0}, result5)
is.Equal([]float32{-1.0, -2.0, -3.0}, result6)
}

func TestMust(t *testing.T) {
is := assert.New(t)
is.Equal("foo", Must("foo", nil))
is.Panics(func() {
Must("", errors.New("something went wrong"))
})
}

0 comments on commit 986f172

Please sign in to comment.