filter

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(expr Expr, post *models.Post, ctx *EvalContext) (bool, error)

Evaluate evaluates an expression against a post

func MatchPost

func MatchPost(expression string, post *models.Post) (bool, error)

MatchPost is a convenience function that parses an expression and matches a single post

func Posts

func Posts(expression string, posts []*models.Post) ([]*models.Post, error)

Posts is a convenience function that parses an expression and filters posts

Types

type BinaryExpr

type BinaryExpr struct {
	Left  Expr
	Op    string
	Right Expr
}

BinaryExpr represents a binary expression (left op right)

func (*BinaryExpr) String

func (e *BinaryExpr) String() string

type CallExpr

type CallExpr struct {
	Object Expr
	Method string
	Args   []Expr
}

CallExpr represents a method call (object.method(args...))

func (*CallExpr) String

func (e *CallExpr) String() string

type EvalContext

type EvalContext struct {
	Today time.Time
	Now   time.Time
}

EvalContext contains the context for evaluating filter expressions

func NewEvalContext

func NewEvalContext() *EvalContext

NewEvalContext creates a new evaluation context with current time values

type Expr

type Expr interface {
	String() string
	// contains filtered or unexported methods
}

Expr is the interface for all AST nodes

func ParseExpression

func ParseExpression(input string) (Expr, error)

ParseExpression parses a filter expression string into an AST

type FieldAccess

type FieldAccess struct {
	Object Expr
	Field  string
}

FieldAccess represents field access (object.field)

func (*FieldAccess) String

func (e *FieldAccess) String() string

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter represents a compiled filter expression

func Always

func Always() *Filter

Always returns a filter that always matches

func And

func And(filters ...*Filter) *Filter

And combines multiple filters with AND logic

func MustParse

func MustParse(expression string) *Filter

MustParse parses a filter expression and panics on error

func Never

func Never() *Filter

Never returns a filter that never matches

func Not

func Not(f *Filter) *Filter

Not creates a negated filter

func Or

func Or(filters ...*Filter) *Filter

Or combines multiple filters with OR logic

func Parse

func Parse(expression string) (*Filter, error)

Parse parses a filter expression and returns a Filter. Parsed ASTs are cached to avoid re-parsing identical expressions.

func (*Filter) Expression

func (f *Filter) Expression() string

Expression returns the original filter expression string

func (*Filter) Match

func (f *Filter) Match(post *models.Post) (bool, error)

Match evaluates the filter against a single post

func (*Filter) MatchAll

func (f *Filter) MatchAll(posts []*models.Post) []*models.Post

MatchAll filters a slice of posts and returns only those that match

func (*Filter) MatchAllWithErrors

func (f *Filter) MatchAllWithErrors(posts []*models.Post) ([]*models.Post, []error)

MatchAllWithErrors filters a slice of posts and returns matches along with any errors

func (*Filter) MustMatch

func (f *Filter) MustMatch(post *models.Post) bool

MustMatch evaluates the filter against a single post and panics on error

func (*Filter) RefreshContext

func (f *Filter) RefreshContext()

RefreshContext refreshes the context with current time values

func (*Filter) SetContext

func (f *Filter) SetContext(ctx *EvalContext)

SetContext sets a custom evaluation context

type Identifier

type Identifier struct {
	Name string
}

Identifier represents an identifier

func (*Identifier) String

func (e *Identifier) String() string

type InExpr

type InExpr struct {
	Value      Expr
	Collection Expr
}

InExpr represents an 'in' expression (value in collection)

func (*InExpr) String

func (e *InExpr) String() string

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes a filter expression

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the given input

func (*Lexer) NextToken

func (l *Lexer) NextToken() (Token, error)

NextToken returns the next token from the input

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() ([]Token, error)

Tokenize returns all tokens from the input

type Literal

type Literal struct {
	Value interface{}
}

Literal represents a literal value (string, number, bool, nil)

func (*Literal) String

func (e *Literal) String() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses filter expressions into an AST

func NewParser

func NewParser(input string) (*Parser, error)

NewParser creates a new parser for the given input

func (*Parser) Parse

func (p *Parser) Parse() (Expr, error)

Parse parses the input and returns the AST

type SpecialValue

type SpecialValue struct {
	Name string
}

SpecialValue represents special values like 'today' and 'now'

func (*SpecialValue) String

func (e *SpecialValue) String() string

type Token

type Token struct {
	Type    TokenType
	Value   string
	Literal interface{} // For STRING, NUMBER, BOOL: the actual value
	Pos     int         // Position in the input string
}

Token represents a lexical token

func (Token) String

func (t Token) String() string

String returns a string representation of the token

type TokenType

type TokenType int

TokenType represents the type of a token

const (
	TokenEOF TokenType = iota
	TokenIdentifier
	TokenString
	TokenNumber
	TokenBool
	TokenNone
	TokenCompareOp // ==, !=, <, >, <=, >=
	TokenLogicOp   // and, or
	TokenIn
	TokenContains // contains (legacy syntax support)
	TokenNot
	TokenLParen
	TokenRParen
	TokenDot
	TokenComma
	TokenToday
	TokenNow
)

type UnaryExpr

type UnaryExpr struct {
	Op   string
	Expr Expr
}

UnaryExpr represents a unary expression (op expr)

func (*UnaryExpr) String

func (e *UnaryExpr) String() string

Jump to

Keyboard shortcuts

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