Documentation
¶
Index ¶
- Variables
- func Match(v any, exp Expr) (bool, error)
- type Expr
- func AND(exprs ...Expr) Expr
- func CONTAINS(f string, v any) Expr
- func EQ(f string, v any) Expr
- func GT(f string, v any) Expr
- func GTE(f string, v any) Expr
- func HAS(f string, v any) Expr
- func HASANY(f string, v any) Expr
- func HASNONE(f string, v any) Expr
- func HASNOT(f string, v any) Expr
- func IN(f string, v any) Expr
- func LT(f string, v any) Expr
- func LTE(f string, v any) Expr
- func NE(f string, v any) Expr
- func NOT(exp Expr) Expr
- func NOTEQ(f string, v any) Expr
- func NOTIN(f string, v any) Expr
- func OR(exprs ...Expr) Expr
- func PREFIX(f string, v any) Expr
- func SUFFIX(f string, v any) Expr
- type MatchFunc
- type Matcher
- type Op
- type Order
- type OrderDirection
- type OrderType
- type QX
- func (qx *QX) AND(exprs ...Expr) *QX
- func (qx *QX) By(field string, dir OrderDirection) *QX
- func (qx *QX) ByArrayCount(field string, dir OrderDirection) *QX
- func (qx *QX) ByArrayPos(field string, slice any, dir OrderDirection) *QX
- func (qx *QX) CacheKey(key string) *QX
- func (qx *QX) Max(limit int) *QX
- func (qx *QX) OR(exprs ...Expr) *QX
- func (qx *QX) Page(pageNum, perPage int) *QX
- func (qx *QX) Skip(offset int) *QX
- func (qx *QX) UsedFields() []string
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Match ¶
Match evaluates the expression exp against v in one shot. v may be provided either as a struct value (T) or as a pointer to a struct (*T); interfaces wrapping T or *T are also supported. Nil values never match and return (false, nil). For repeated evaluations over the same type, prefer creating a Matcher via MatcherFor and reusing it.
Types ¶
type Expr ¶
Expr represents a single filter expression or a logical group of expressions. Depending on Op, an Expr may represent:
- a scalar comparison (EQ, GT, LT, etc.),
- a slice operation (IN, HAS, HASANY),
- or a logical operation (AND, OR) combining sub-expressions.
For logical operations, Operands contains the nested expressions. For non-logical operations, Field and Value describe the comparison. If Not is set, the result of the expression is logically negated.
func CONTAINS ¶
CONTAINS builds an expression that matches when the string field contains the provided substring.
func HAS ¶
HAS builds a slice containment expression for slice fields: the field slice must contain all elements from the provided value slice.
func HASANY ¶
HASANY builds a slice intersection expression for slice fields: the field slice must share at least one element with the provided value slice.
func HASNONE ¶
HASNONE builds an expression that matches when the slice field contains none of the provided values. It evaluates to true only if the intersection between the field slice and the provided values is empty. HASNONE is the logical equivalent of NOT(HASANY(...)).
func HASNOT ¶
HASNOT builds a negated slice containment expression for slice fields. It matches when the field slice does not contain all elements from the provided value slice (i.e., at least one of the provided values is missing). HASNOT is the logical equivalent of NOT(HAS(...)).
func IN ¶
IN builds a set-membership expression: field IN valueSlice. The provided value must be a slice; the field is compared against each element of the slice.
func NOTIN ¶
NOTIN builds a negated set-membership expression: field NOT IN valueSlice. The provided value must be a slice; the field is compared against each element of the slice.
func PREFIX ¶
PREFIX builds an expression that matches when the string field starts with the provided prefix.
func SUFFIX ¶
SUFFIX builds an expression that matches when the string field ends with the provided suffix.
func (Expr) UsedFields ¶ added in v0.1.1
UsedFields returns a de-duplicated list of field names referenced by the expression. The returned slice includes fields from nested expressions.
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
func MatcherFor ¶
MatcherFor creates a Matcher for the struct type T. T may be a struct or a pointer to a struct (any pointer depth is allowed). Reflection data for the type is computed once and cached for reuse. The matcher supports addressing fields by Go name and by selected struct tag aliases.
func NewMatcher ¶
NewMatcher creates a matcher for the type of the provided value. Pointer types allow faster matching.
func (*Matcher) Compile ¶
Compile compiles expr into an efficient predicate function. The returned function may be called with either a struct value (T) or a pointer to a struct (*T); interfaces wrapping T or *T are also supported. Passing nil to the predicate returns (false, nil). The compiled predicate can be reused safely for repeated evaluations.
func (*Matcher) DiffFields ¶
DiffFields returns the names of exported fields whose values differ between the provided values. Each value may be provided either as T or *T; interfaces wrapping T or *T are also supported. At least two values must be provided; if fewer are provided, it returns nil, nil. Values must be of the matcher's source type or convertible to it.
func (*Matcher) DiffFieldsTag ¶
DiffFieldsTag is like DiffFields but returns field names using the provided struct tag. Each value may be provided either as T or *T; interfaces wrapping T or *T are also supported. For each differing field, the first component of the tag (before a comma) is used; if the tag is missing or "-", it falls back to the Go field name. If tag is empty, DiffFieldsTag behaves the same as DiffFields.
func (*Matcher) Match ¶
Match evaluates the expression expr against v using this Matcher. v may be provided either as a struct value (T) or as a pointer to a struct (*T); interfaces wrapping T or *T are also supported. This method is intended for occasional, one-off evaluations. For repeated matching with the same expression, use Compile to obtain a reusable predicate.
type Order ¶
Order describes a single ordering rule applied to query results. The meaning of Data depends on the ordering Type. If Desc is true, the ordering is descending; otherwise ascending.
type OrderDirection ¶
type OrderDirection byte
const ( ASC OrderDirection = 0 DESC OrderDirection = 1 )
type QX ¶
type QX struct {
Key string // optional cache key
Expr Expr
Order []Order
Offset uint64
Limit uint64
}
QX represents a compiled query description. It combines a filter expression, optional ordering rules, offset and limit.
func Query ¶
Query creates a new QX and sets the provided expressions as part of the root AND group. With zero expressions it returns an empty query. With one expression it becomes the root.
func (*QX) By ¶
func (qx *QX) By(field string, dir OrderDirection) *QX
By adds a basic sort order by field; dir may be ASC or DESC.
func (*QX) ByArrayCount ¶
func (qx *QX) ByArrayCount(field string, dir OrderDirection) *QX
ByArrayCount adds an ordering rule by the number of items in a slice field; dir may be ASC or DESC.
func (*QX) ByArrayPos ¶
func (qx *QX) ByArrayPos(field string, slice any, dir OrderDirection) *QX
ByArrayPos adds an ordering rule by the position of field's value within the provided slice; dir may be ASC or DESC.
func (*QX) Max ¶
Max sets the query limit (maximum number of items to return). It panics if limit is negative.
func (*QX) Page ¶
Page sets the offset and limit based on 1-based pageNum and perPage. It panics if pageNum or perPage are not positive.
func (*QX) Skip ¶
Skip sets the query offset (number of items to skip). It panics if offset is negative.
func (*QX) UsedFields ¶
UsedFields returns a de-duplicated list of field names referenced by the query expression and sort orders. The returned slice includes fields from nested expressions.