helpers

package
v2.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain[T any](fns ...func(T) T) func(T) T

Chain is a reverse Pipe

Chain(m1, m2, m3)(value) => m1(m2(m3(value)))

func ChainWithErr

func ChainWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)

ChainWithErr is a reverse PipeWithErr

ChainWithErr(m1, m2, m3)(value) => m1(m2(m3(value)))

func Default added in v2.12.0

func Default[T comparable](values ...T) T

Default returns the first non-zero value. If all values are zero, return the zero value.

Default("", "foo") // "foo"
Default("bar", "foo") // "bar"
Default("", "", "foo") // "foo"

func DefaultFunc added in v2.15.1

func DefaultFunc[T comparable](callbacks ...func() T) T

func DefaultWithFunc added in v2.12.0

func DefaultWithFunc[T comparable](value T, callbacks ...func() T) T

DefaultWithFunc returns defaultValue if value is zero, otherwise value.

DefaultWithFunc("", func() string { return "foo" }) // "foo"
DefaultWithFunc("bar", func() string { return "foo" }) // "bar"
DefaultWithFunc("", func() string { return "" }, func() string { return "foo" }) // "foo"

func ErrorIf

func ErrorIf(condition bool, format string, a ...any) error

ErrorIf returns an error if the condition is true.

ErrorIf(true, "error") => error
ErrorIf(false, "error") => nil
ErrorIf(true, "error %s", "with value") => error with value

func If added in v2.12.0

func If[T any](condition bool, trueVal T, falseVal T) T

If returns trueVal if condition is true, otherwise falseVal.

If(true, "foo", "bar") // "foo"
If(false, "foo", "bar") // "bar"

Warning: that the trueVal and falseVal in this method will also be executed during runtime, and there may be panics during use. Please be cautious when using it. You can use IfFunc or Optional to avoid this situation.

Example:

var nilVal *foo
If(nilVal != nil, nilVal.Name, "") // panic: runtime error: invalid memory address or nil pointer dereference
If(nilVal != nil, Optional(nilVal).Name, "") // ""

func IfFunc added in v2.12.0

func IfFunc[T any](condition bool, trueFunc func() T, falseFunc func() T) T

IfFunc returns trueFunc() if condition is true, otherwise falseFunc().

IfFunc(true, func() string {
	return "foo"
}, func() string {
	return "bar"
}) // "foo"

func IsEmpty added in v2.12.0

func IsEmpty[T comparable](value T) bool

IsEmpty returns true if the value is zero.

IsEmpty(0) // true
IsEmpty("") // true

func IsType added in v2.12.0

func IsType[T any](value any) bool

IsType returns true if the value is the type.

IsType[int](1) // true
IsType[int]("foo") // false

func IsZero added in v2.12.0

func IsZero[T comparable](value T) bool

IsZero returns true if the value is zero.

IsZero(0) // true
IsZero("") // true
IsZero("foo") // false

func Once added in v2.14.2

func Once(fn func()) func()

Once returns a function that calls the given function only once.

once := Once(func() { fmt.Println("hello") })
once() // prints hello
once() // does nothing

func Optional added in v2.12.0

func Optional[T any](value *T) *T

Optional returns the value if it is not nil, otherwise the zero value.

Optional(&foo{Name: "bar"}) // &foo{Name: "bar"}
Optional[foo](nil) // &foo{}
Optional[int](nil) // *int(0)

func PanicIf

func PanicIf(condition bool, format string, a ...any)

PanicIf panics if the condition is true.

PanicIf(true, "error") => panic("error")
PanicIf(false, "error") => nil
PanicIf(true, "error %s", "with value") => panic("error with value")

func Pipe

func Pipe[T any](fns ...func(T) T) func(T) T

Pipe is a function that takes a value and returns a value

Pipe(m1, m2, m3)(value) => m3(m2(m1(value)))

func PipeWithErr

func PipeWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)

PipeWithErr is a function that takes a value and returns a value and an error

PipeWithErr(m1, m2, m3)(value) => m3(m2(m1(value)))

func Ptr added in v2.12.0

func Ptr[T any](value T) *T

Ptr returns a pointer to the value.

Ptr("foo") // *string("foo")
Ptr(1) // *int(1)

func Repeat added in v2.11.0

func Repeat(fn func() error, times int) error

Repeat runs the given function `times` times or until an error is returned.

Repeat(func() error { fmt.Println("hello"); return nil }, 3) => prints hello 3 times and returns nil
Repeat(func() error { return fmt.Errorf("error") }, 3) => returns error

func Retry

func Retry(fn func() error, attempts int, sleeps ...time.Duration) (err error)

Retry retries the given function until it returns nil or the attempts are exhausted. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep.

Retry(func() error { return nil }, 3) => nil
Retry(func() error { return nil }, 3, time.Second) => nil
Retry(func() error { return fmt.Errorf("error") }, 3) => error

func ReturnIf added in v2.15.1

func ReturnIf[T any](condition bool, callback func() T, defaults ...T) T

ReturnIf returns the result of the callback if the condition is true. Otherwise, return the default value or zero value.

ReturnIf(true, func() string {
	return "foo"
}, "bar") // "foo"
ReturnIf(false, func() string {
	return "foo"
}, "bar") // "bar"

func Scan

func Scan(src any, dest any) error

Scan sets the value of dest to the value of src.

var foo string
Scan("bar", &foo) // foo == "bar"

var bar struct {A string}
Scan(struct{A string}{"foo"}, &bar) // bar == struct{A string}{"foo"}

func Tap added in v2.12.0

func Tap[T any](value T, callbacks ...func(T)) T

Tap calls the given callback with the given value then returns the value.

Tap("foo", func(s string) {
	fmt.Println(s) // "foo" and os.Stdout will print "foo"
}, func(s string) {
	// more callbacks
}...)

func Timeout

func Timeout(fn func() error, timeout time.Duration) error

Timeout runs the given function with a timeout. If the function does not return before the timeout, it returns an error.

Timeout(func() error { return nil }, time.Second) => nil
Timeout(func() error { time.Sleep(2 * time.Second); return nil }, time.Second) => error

func Transform added in v2.12.0

func Transform[T, R any](value T, callback func(T) R) R

Transform calls the given callback with the given value then return the result.

Transform(1, strconv.Itoa) // "1"
Transform("foo", func(s string) *foo {
	return &foo{Name: s}
}) // &foo{Name: "foo"}

func Unless added in v2.12.0

func Unless[T any](condition bool, falseVal T, trueVal T) T

Unless returns falseVal if condition is true, otherwise trueVal.

Unless(true, "foo", "bar") // "bar"
Unless(false, "foo", "bar") // "foo"

func Until

func Until(fn func() bool, sleeps ...time.Duration)

Until retries the given function until it returns true. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep.

Until(func() bool { return true })
Until(func() bool { return true }, time.Second)

func UntilTimeout added in v2.13.0

func UntilTimeout(fn func() bool, timeout time.Duration, sleeps ...time.Duration) error

UntilTimeout retries the given function until it returns true or the timeout is reached. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep. The timeout includes the time to sleep.

UntilTimeout(func() bool { return true }, time.Second)
UntilTimeout(func() bool { return true }, time.Second, time.Millisecond)

func Val added in v2.12.0

func Val[T any](value *T) T

Val returns the value of the pointer. If the pointer is nil, return the zero value.

Val((*string)(nil)) // ""
Val(Ptr("foo")) // "foo"

func When added in v2.12.0

func When(condition bool, callback func())

When calls the given callback with the given value if the condition is true.

func With added in v2.12.0

func With[T any](value T, callbacks ...func(T) T) T

With calls the given callbacks with the given value then return the value.

With("foo", func(s string) string {
	return s + "bar"
}, func(s string) string {
	return s + "baz"
}) // "foobarbaz"

Types

type Proxy added in v2.12.0

type Proxy[T any] struct {
	// contains filtered or unexported fields
}

func NewProxy added in v2.12.0

func NewProxy[T any](value T) *Proxy[T]

func (*Proxy[T]) Tap added in v2.12.0

func (p *Proxy[T]) Tap(callbacks ...func(T)) *Proxy[T]

func (*Proxy[T]) Transform added in v2.12.0

func (p *Proxy[T]) Transform(callback func(T) T) *Proxy[T]

func (*Proxy[T]) Unless added in v2.12.0

func (p *Proxy[T]) Unless(condition bool, callbacks ...func(T) T) *Proxy[T]

func (*Proxy[T]) Value added in v2.12.0

func (p *Proxy[T]) Value() T

func (*Proxy[T]) When added in v2.12.0

func (p *Proxy[T]) When(condition bool, callbacks ...func(T) T) *Proxy[T]

func (*Proxy[T]) With added in v2.12.0

func (p *Proxy[T]) With(callbacks ...func(T) T) *Proxy[T]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL