helpers

package
v2.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 3 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](value T, defaultValue T) T

Default returns defaultValue if value is zero, otherwise value.

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

func DefaultWithFunc added in v2.12.0

func DefaultWithFunc[T comparable](value T, defaultValue 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"

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 to avoid this situation.

Example:

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

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 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 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) bool

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 }) => true
Until(func() bool { return true }, time.Second) => true

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[T any](value T, condition bool, callbacks ...func(T) T) T

When calls the given callbacks with the given value if condition is true then return the value.

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

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