Standard function names.
const ( FunctionFuzzyAnd = "FUZZY" FunctionAnd = "AND" FunctionOr = "OR" FunctionNot = "NOT" FunctionEquals = "=" FunctionNotEquals = "!=" FunctionLessThan = "<" FunctionLessEquals = "<=" FunctionGreaterEquals = ">=" FunctionGreaterThan = ">" FunctionHas = ":" FunctionDuration = "duration" FunctionTimestamp = "timestamp" )
Has overloads.
const ( FunctionOverloadHasString = FunctionHas + "_string" FunctionOverloadHasMapStringString = FunctionHas + "_map_string_string" FunctionOverloadHasListString = FunctionHas + "_list_string" )
LessThan overloads.
const ( FunctionOverloadLessThanInt = FunctionLessThan + "_int" FunctionOverloadLessThanFloat = FunctionLessThan + "_float" FunctionOverloadLessThanString = FunctionLessThan + "_string" FunctionOverloadLessThanTimestamp = FunctionLessThan + "_timestamp" FunctionOverloadLessThanTimestampString = FunctionLessThan + "_timestamp_string" FunctionOverloadLessThanDuration = FunctionLessThan + "_duration" )
GreaterThan overloads.
const ( FunctionOverloadGreaterThanInt = FunctionGreaterThan + "_int" FunctionOverloadGreaterThanFloat = FunctionGreaterThan + "_float" FunctionOverloadGreaterThanString = FunctionGreaterThan + "_string" FunctionOverloadGreaterThanTimestamp = FunctionGreaterThan + "_timestamp" FunctionOverloadGreaterThanTimestampString = FunctionGreaterThan + "_timestamp_string" FunctionOverloadGreaterThanDuration = FunctionGreaterThan + "_duration" )
LessEquals overloads.
const ( FunctionOverloadLessEqualsInt = FunctionLessEquals + "_int" FunctionOverloadLessEqualsFloat = FunctionLessEquals + "_float" FunctionOverloadLessEqualsString = FunctionLessEquals + "_string" FunctionOverloadLessEqualsTimestamp = FunctionLessEquals + "_timestamp" FunctionOverloadLessEqualsTimestampString = FunctionLessEquals + "_timestamp_string" FunctionOverloadLessEqualsDuration = FunctionLessEquals + "_duration" )
GreaterEquals overloads.
const ( FunctionOverloadGreaterEqualsInt = FunctionGreaterEquals + "_int" FunctionOverloadGreaterEqualsFloat = FunctionGreaterEquals + "_float" FunctionOverloadGreaterEqualsString = FunctionGreaterEquals + "_string" FunctionOverloadGreaterEqualsTimestamp = FunctionGreaterEquals + "_timestamp" FunctionOverloadGreaterEqualsTimestampString = FunctionGreaterEquals + "_timestamp_string" FunctionOverloadGreaterEqualsDuration = FunctionGreaterEquals + "_duration" )
Equals overloads.
const ( FunctionOverloadEqualsBool = FunctionEquals + "_bool" FunctionOverloadEqualsInt = FunctionEquals + "_int" FunctionOverloadEqualsFloat = FunctionEquals + "_float" FunctionOverloadEqualsString = FunctionEquals + "_string" FunctionOverloadEqualsTimestamp = FunctionEquals + "_timestamp" FunctionOverloadEqualsTimestampString = FunctionEquals + "_timestamp_string" FunctionOverloadEqualsDuration = FunctionEquals + "_duration" )
NotEquals overloads.
const ( FunctionOverloadNotEqualsBool = FunctionNotEquals + "_bool" FunctionOverloadNotEqualsInt = FunctionNotEquals + "_int" FunctionOverloadNotEqualsFloat = FunctionNotEquals + "_float" FunctionOverloadNotEqualsString = FunctionNotEquals + "_string" FunctionOverloadNotEqualsTimestamp = FunctionNotEquals + "_timestamp" FunctionOverloadNotEqualsTimestampString = FunctionNotEquals + "_timestamp_string" FunctionOverloadNotEqualsDuration = FunctionNotEquals + "_duration" )
And overloads.
const ( FunctionOverloadAndBool = FunctionAnd + "_bool" )
Duration overloads.
const ( FunctionOverloadDurationString = FunctionDuration + "_string" )
Not overloads.
const ( FunctionOverloadNotBool = FunctionNot + "_bool" )
Or overloads.
const ( FunctionOverloadOrBool = FunctionOr + "_bool" )
Timestamp overloads.
const ( FunctionOverloadTimestampString = FunctionTimestamp + "_string" )
Primitive types.
var ( TypeInt = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_INT64}} TypeFloat = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_DOUBLE}} TypeString = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_STRING}} TypeBool = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_BOOL}} )
Well-known types.
var ( TypeDuration = &expr.Type{TypeKind: &expr.Type_WellKnown{WellKnown: expr.Type_DURATION}} TypeTimestamp = &expr.Type{TypeKind: &expr.Type_WellKnown{WellKnown: expr.Type_TIMESTAMP}} )
func And(args ...*expr.Expr) *expr.Expr
func Duration(value time.Duration) *expr.Expr
func Equals(lhs, rhs *expr.Expr) *expr.Expr
func Expression(sequences ...*expr.Expr) *expr.Expr
func Factor(terms ...*expr.Expr) *expr.Expr
func Float(value float64) *expr.Expr
func Function(name string, args ...*expr.Expr) *expr.Expr
func GreaterEquals(lhs, rhs *expr.Expr) *expr.Expr
func GreaterThan(lhs, rhs *expr.Expr) *expr.Expr
func Has(lhs, rhs *expr.Expr) *expr.Expr
func Int(value int64) *expr.Expr
func LessEquals(lhs, rhs *expr.Expr) *expr.Expr
func LessThan(lhs, rhs *expr.Expr) *expr.Expr
func Member(operand *expr.Expr, field string) *expr.Expr
func NewConstantDeclaration(name string, constantType *expr.Type, constantValue *expr.Constant) *expr.Decl
NewConstantDeclaration creates a new constant ident declaration.
func NewFunctionDeclaration(name string, overloads ...*expr.Decl_FunctionDecl_Overload) *expr.Decl
NewFunctionDeclaration creates a new function declaration.
func NewFunctionOverload(id string, result *expr.Type, params ...*expr.Type) *expr.Decl_FunctionDecl_Overload
NewFunctionOverload creates a new function overload.
func NewIdentDeclaration(name string, identType *expr.Type) *expr.Decl
NewIdentDeclaration creates a new ident declaration.
func NewStringConstant(value string) *expr.Constant
NewStringConstant creates a new string constant.
func Not(arg *expr.Expr) *expr.Expr
func NotEquals(lhs, rhs *expr.Expr) *expr.Expr
func Or(args ...*expr.Expr) *expr.Expr
func Sequence(factors ...*expr.Expr) *expr.Expr
func StandardFunctionAnd() *expr.Decl
StandardFunctionAnd returns a declaration for the standard `AND` function and all its standard overloads.
func StandardFunctionDeclarations() []*expr.Decl
StandardFunctionDeclarations returns declarations for all standard functions and their standard overloads.
func StandardFunctionDuration() *expr.Decl
StandardFunctionDuration returns a declaration for the standard `duration` function and all its standard overloads.
func StandardFunctionEquals() *expr.Decl
StandardFunctionEquals returns a declaration for the standard '=' function and all its standard overloads.
func StandardFunctionGreaterEquals() *expr.Decl
StandardFunctionGreaterEquals returns a declaration for the standard '>=' function and all its standard overloads.
func StandardFunctionGreaterThan() *expr.Decl
StandardFunctionGreaterThan returns a declaration for the standard '>' function and all its standard overloads.
func StandardFunctionHas() *expr.Decl
StandardFunctionHas returns a declaration for the standard `:` function and all its standard overloads.
func StandardFunctionLessEquals() *expr.Decl
StandardFunctionLessEquals returns a declaration for the standard '<=' function and all its standard overloads.
func StandardFunctionLessThan() *expr.Decl
StandardFunctionLessThan returns a declaration for the standard '<' function and all its standard overloads.
func StandardFunctionNot() *expr.Decl
StandardFunctionNot returns a declaration for the standard `NOT` function and all its standard overloads.
func StandardFunctionNotEquals() *expr.Decl
StandardFunctionNotEquals returns a declaration for the standard '!=' function and all its standard overloads.
func StandardFunctionOr() *expr.Decl
StandardFunctionOr returns a declaration for the standard `OR` function and all its standard overloads.
func StandardFunctionTimestamp() *expr.Decl
StandardFunctionTimestamp returns a declaration for the standard `timestamp` function and all its standard overloads.
func String(s string) *expr.Expr
func Text(text string) *expr.Expr
func Timestamp(value time.Time) *expr.Expr
func TypeEnum(enumType protoreflect.EnumType) *expr.Type
TypeEnum returns the type of a protobuf enum.
func TypeList(elementType *expr.Type) *expr.Type
TypeList returns the type for a list with the provided element type.
func TypeMap(keyType, valueType *expr.Type) *expr.Type
TypeMap returns the type for a map with the provided key and value types.
func Walk(fn WalkFunc, currExpr *expr.Expr)
Walk an expression in depth-first order.
type Checker struct {
// contains filtered or unexported fields
}
func (c *Checker) Check() (*expr.CheckedExpr, error)
func (c *Checker) Init(exp *expr.Expr, sourceInfo *expr.SourceInfo, declarations *Declarations)
A Cursor describes an expression encountered while applying a Macro.
The method Replace can be used to rewrite the filter.
type Cursor struct {
// contains filtered or unexported fields
}
func (c *Cursor) Expr() *expr.Expr
Expr returns the current expression.
func (c *Cursor) Parent() (*expr.Expr, bool)
Parent returns the parent of the current expression.
func (c *Cursor) Replace(newExpr *expr.Expr)
Replace the current expression with a new expression.
DeclarationOption configures Declarations.
type DeclarationOption func(*Declarations) error
func DeclareEnumIdent(name string, enumType protoreflect.EnumType) DeclarationOption
func DeclareFunction(name string, overloads ...*expr.Decl_FunctionDecl_Overload) DeclarationOption
DeclareFunction is a DeclarationOption that declares a single function and its overloads.
func DeclareIdent(name string, t *expr.Type) DeclarationOption
DeclareIdent is a DeclarationOption that declares a single ident.
func DeclareStandardFunctions() DeclarationOption
DeclareStandardFunction is a DeclarationOption that declares all standard functions and their overloads.
Declarations contain declarations for type-checking filter expressions.
type Declarations struct {
// contains filtered or unexported fields
}
func NewDeclarations(opts ...DeclarationOption) (*Declarations, error)
NewDeclarations creates a new set of Declarations for filter expression type-checking.
func (d *Declarations) LookupEnumIdent(name string) (protoreflect.EnumType, bool)
func (d *Declarations) LookupFunction(name string) (*expr.Decl, bool)
func (d *Declarations) LookupIdent(name string) (*expr.Decl, bool)
Filter represents a parsed and type-checked filter.
type Filter struct { CheckedExpr *expr.CheckedExpr }
func ApplyMacros(filter Filter, declarations *Declarations, macros ...Macro) (Filter, error)
ApplyMacros applies the provided macros to the filter and type-checks the result against the provided declarations.
func ParseFilter(request Request, declarations *Declarations) (Filter, error)
ParseFilter parses and type-checks the filter in the provided Request.
Lexer is a filter expression lexer.
type Lexer struct {
// contains filtered or unexported fields
}
func (l *Lexer) Init(filter string)
Init initializes the lexer with the provided filter.
func (l *Lexer) Lex() (Token, error)
Lex returns the next token in the filter expression, or io.EOF when there are no more tokens to lex.
func (l *Lexer) LineOffsets() []int32
LineOffsets returns a monotonically increasing list of character offsets where newlines appear.
func (l *Lexer) Position() Position
Position returns the current position of the lexer.
Macro represents a function that can perform macro replacements on a filter expression.
type Macro func(*Cursor)
Parser for filter expressions.
type Parser struct {
// contains filtered or unexported fields
}
func (p *Parser) Init(filter string)
Init (re-)initializes the parser to parse the provided filter.
func (p *Parser) Parse() (*expr.ParsedExpr, error)
Parse the filter.
func (p *Parser) ParseArg() (_ *expr.Expr, err error)
ParseArg parses an Arg.
EBNF
arg : comparable | composite ;
func (p *Parser) ParseComparable() (_ *expr.Expr, err error)
ParseComparable parses a Comparable.
EBNF
comparable : member | function | number (custom) ;
func (p *Parser) ParseComposite() (_ *expr.Expr, err error)
ParseComposite parses a Composite.
EBNF
composite : LPAREN expression RPAREN ;
func (p *Parser) ParseExpression() (_ *expr.Expr, err error)
ParseExpression parses an Expression.
EBNF
expression : sequence {WS AND WS sequence} ;
func (p *Parser) ParseFactor() (_ *expr.Expr, err error)
ParseFactor parses a Factor.
EBNF
factor : term {WS OR WS term} ;
func (p *Parser) ParseFloat() (_ *expr.Expr, err error)
ParseFloat parses a float.
EBNF
float : MINUS? (NUMBER DOT NUMBER* | DOT NUMBER) EXP? ;
func (p *Parser) ParseFunction() (_ *expr.Expr, err error)
ParseFunction parses a Function.
EBNF
function : name {DOT name} LPAREN [argList] RPAREN ; name : TEXT | keyword ;
func (p *Parser) ParseInt() (_ *expr.Expr, err error)
ParseInt parses an int.
EBNF
int : MINUS? NUMBER | MINUS? HEX ;
func (p *Parser) ParseMember() (_ *expr.Expr, err error)
ParseMember parses a Member.
EBNF
member : value {DOT field} ; value : TEXT | STRING ; field : value | keyword | number ;
func (p *Parser) ParseNumber() (_ *expr.Expr, err error)
ParseNumber parses a number.
EBNF
number : float | int ; float : MINUS? (NUMBER DOT NUMBER* | DOT NUMBER) EXP? ; int : MINUS? NUMBER | MINUS? HEX ;
func (p *Parser) ParseRestriction() (_ *expr.Expr, err error)
ParseRestriction parses a Restriction.
EBNF
restriction : comparable [comparator arg] ;
func (p *Parser) ParseSequence() (_ *expr.Expr, err error)
ParseSequence parses a Sequence.
EBNF
sequence : factor {WS factor} ;
func (p *Parser) ParseSimple() (_ *expr.Expr, err error)
ParseSimple parses a Simple.
EBNF
simple : restriction | composite ;
func (p *Parser) ParseTerm() (_ *expr.Expr, err error)
ParseTerm parses a Term.
EBNF
term : [(NOT WS | MINUS)] simple ;
func (p *Parser) SourceInfo() *expr.SourceInfo
func (p *Parser) TryParseFloat() (*expr.Expr, bool)
func (p *Parser) TryParseFunction() (*expr.Expr, bool)
func (p *Parser) TryParseNumber() (*expr.Expr, bool)
Position represents a position in a filter expression.
type Position struct { // Offset is the byte offset, starting at 0. Offset int32 // Line is the line number, starting at 1. Line int32 // Column is the column number, starting at 1 (character count per line). Column int32 }
func (p Position) String() string
String returns a string representation of the position on the format <line>:<column>.
Request is an interface for gRPC requests that contain a standard AIP filter.
type Request interface { GetFilter() string }
Token represents a token in a filter expression.
type Token struct { // Position of the token. Position Position // Type of the token. Type TokenType // Value of the token, if the token is a text or a string. Value string }
func (t Token) Unquote() string
TokenType represents the type of a filter expression token.
See: https://google.aip.dev/assets/misc/ebnf-filtering.txt
type TokenType string
Value token types.
const ( TokenTypeWhitespace TokenType = "WS" TokenTypeText TokenType = "TEXT" TokenTypeString TokenType = "STRING" )
Keyword token types.
const ( TokenTypeNot TokenType = "NOT" TokenTypeAnd TokenType = "AND" TokenTypeOr TokenType = "OR" )
Numeric token types.
const ( TokenTypeNumber TokenType = "NUM" TokenTypeHexNumber TokenType = "HEX" )
Operator token types.
const ( TokenTypeLeftParen TokenType = "(" TokenTypeRightParen TokenType = ")" TokenTypeMinus TokenType = "-" TokenTypeDot TokenType = "." TokenTypeEquals TokenType = "=" TokenTypeHas TokenType = ":" TokenTypeLessThan TokenType = "<" TokenTypeGreaterThan TokenType = ">" TokenTypeExclaim TokenType = "!" TokenTypeComma TokenType = "," TokenTypeLessEquals TokenType = "<=" TokenTypeGreaterEquals TokenType = ">=" TokenTypeNotEquals TokenType = "!=" )
func (t TokenType) Function() string
func (t TokenType) IsComparator() bool
IsComparator returns true if the token is a valid comparator.
func (t TokenType) IsField() bool
IsField returns true if the token is a valid field.
func (t TokenType) IsKeyword() bool
IsKeyword returns true if the token is a valid keyword.
func (t TokenType) IsName() bool
IsName returns true if the token is a valid name.
func (t TokenType) IsValue() bool
IsField returns true if the token is a valid value.
func (t TokenType) Test(other TokenType) bool
WalkFunc is called for every expression while calling Walk. Return false to stop Walk.
type WalkFunc func(currExpr, parentExpr *expr.Expr) bool