Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(base): add new pkg basefn for provide some base func
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Package basefn provide some no-dependents util functions | ||
package basefn | ||
|
||
import "fmt" | ||
|
||
// Panicf format panic message use fmt.Sprintf | ||
func Panicf(format string, v ...any) { | ||
panic(fmt.Sprintf(format, v...)) | ||
} | ||
|
||
// MustOK if error is not empty, will panic | ||
func MustOK(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Must if error is not empty, will panic | ||
func Must[T any](v T, err error) T { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} | ||
|
||
// ErrOnFail return input error on cond is false, otherwise return nil | ||
func ErrOnFail(cond bool, err error) error { | ||
return OrError(cond, err) | ||
} | ||
|
||
// OrError return input error on cond is false, otherwise return nil | ||
func OrError(cond bool, err error) error { | ||
if !cond { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// FirstOr get first elem or elseVal | ||
func FirstOr[T any](sl []T, elseVal T) T { | ||
if len(sl) > 0 { | ||
return sl[0] | ||
} | ||
return elseVal | ||
} | ||
|
||
// OrValue get | ||
func OrValue[T any](cond bool, okVal, elVal T) T { | ||
if cond { | ||
return okVal | ||
} | ||
return elVal | ||
} | ||
|
||
// OrReturn call okFunc() on condition is true, else call elseFn() | ||
func OrReturn[T any](cond bool, okFn, elseFn func() T) T { | ||
if cond { | ||
return okFn() | ||
} | ||
return elseFn() | ||
} | ||
|
||
// ErrFunc type | ||
type ErrFunc func() error | ||
|
||
// CallOn call func on condition is true | ||
func CallOn(cond bool, fn ErrFunc) error { | ||
if cond { | ||
return fn() | ||
} | ||
return nil | ||
} | ||
|
||
// CallOrElse call okFunc() on condition is true, else call elseFn() | ||
func CallOrElse(cond bool, okFn, elseFn ErrFunc) error { | ||
if cond { | ||
return okFn() | ||
} | ||
return elseFn() | ||
} |
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,65 @@ | ||
package basefn_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/basefn" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestPanicf(t *testing.T) { | ||
basefn.MustOK(nil) | ||
|
||
assert.Eq(t, "hi", basefn.Must("hi", nil)) | ||
assert.Panics(t, func() { | ||
basefn.Must("hi", errors.New("a error")) | ||
}) | ||
assert.Panics(t, func() { | ||
basefn.Panicf("hi %s", "inhere") | ||
}) | ||
} | ||
|
||
func TestErrOnFail(t *testing.T) { | ||
err := errors.New("a error") | ||
assert.Err(t, basefn.ErrOnFail(false, err)) | ||
assert.NoErr(t, basefn.ErrOnFail(true, err)) | ||
} | ||
|
||
func TestOrValue(t *testing.T) { | ||
assert.Eq(t, "ab", basefn.OrValue(true, "ab", "dc")) | ||
assert.Eq(t, "dc", basefn.OrValue(false, "ab", "dc")) | ||
} | ||
|
||
func TestOrReturn(t *testing.T) { | ||
assert.Eq(t, "ab", basefn.OrReturn(true, func() string { | ||
return "ab" | ||
}, func() string { | ||
return "dc" | ||
})) | ||
assert.Eq(t, "dc", basefn.OrReturn(false, func() string { | ||
return "ab" | ||
}, func() string { | ||
return "dc" | ||
})) | ||
} | ||
|
||
func TestCallOn(t *testing.T) { | ||
assert.NoErr(t, basefn.CallOn(false, func() error { | ||
return errors.New("a error") | ||
})) | ||
assert.Err(t, basefn.CallOn(true, func() error { | ||
return errors.New("a error") | ||
})) | ||
|
||
assert.ErrMsg(t, basefn.CallOrElse(true, func() error { | ||
return errors.New("a error 001") | ||
}, func() error { | ||
return errors.New("a error 002") | ||
}), "a error 001") | ||
assert.ErrMsg(t, basefn.CallOrElse(false, func() error { | ||
return errors.New("a error 001") | ||
}, func() error { | ||
return errors.New("a error 002") | ||
}), "a error 002") | ||
} |