-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
miaoyin
committed
Sep 26, 2023
1 parent
bf37ed3
commit c550aba
Showing
3 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package los | ||
|
||
import "github.com/samber/lo" | ||
|
||
func Must[T any](val T, err any, messageArgs ...any) T { | ||
must(err, messageArgs...) | ||
|
||
return val | ||
} | ||
|
||
func Must0(err any, messageArgs ...any) { | ||
must(err, messageArgs...) | ||
} | ||
|
||
func Must1[T any](val T, err any, messageArgs ...any) T { | ||
return Must(val, err, messageArgs...) | ||
} | ||
|
||
func Must2[T1, T2 any](val1 T1, val2 T2, err any, messageArgs ...any) (T1, T2) { | ||
must(err, messageArgs...) | ||
|
||
return val1, val2 | ||
} | ||
|
||
func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...any) (T1, T2, T3) { | ||
must(err, messageArgs...) | ||
|
||
return val1, val2, val3 | ||
} | ||
|
||
func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4) { | ||
must(err, messageArgs...) | ||
|
||
return val1, val2, val3, val4 | ||
} | ||
|
||
func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, | ||
messageArgs ...any, | ||
) (T1, T2, T3, T4, T5) { | ||
must(err, messageArgs...) | ||
|
||
return val1, val2, val3, val4, val5 | ||
} | ||
|
||
func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, | ||
messageArgs ...any, | ||
) (T1, T2, T3, T4, T5, T6) { | ||
must(err, messageArgs...) | ||
|
||
return val1, val2, val3, val4, val5, val6 | ||
} | ||
|
||
func must(err any, messageArgs ...any) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
switch val := err.(type) { | ||
case error: | ||
panic(val) | ||
default: | ||
lo.Must0(err, messageArgs...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package los_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xuender/kit/los" | ||
) | ||
|
||
func TestMust(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must(1, nil) | ||
ass.Panics(func() { | ||
los.Must(1, os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust0(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must0(nil) | ||
los.Must0(true) | ||
ass.Panics(func() { | ||
los.Must0(os.ErrClosed) | ||
}) | ||
ass.Panics(func() { | ||
los.Must0(false) | ||
}) | ||
} | ||
|
||
func TestMust1(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must1(1, nil) | ||
ass.Panics(func() { | ||
los.Must1(1, os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust2(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must2(1, "a", nil) | ||
ass.Panics(func() { | ||
los.Must2(1, "a", os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust3(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must3(1, "a", 1, nil) | ||
ass.Panics(func() { | ||
los.Must3(1, "a", 1, os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust4(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must4(true, 1, "a", 1, nil) | ||
ass.Panics(func() { | ||
los.Must4(true, 1, "a", 1, os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust5(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must5(true, 1, "a", 1, 3.3, nil) | ||
ass.Panics(func() { | ||
los.Must5(true, 1, "a", 1, 4, 5, os.ErrClosed) | ||
}) | ||
} | ||
|
||
func TestMust6(t *testing.T) { | ||
t.Parallel() | ||
|
||
ass := assert.New(t) | ||
|
||
los.Must6('a', true, 1, "a", 1, 3.3, nil) | ||
ass.Panics(func() { | ||
los.Must6('a', true, 1, "a", 1, 4, 5, os.ErrClosed) | ||
}) | ||
} |