Documentation ¶
Overview ¶
Package filtering provides primitives for implementing AIP filtering.
See: https://google.aip.dev/160 (Filtering)
Index ¶
- Constants
- Variables
- 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
- func NewFunctionDeclaration(name string, overloads ...*expr.Decl_FunctionDecl_Overload) *expr.Decl
- func NewFunctionOverload(id string, result *expr.Type, params ...*expr.Type) *expr.Decl_FunctionDecl_Overload
- func NewIdentDeclaration(name string, identType *expr.Type) *expr.Decl
- func NewStringConstant(value string) *expr.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
- func StandardFunctionDeclarations() []*expr.Decl
- func StandardFunctionDuration() *expr.Decl
- func StandardFunctionEquals() *expr.Decl
- func StandardFunctionGreaterEquals() *expr.Decl
- func StandardFunctionGreaterThan() *expr.Decl
- func StandardFunctionHas() *expr.Decl
- func StandardFunctionLessEquals() *expr.Decl
- func StandardFunctionLessThan() *expr.Decl
- func StandardFunctionNot() *expr.Decl
- func StandardFunctionNotEquals() *expr.Decl
- func StandardFunctionOr() *expr.Decl
- func StandardFunctionTimestamp() *expr.Decl
- 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
- func TypeList(elementType *expr.Type) *expr.Type
- func TypeMap(keyType, valueType *expr.Type) *expr.Type
- func Walk(fn WalkFunc, currExpr *expr.Expr)
- type Checker
- type Cursor
- type DeclarationOption
- type Declarations
- type Filter
- type Lexer
- type Macro
- type Parser
- func (p *Parser) Init(filter string)
- func (p *Parser) Parse() (*expr.ParsedExpr, error)
- func (p *Parser) ParseArg() (_ *expr.Expr, err error)
- func (p *Parser) ParseComparable() (_ *expr.Expr, err error)
- func (p *Parser) ParseComposite() (_ *expr.Expr, err error)
- func (p *Parser) ParseExpression() (_ *expr.Expr, err error)
- func (p *Parser) ParseFactor() (_ *expr.Expr, err error)
- func (p *Parser) ParseFloat() (_ *expr.Expr, err error)
- func (p *Parser) ParseFunction() (_ *expr.Expr, err error)
- func (p *Parser) ParseInt() (_ *expr.Expr, err error)
- func (p *Parser) ParseMember() (_ *expr.Expr, err error)
- func (p *Parser) ParseNumber() (_ *expr.Expr, err error)
- func (p *Parser) ParseRestriction() (_ *expr.Expr, err error)
- func (p *Parser) ParseSequence() (_ *expr.Expr, err error)
- func (p *Parser) ParseSimple() (_ *expr.Expr, err error)
- func (p *Parser) ParseTerm() (_ *expr.Expr, err error)
- 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)
- type Position
- type Request
- type Token
- type TokenType
- type WalkFunc
Constants ¶
const ( FunctionFuzzyAnd = "FUZZY" FunctionAnd = "AND" FunctionOr = "OR" FunctionNot = "NOT" FunctionEquals = "=" FunctionNotEquals = "!=" FunctionLessThan = "<" FunctionLessEquals = "<=" FunctionGreaterEquals = ">=" FunctionGreaterThan = ">" FunctionHas = ":" FunctionDuration = "duration" FunctionTimestamp = "timestamp" )
Standard function names.
const ( FunctionOverloadHasString = FunctionHas + "_string" FunctionOverloadHasMapStringString = FunctionHas + "_map_string_string" FunctionOverloadHasListString = FunctionHas + "_list_string" )
Has overloads.
const ( FunctionOverloadLessThanInt = FunctionLessThan + "_int" FunctionOverloadLessThanFloat = FunctionLessThan + "_float" FunctionOverloadLessThanString = FunctionLessThan + "_string" FunctionOverloadLessThanTimestamp = FunctionLessThan + "_timestamp" FunctionOverloadLessThanDuration = FunctionLessThan + "_duration" )
LessThan overloads.
const ( FunctionOverloadGreaterThanInt = FunctionGreaterThan + "_int" FunctionOverloadGreaterThanFloat = FunctionGreaterThan + "_float" FunctionOverloadGreaterThanString = FunctionGreaterThan + "_string" FunctionOverloadGreaterThanTimestamp = FunctionGreaterThan + "_timestamp" FunctionOverloadGreaterThanDuration = FunctionGreaterThan + "_duration" )
GreaterThan overloads.
const ( FunctionOverloadLessEqualsInt = FunctionLessEquals + "_int" FunctionOverloadLessEqualsFloat = FunctionLessEquals + "_float" FunctionOverloadLessEqualsString = FunctionLessEquals + "_string" FunctionOverloadLessEqualsTimestamp = FunctionLessEquals + "_timestamp" FunctionOverloadLessEqualsDuration = FunctionLessEquals + "_duration" )
LessEquals overloads.
const ( FunctionOverloadGreaterEqualsInt = FunctionGreaterEquals + "_int" FunctionOverloadGreaterEqualsFloat = FunctionGreaterEquals + "_float" FunctionOverloadGreaterEqualsString = FunctionGreaterEquals + "_string" FunctionOverloadGreaterEqualsTimestamp = FunctionGreaterEquals + "_timestamp" FunctionOverloadGreaterEqualsDuration = FunctionGreaterEquals + "_duration" )
GreaterEquals overloads.
const ( FunctionOverloadEqualsBool = FunctionEquals + "_bool" FunctionOverloadEqualsInt = FunctionEquals + "_int" FunctionOverloadEqualsFloat = FunctionEquals + "_float" FunctionOverloadEqualsString = FunctionEquals + "_string" FunctionOverloadEqualsTimestamp = FunctionEquals + "_timestamp" FunctionOverloadEqualsDuration = FunctionEquals + "_duration" )
Equals overloads.
const ( FunctionOverloadNotEqualsBool = FunctionNotEquals + "_bool" FunctionOverloadNotEqualsInt = FunctionNotEquals + "_int" FunctionOverloadNotEqualsFloat = FunctionNotEquals + "_float" FunctionOverloadNotEqualsString = FunctionNotEquals + "_string" FunctionOverloadNotEqualsTimestamp = FunctionNotEquals + "_timestamp" FunctionOverloadNotEqualsDuration = FunctionNotEquals + "_duration" )
NotEquals overloads.
const (
FunctionOverloadAndBool = FunctionAnd + "_bool"
)
And overloads.
const (
FunctionOverloadDurationString = FunctionDuration + "_string"
)
Duration overloads.
const (
FunctionOverloadNotBool = FunctionNot + "_bool"
)
Not overloads.
const (
FunctionOverloadOrBool = FunctionOr + "_bool"
)
Or overloads.
const (
FunctionOverloadTimestampString = FunctionTimestamp + "_string"
)
Timestamp overloads.
Variables ¶
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}} )
Primitive types. nolint: gochecknoglobals
var ( TypeDuration = &expr.Type{TypeKind: &expr.Type_WellKnown{WellKnown: expr.Type_DURATION}} TypeTimestamp = &expr.Type{TypeKind: &expr.Type_WellKnown{WellKnown: expr.Type_TIMESTAMP}} )
Well-known types. nolint: gochecknoglobals
Functions ¶
func NewConstantDeclaration ¶
func NewConstantDeclaration(name string, constantType *expr.Type, constantValue *expr.Constant) *expr.Decl
NewConstantDeclaration creates a new constant ident declaration.
func NewFunctionDeclaration ¶
func NewFunctionDeclaration(name string, overloads ...*expr.Decl_FunctionDecl_Overload) *expr.Decl
NewFunctionDeclaration creates a new function declaration.
func NewFunctionOverload ¶
func NewFunctionOverload(id string, result *expr.Type, params ...*expr.Type) *expr.Decl_FunctionDecl_Overload
NewFunctionOverload creates a new function overload.
func NewIdentDeclaration ¶
NewIdentDeclaration creates a new ident declaration.
func NewStringConstant ¶
NewStringConstant creates a new string constant.
func StandardFunctionAnd ¶
StandardFunctionAnd returns a declaration for the standard `AND` function and all its standard overloads.
func StandardFunctionDeclarations ¶
StandardFunctionDeclarations returns declarations for all standard functions and their standard overloads.
func StandardFunctionDuration ¶
StandardFunctionDuration returns a declaration for the standard `duration` function and all its standard overloads.
func StandardFunctionEquals ¶
StandardFunctionEquals returns a declaration for the standard '=' function and all its standard overloads.
func StandardFunctionGreaterEquals ¶
StandardFunctionGreaterEquals returns a declaration for the standard '>=' function and all its standard overloads.
func StandardFunctionGreaterThan ¶
StandardFunctionGreaterThan returns a declaration for the standard '>' function and all its standard overloads.
func StandardFunctionHas ¶
StandardFunctionHas returns a declaration for the standard `:` function and all its standard overloads.
func StandardFunctionLessEquals ¶
StandardFunctionLessEquals returns a declaration for the standard '<=' function and all its standard overloads.
func StandardFunctionLessThan ¶
StandardFunctionLessThan returns a declaration for the standard '<' function and all its standard overloads.
func StandardFunctionNot ¶
StandardFunctionNot returns a declaration for the standard `NOT` function and all its standard overloads.
func StandardFunctionNotEquals ¶
StandardFunctionNotEquals returns a declaration for the standard '!=' function and all its standard overloads.
func StandardFunctionOr ¶
StandardFunctionOr returns a declaration for the standard `OR` function and all its standard overloads.
func StandardFunctionTimestamp ¶
StandardFunctionTimestamp returns a declaration for the standard `timestamp` function and all its standard overloads.
func TypeEnum ¶
func TypeEnum(enumType protoreflect.EnumType) *expr.Type
TypeEnum returns the type of a protobuf enum.
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
func (*Checker) Init ¶
func (c *Checker) Init(exp *expr.Expr, sourceInfo *expr.SourceInfo, declarations *Declarations)
type Cursor ¶ added in v0.26.0
type Cursor struct {
// contains filtered or unexported fields
}
A Cursor describes an expression encountered while applying a Macro.
The method Replace can be used to rewrite the filter.
type DeclarationOption ¶
type DeclarationOption func(*Declarations) error
DeclarationOption configures Declarations.
func DeclareEnumIdent ¶
func DeclareEnumIdent(name string, enumType protoreflect.EnumType) DeclarationOption
func DeclareFunction ¶
func DeclareFunction(name string, overloads ...*expr.Decl_FunctionDecl_Overload) DeclarationOption
DeclareFunction is a DeclarationOption that declares a single function and its overloads.
func DeclareIdent ¶
func DeclareIdent(name string, t *expr.Type) DeclarationOption
DeclareIdent is a DeclarationOption that declares a single ident.
func DeclareStandardFunctions ¶
func DeclareStandardFunctions() DeclarationOption
DeclareStandardFunction is a DeclarationOption that declares all standard functions and their overloads.
type Declarations ¶
type Declarations struct {
// contains filtered or unexported fields
}
Declarations contain declarations for type-checking filter expressions.
func NewDeclarations ¶
func NewDeclarations(opts ...DeclarationOption) (*Declarations, error)
NewDeclarations creates a new set of Declarations for filter expression type-checking.
func (*Declarations) LookupEnumIdent ¶
func (d *Declarations) LookupEnumIdent(name string) (protoreflect.EnumType, bool)
func (*Declarations) LookupFunction ¶
func (d *Declarations) LookupFunction(name string) (*expr.Decl, bool)
func (*Declarations) LookupIdent ¶
func (d *Declarations) LookupIdent(name string) (*expr.Decl, bool)
type Filter ¶
type Filter struct {
CheckedExpr *expr.CheckedExpr
}
Filter represents a parsed and type-checked filter.
func ApplyMacros ¶ added in v0.26.0
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 ¶
func ParseFilter(request Request, declarations *Declarations) (Filter, error)
ParseFilter parses and type-checks the filter in the provided Request.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is a filter expression lexer.
func (*Lexer) Lex ¶
Lex returns the next token in the filter expression, or io.EOF when there are no more tokens to lex.
func (*Lexer) LineOffsets ¶
LineOffsets returns a monotonically increasing list of character offsets where newlines appear.
type Macro ¶ added in v0.26.0
type Macro func(*Cursor)
Macro represents a function that can perform macro replacements on a filter expression.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser for filter expressions.
func (*Parser) ParseComparable ¶
ParseComparable parses a Comparable.
EBNF
comparable : member | function | number (custom) ;
func (*Parser) ParseComposite ¶
ParseComposite parses a Composite.
EBNF
composite : LPAREN expression RPAREN ;
func (*Parser) ParseExpression ¶
ParseExpression parses an Expression.
EBNF
expression : sequence {WS AND WS sequence} ;
func (*Parser) ParseFloat ¶
ParseFloat parses a float.
EBNF
float : MINUS? (NUMBER DOT NUMBER* | DOT NUMBER) EXP? ;
func (*Parser) ParseFunction ¶
ParseFunction parses a Function.
EBNF
function : name {DOT name} LPAREN [argList] RPAREN ; name : TEXT | keyword ;
func (*Parser) ParseMember ¶
ParseMember parses a Member.
EBNF
member : value {DOT field} ; value : TEXT | STRING ; field : value | keyword | number ;
func (*Parser) ParseNumber ¶
ParseNumber parses a number.
EBNF
number : float | int ; float : MINUS? (NUMBER DOT NUMBER* | DOT NUMBER) EXP? ; int : MINUS? NUMBER | MINUS? HEX ;
func (*Parser) ParseRestriction ¶
ParseRestriction parses a Restriction.
EBNF
restriction : comparable [comparator arg] ;
func (*Parser) ParseSequence ¶
ParseSequence parses a Sequence.
EBNF
sequence : factor {WS factor} ;
func (*Parser) SourceInfo ¶
func (p *Parser) SourceInfo() *expr.SourceInfo
type Position ¶
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 }
Position represents a position in a filter expression.
type Request ¶
type Request interface {
GetFilter() string
}
Request is an interface for gRPC requests that contain a standard AIP filter.
type Token ¶
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 }
Token represents a token in a filter expression.
type TokenType ¶
type TokenType string
TokenType represents the type of a filter expression token.
See: https://google.aip.dev/assets/misc/ebnf-filtering.txt
const ( TokenTypeWhitespace TokenType = "WS" TokenTypeText TokenType = "TEXT" TokenTypeString TokenType = "STRING" )
Value token types.
const ( TokenTypeNot TokenType = "NOT" TokenTypeAnd TokenType = "AND" TokenTypeOr TokenType = "OR" )
Keyword token types.
Numeric 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 = "!=" )
Operator token types.
func (TokenType) IsComparator ¶
IsComparator returns true if the token is a valid comparator.