Documentation
¶
Overview ¶
Package cmp provides generic helpers to compare and match values. It includes functions for equality, ordering and custom comparisons, along with utilities to convert between different comparison function types.
Index ¶
- func Eq[T comparable](a, b T) bool
- func EqFn[T any](a, b T, cmp CompFunc[T]) bool
- func EqFn2[T any](a, b T, eq CondFunc[T]) bool
- func Gt[T core.Ordered](a, b T) bool
- func GtEq[T core.Ordered](a, b T) bool
- func GtEqFn[T any](a, b T, cmp CompFunc[T]) bool
- func GtEqFn2[T any](a, b T, less CondFunc[T]) bool
- func GtFn[T any](a, b T, cmp CompFunc[T]) bool
- func Lt[T core.Ordered](a, b T) bool
- func LtEq[T core.Ordered](a, b T) bool
- func LtEqFn[T any](a, b T, cmp CompFunc[T]) bool
- func LtEqFn2[T any](a, b T, less CondFunc[T]) bool
- func LtFn[T any](a, b T, cmp CompFunc[T]) bool
- func LtFn2[T any](a, b T, less CondFunc[T]) bool
- func M[T any](m Matcher[T]) func(T) bool
- func NotEq[T comparable](a, b T) bool
- func NotEqFn[T any](a, b T, cmp CompFunc[T]) bool
- func NotEqFn2[T any](a, b T, eq CondFunc[T]) bool
- type CompFunc
- type CondFunc
- type MatchFunc
- type Matcher
- func AsMatcher[T any](fn MatchFunc[T]) Matcher[T]
- func Compose[T any, V any](fn func(T) (V, bool), match Matcher[V]) Matcher[T]
- func MatchAll[T any](queries ...Matcher[T]) Matcher[T]
- func MatchAny[T any](queries ...Matcher[T]) Matcher[T]
- func MatchEq[T comparable](v T) Matcher[T]
- func MatchEqFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchEqFn2[T any](v T, eq CondFunc[T]) Matcher[T]
- func MatchGt[T core.Ordered](v T) Matcher[T]
- func MatchGtEq[T core.Ordered](v T) Matcher[T]
- func MatchGtEqFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchGtEqFn2[T any](v T, less CondFunc[T]) Matcher[T]
- func MatchGtFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchLt[T core.Ordered](v T) Matcher[T]
- func MatchLtEq[T core.Ordered](v T) Matcher[T]
- func MatchLtEqFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchLtEqFn2[T any](v T, less CondFunc[T]) Matcher[T]
- func MatchLtFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchLtFn2[T any](v T, less CondFunc[T]) Matcher[T]
- func MatchNotEq[T comparable](v T) Matcher[T]
- func MatchNotEqFn[T any](v T, cmp CompFunc[T]) Matcher[T]
- func MatchNotEqFn2[T any](v T, eq CondFunc[T]) Matcher[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Eq ¶
func Eq[T comparable](a, b T) bool
Eq returns true if a equals b for comparable types. Uses the standard Go equality operator.
func EqFn ¶
EqFn returns true if a equals b using a custom comparison function. Returns true when the comparison function returns zero. Panics if the provided comparison function is nil.
func EqFn2 ¶
EqFn2 returns true if a equals b using a custom equality condition. Returns the direct result of the equality condition function. Panics if the provided equality function is nil.
func Gt ¶
Gt returns true if a is greater than b for ordered types. Uses the standard Go greater-than operator.
func GtEq ¶
GtEq returns true if a is greater than or equal to b for ordered types. Uses the standard Go greater-than-or-equal operator.
func GtEqFn ¶
GtEqFn returns true if a is greater than or equal to b using a comparison. Returns true when comparison result is greater than or equal to zero. Panics if the provided comparison function is nil.
func GtEqFn2 ¶
GtEqFn2 returns true if a is greater than or equal to b using a condition. Returns true when a is not less than b using the provided function. Panics if the provided less-than condition function is nil.
func GtFn ¶
GtFn returns true if a is greater than b using a custom comparison function. Returns true when the comparison function returns a positive value. Panics if the provided comparison function is nil.
func Lt ¶
Lt returns true if a is less than b for ordered types. Uses the standard Go less-than operator.
func LtEq ¶
LtEq returns true if a is less than or equal to b for ordered types. Uses the standard Go less-than-or-equal operator.
func LtEqFn ¶
LtEqFn returns true if a is less than or equal to b using a comparison function. Returns true when comparison result is less than or equal to zero. Panics if the provided comparison function is nil.
func LtEqFn2 ¶
LtEqFn2 returns true if a is less than or equal to b using a condition. Returns true when b is not less than a using the provided function. Panics if the provided less-than condition function is nil.
func LtFn ¶
LtFn returns true if a is less than b using a custom comparison function. Returns true when the comparison function returns a negative value. Panics if the provided comparison function is nil.
func LtFn2 ¶
LtFn2 returns true if a is less than b using a less-than condition. Returns the direct result of the less-than condition function. Panics if the provided less-than condition function is nil.
func M ¶
M converts a Matcher to a simple function that can be used for matching. If the Matcher is nil, it returns a function that always returns true, allowing seamless usage of Matcher types as boolean functions.
func NotEq ¶
func NotEq[T comparable](a, b T) bool
NotEq returns true if a does not equal b for comparable types. Uses the standard Go inequality operator.
Types ¶
type CompFunc ¶
CompFunc is a generic comparison function for type T that returns an integer. The return value follows the standard comparison convention: - Negative value if a < b - Zero if a == b - Positive value if a > b
type CondFunc ¶
CondFunc is a generic condition function for type T that returns a boolean. The return value indicates whether the condition is true or false for the given pair of values.
type MatchFunc ¶
MatchFunc is a function type that implements the Matcher interface, allowing simple functions to be used as matchers.
func (MatchFunc[T]) Match ¶
Match calls the matcher function with the provided value. If the function is nil, it returns true (matches everything). This default behaviour makes nil MatchFunc act as a logical identity element, simplifying composition when conditions are optional.
type Matcher ¶
type Matcher[T any] interface { // And combines this query with others using logical AND. // All conditions must match for the combined query to match. // Nil matchers in the provided list are ignored during matching. And(...Matcher[T]) Matcher[T] // Or combines this query with others using logical OR. // At least one condition must match for the combined query to match. // Nil matchers in the provided list are ignored during matching. Or(...Matcher[T]) Matcher[T] // Not negates this matcher's result. Not() Matcher[T] // Match tests if the given value satisfies this query's conditions. Match(T) bool }
Matcher is a generic interface for filtering and combining predicates of type T. It enables logical AND and OR operations between conditions, and matching against a value.
Matchers are composable, allowing complex matching logic to be built from simpler components. The zero value of any Matcher should either be safe to use or well-documented if it has special behaviour.
func AsMatcher ¶
AsMatcher converts a MatchFunc to a Matcher. Returns nil if the provided function is nil.
func Compose ¶
Compose creates a new Matcher by applying an accessor function to transform input values before matching against an existing matcher. It enables composition of matchers across different types, allowing you to build complex matching logic by combining simple matchers.
The accessor function (fn) extracts or transforms a value of type T into a value of type V and indicates whether the extraction was successful. The returned matcher evaluates to false if the accessor function returns ok=false.
Common uses include: - Extracting and matching on struct fields - Transforming values before matching - Creating conditional matching chains
Example:
type Person struct {
Name string
Age int
}
// Create a matcher that checks if a person is an adult
isAdult := Compose(
func(p Person) (int, bool) { return p.Age, true },
GtEq(18),
)
Panics if the accessor function or the base matcher is nil.
func MatchAll ¶
MatchAll returns a query that matches if all of the provided queries match. If no queries are provided, the result will match everything (return true). Nil queries in the provided list are ignored during matching.
func MatchAny ¶
MatchAny returns a query that matches if any of the provided queries match. If no queries are provided, the result will match nothing (return false). Nil queries in the provided list are ignored during matching.
func MatchEq ¶
func MatchEq[T comparable](v T) Matcher[T]
MatchEq creates a Matcher that checks for equality with the given value.
Returns a function that yields true if the input equals the specified value. Useful for creating filters or validators that need to match exact values.
Example:
isApple := MatchEq("apple")
isApple("apple") // returns true
isApple("banana") // returns false
func MatchEqFn ¶
MatchEqFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input equals the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchEqFn2 ¶
MatchEqFn2 creates a Matcher using a custom equality function. Returns a function that yields true if the input equals the specified value according to the provided equality function. Panics if the equality function is nil.
func MatchGt ¶
MatchGt creates a Matcher that checks if a value is strictly greater than the given value. Returns a function that yields true if the input is greater than the specified value.
func MatchGtEq ¶
MatchGtEq creates a Matcher that checks if a value is greater than or equal to the given value. Returns a function that yields true if the input is greater than or equal to the specified value.
func MatchGtEqFn ¶
MatchGtEqFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input is greater than or equal to the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchGtEqFn2 ¶
MatchGtEqFn2 creates a Matcher using a custom condition function. Returns a function that yields true if the input is greater than or equal to the specified value according to the provided condition function. Panics if the condition function is nil.
func MatchGtFn ¶
MatchGtFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input is greater than the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchLt ¶
MatchLt creates a Matcher that checks if a value is strictly less than the given value. Returns a function that yields true if the input is less than the specified value.
func MatchLtEq ¶
MatchLtEq creates a Matcher that checks if a value is less than or equal to the given value. Returns a function that yields true if the input is less than or equal to the specified value.
func MatchLtEqFn ¶
MatchLtEqFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input is less than or equal to the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchLtEqFn2 ¶
MatchLtEqFn2 creates a Matcher using a custom condition function. Returns a function that yields true if the input is less than or equal to the specified value according to the provided condition function. Panics if the condition function is nil.
func MatchLtFn ¶
MatchLtFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input is less than the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchLtFn2 ¶
MatchLtFn2 creates a Matcher using a custom condition function. Returns a function that yields true if the input is less than the specified value according to the provided condition function. Panics if the condition function is nil.
func MatchNotEq ¶
func MatchNotEq[T comparable](v T) Matcher[T]
MatchNotEq creates a Matcher that checks for inequality with a given value. Returns a function that yields true if the input is not equal to the specified value.
func MatchNotEqFn ¶
MatchNotEqFn creates a Matcher using a custom comparison function. Returns a function that yields true if the input is not equal to the specified value according to the provided comparison function. Panics if the comparison function is nil.
func MatchNotEqFn2 ¶
MatchNotEqFn2 creates a Matcher using a custom equality function. Returns a function that yields true if the input is not equal to the specified value according to the provided equality function. Panics if the equality function is nil.