tools

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package tools provides various helpers for writing declarative option setters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And(preds ...bool) func() bool

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

func Each[T any, V any, O ~func(V) error](items []T, f func(i int, t T) O) O

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 Group

func Group[T any, O ~func(T) error](fns ...O) O

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

func Or(preds ...bool) func() bool

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

func WhenTrue[T any, O ~func(T) error](check bool, fns ...O) O

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

func WhenTrueElse[T any, O ~func(T) error](check bool, fns O, elseFn O) O

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 CheckClosure func() (bool, error)

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.

Jump to

Keyboard shortcuts

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