Documentation
¶
Overview ¶
Package tools provides various helpers for writing declarative option setters.
Index ¶
- func And(preds ...bool) func() bool
- func AndFn(preds ...PredicateClosure) func() bool
- func Each[T any, V any, O ~func(V) error](items []T, f func(i int, t T) O) O
- func Group[T any, O ~func(T) error](fns ...O) O
- func OnlyIf[T any, O ~func(T) error](check CheckClosure, f O) O
- func Or(preds ...bool) func() bool
- func OrFn(preds ...PredicateClosure) func() bool
- func WhenTrue[T any, O ~func(T) error](check bool, fns ...O) O
- func WhenTrueElse[T any, O ~func(T) error](check bool, fns O, elseFn O) O
- func WhenTrueElseFn[T any, O ~func(T) error](predicate PredicateClosure, fns O, elseFn O) O
- func WhenTrueFn[T any, O ~func(T) error](predicate PredicateClosure, fns ...O) O
- type CheckClosure
- type PredicateClosure
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶
And is a function that takes a variadic number of booleans and returns true if all the booleans are true.
func AndFn ¶
func AndFn(preds ...PredicateClosure) func() bool
AndFn is a function that takes a variadic number of predicates and returns a single predicate. It returns true if all the predicates are true.
func Each ¶ added in v0.1.4
Each is a function that takes a slice of items and a function that returns a setter. It applies the setter function to the input for each item in the slice.
note: if the setter function is nil, it will be skipped and not added to errors
func OnlyIf ¶
func OnlyIf[T any, O ~func(T) error](check CheckClosure, f O) O
OnlyIf is a function that takes a check closure (which is a function that returns a boolean and an error) and a setter function. It applies the setter function to the input if the check closure returns true AND the error is nil.
note: if the check closure is nil, an error will be returned
note: If your intent is "apply the setter only if the condition is met, otherwise silently skip without error", then use tools.WhenTrue
func Or ¶
Or is a function that takes a variadic number of booleans and returns true if any of the booleans are true.
func OrFn ¶
func OrFn(preds ...PredicateClosure) func() bool
OrFn is a function that takes a variadic number of predicates and returns a single predicate.
It returns true if any of the predicates are true.
func WhenTrue ¶
WhenTrue is a function that takes a boolean and a variadic number of setters and returns a single setter. Only if the boolean is true, the setters will be called.
note: if any of the setters are nil, they will be skipped and not added to errors
func WhenTrueElse ¶
WhenTrueElse is a function that takes a boolean, a setter to call if the boolean is true, and a setter to call if the boolean is false. It returns a single setter. Only if it passes the provided predicate closure, the setters will be called.
note: if any of the setters are nil, they will be skipped and not added to errors
func WhenTrueElseFn ¶
func WhenTrueElseFn[T any, O ~func(T) error](predicate PredicateClosure, fns O, elseFn O) O
WhenTrueElseFn is a function that takes a predicate closure, a setter to call if the predicate is true, and a setter to call if the predicate is false. It returns a single setter. Only if it passes the provided predicate closure, the setters will be called.
note: if any of the setters are nil, they will be skipped and not added to errors
func WhenTrueFn ¶
func WhenTrueFn[T any, O ~func(T) error](predicate PredicateClosure, fns ...O) O
WhenTrueFn is a function that takes a variadic number of setters and returns a single setter. Only if it passes the provided predicate closures, the setters will be called.
note: if any of the setters are nil, they will be skipped and not added to errors
Types ¶
type CheckClosure ¶
type PredicateClosure ¶
type PredicateClosure func() bool
PredicateClosure is a condition based on external or ambient context, not the internal config. This allows behavior to be toggled based on CLI flags, environment variables, testing hooks, etc.