mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-12 01:55:57 -04:00
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.1.8 to 2.2.2. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.1.8...v2.2.2) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
// 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()
|
|
}
|