filtering

package
v0.67.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 11 Imported by: 49

Documentation

Overview

Package filtering provides primitives for implementing AIP filtering.

See: https://google.aip.dev/160 (Filtering)

Index

Constants

View Source
const (
	FunctionFuzzyAnd      = "FUZZY"
	FunctionAnd           = "AND"
	FunctionOr            = "OR"
	FunctionNot           = "NOT"
	FunctionEquals        = "="
	FunctionNotEquals     = "!="
	FunctionLessThan      = "<"
	FunctionLessEquals    = "<="
	FunctionGreaterEquals = ">="
	FunctionGreaterThan   = ">"
	FunctionHas           = ":"
	FunctionDuration      = "duration"
	FunctionTimestamp     = "timestamp"
)

Standard function names.

View Source
const (
	FunctionOverloadHasString          = FunctionHas + "_string"
	FunctionOverloadHasMapStringString = FunctionHas + "_map_string_string"
	FunctionOverloadHasListString      = FunctionHas + "_list_string"
)

Has overloads.

View Source
const (
	FunctionOverloadLessThanInt             = FunctionLessThan + "_int"
	FunctionOverloadLessThanFloat           = FunctionLessThan + "_float"
	FunctionOverloadLessThanString          = FunctionLessThan + "_string"
	FunctionOverloadLessThanTimestamp       = FunctionLessThan + "_timestamp"
	FunctionOverloadLessThanTimestampString = FunctionLessThan + "_timestamp_string"
	FunctionOverloadLessThanDuration        = FunctionLessThan + "_duration"
)

LessThan overloads.

View Source
const (
	FunctionOverloadGreaterThanInt             = FunctionGreaterThan + "_int"
	FunctionOverloadGreaterThanFloat           = FunctionGreaterThan + "_float"
	FunctionOverloadGreaterThanString          = FunctionGreaterThan + "_string"
	FunctionOverloadGreaterThanTimestamp       = FunctionGreaterThan + "_timestamp"
	FunctionOverloadGreaterThanTimestampString = FunctionGreaterThan + "_timestamp_string"
	FunctionOverloadGreaterThanDuration        = FunctionGreaterThan + "_duration"
)

GreaterThan overloads.

View Source
const (
	FunctionOverloadLessEqualsInt             = FunctionLessEquals + "_int"
	FunctionOverloadLessEqualsFloat           = FunctionLessEquals + "_float"
	FunctionOverloadLessEqualsString          = FunctionLessEquals + "_string"
	FunctionOverloadLessEqualsTimestamp       = FunctionLessEquals + "_timestamp"
	FunctionOverloadLessEqualsTimestampString = FunctionLessEquals + "_timestamp_string"
	FunctionOverloadLessEqualsDuration        = FunctionLessEquals + "_duration"
)

LessEquals overloads.

View Source
const (
	FunctionOverloadGreaterEqualsInt             = FunctionGreaterEquals + "_int"
	FunctionOverloadGreaterEqualsFloat           = FunctionGreaterEquals + "_float"
	FunctionOverloadGreaterEqualsString          = FunctionGreaterEquals + "_string"
	FunctionOverloadGreaterEqualsTimestamp       = FunctionGreaterEquals + "_timestamp"
	FunctionOverloadGreaterEqualsTimestampString = FunctionGreaterEquals + "_timestamp_string"
	FunctionOverloadGreaterEqualsDuration        = FunctionGreaterEquals + "_duration"
)

GreaterEquals overloads.

View Source
const (
	FunctionOverloadEqualsBool            = FunctionEquals + "_bool"
	FunctionOverloadEqualsInt             = FunctionEquals + "_int"
	FunctionOverloadEqualsFloat           = FunctionEquals + "_float"
	FunctionOverloadEqualsString          = FunctionEquals + "_string"
	FunctionOverloadEqualsTimestamp       = FunctionEquals + "_timestamp"
	FunctionOverloadEqualsTimestampString = FunctionEquals + "_timestamp_string"
	FunctionOverloadEqualsDuration        = FunctionEquals + "_duration"
)

Equals overloads.

View Source
const (
	FunctionOverloadNotEqualsBool            = FunctionNotEquals + "_bool"
	FunctionOverloadNotEqualsInt             = FunctionNotEquals + "_int"
	FunctionOverloadNotEqualsFloat           = FunctionNotEquals + "_float"
	FunctionOverloadNotEqualsString          = FunctionNotEquals + "_string"
	FunctionOverloadNotEqualsTimestamp       = FunctionNotEquals + "_timestamp"
	FunctionOverloadNotEqualsTimestampString = FunctionNotEquals + "_timestamp_string"
	FunctionOverloadNotEqualsDuration        = FunctionNotEquals + "_duration"
)

NotEquals overloads.

View Source
const (
	FunctionOverloadAndBool = FunctionAnd + "_bool"
)

And overloads.

View Source
const (
	FunctionOverloadDurationString = FunctionDuration + "_string"
)

Duration overloads.

View Source
const (
	FunctionOverloadNotBool = FunctionNot + "_bool"
)

Not overloads.

View Source
const (
	FunctionOverloadOrBool = FunctionOr + "_bool"
)

Or overloads.

View Source
const (
	FunctionOverloadTimestampString = FunctionTimestamp + "_string"
)

Timestamp overloads.

Variables

View Source
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.

View Source
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.

Functions

func And

func And(args ...*expr.Expr) *expr.Expr

func Duration

func Duration(value time.Duration) *expr.Expr

func Equals

func Equals(lhs, rhs *expr.Expr) *expr.Expr

func Expression

func Expression(sequences ...*expr.Expr) *expr.Expr

func Factor

func Factor(terms ...*expr.Expr) *expr.Expr

func Float

func Float(value float64) *expr.Expr

func Function

func Function(name string, args ...*expr.Expr) *expr.Expr

func GreaterEquals

func GreaterEquals(lhs, rhs *expr.Expr) *expr.Expr

func GreaterThan

func GreaterThan(lhs, rhs *expr.Expr) *expr.Expr

func Has

func Has(lhs, rhs *expr.Expr) *expr.Expr

func Int

func Int(value int64) *expr.Expr

func LessEquals

func LessEquals(lhs, rhs *expr.Expr) *expr.Expr

func LessThan

func LessThan(lhs, rhs *expr.Expr) *expr.Expr

func Member

func Member(operand *expr.Expr, field string) *expr.Expr

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

func NewIdentDeclaration(name string, identType *expr.Type) *expr.Decl

NewIdentDeclaration creates a new ident declaration.

func NewStringConstant

func NewStringConstant(value string) *expr.Constant

NewStringConstant creates a new string constant.

func Not

func Not(arg *expr.Expr) *expr.Expr

func NotEquals

func NotEquals(lhs, rhs *expr.Expr) *expr.Expr

func Or

func Or(args ...*expr.Expr) *expr.Expr

func Sequence

func Sequence(factors ...*expr.Expr) *expr.Expr

func StandardFunctionAnd

func StandardFunctionAnd() *expr.Decl

StandardFunctionAnd returns a declaration for the standard `AND` function and all its standard overloads.

func StandardFunctionDeclarations

func StandardFunctionDeclarations() []*expr.Decl

StandardFunctionDeclarations returns declarations for all standard functions and their standard overloads.

func StandardFunctionDuration

func StandardFunctionDuration() *expr.Decl

StandardFunctionDuration returns a declaration for the standard `duration` function and all its standard overloads.

func StandardFunctionEquals

func StandardFunctionEquals() *expr.Decl

StandardFunctionEquals returns a declaration for the standard '=' function and all its standard overloads.

func StandardFunctionGreaterEquals

func StandardFunctionGreaterEquals() *expr.Decl

StandardFunctionGreaterEquals returns a declaration for the standard '>=' function and all its standard overloads.

func StandardFunctionGreaterThan

func StandardFunctionGreaterThan() *expr.Decl

StandardFunctionGreaterThan returns a declaration for the standard '>' function and all its standard overloads.

func StandardFunctionHas

func StandardFunctionHas() *expr.Decl

StandardFunctionHas returns a declaration for the standard `:` function and all its standard overloads.

func StandardFunctionLessEquals

func StandardFunctionLessEquals() *expr.Decl

StandardFunctionLessEquals returns a declaration for the standard '<=' function and all its standard overloads.

func StandardFunctionLessThan

func StandardFunctionLessThan() *expr.Decl

StandardFunctionLessThan returns a declaration for the standard '<' function and all its standard overloads.

func StandardFunctionNot

func StandardFunctionNot() *expr.Decl

StandardFunctionNot returns a declaration for the standard `NOT` function and all its standard overloads.

func StandardFunctionNotEquals

func StandardFunctionNotEquals() *expr.Decl

StandardFunctionNotEquals returns a declaration for the standard '!=' function and all its standard overloads.

func StandardFunctionOr

func StandardFunctionOr() *expr.Decl

StandardFunctionOr returns a declaration for the standard `OR` function and all its standard overloads.

func StandardFunctionTimestamp

func StandardFunctionTimestamp() *expr.Decl

StandardFunctionTimestamp returns a declaration for the standard `timestamp` function and all its standard overloads.

func String

func String(s string) *expr.Expr

func Text

func Text(text string) *expr.Expr

func Timestamp

func Timestamp(value time.Time) *expr.Expr

func TypeEnum

func TypeEnum(enumType protoreflect.EnumType) *expr.Type

TypeEnum returns the type of a protobuf enum.

func TypeList

func TypeList(elementType *expr.Type) *expr.Type

TypeList returns the type for a list with the provided element type.

func TypeMap

func TypeMap(keyType, valueType *expr.Type) *expr.Type

TypeMap returns the type for a map with the provided key and value types.

func Walk added in v0.26.0

func Walk(fn WalkFunc, currExpr *expr.Expr)

Walk an expression in depth-first order.

Types

type Checker

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

func (*Checker) Check

func (c *Checker) Check() (*expr.CheckedExpr, error)

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.

func (*Cursor) Expr added in v0.26.0

func (c *Cursor) Expr() *expr.Expr

Expr returns the current expression.

func (*Cursor) Parent added in v0.26.0

func (c *Cursor) Parent() (*expr.Expr, bool)

Parent returns the parent of the current expression.

func (*Cursor) Replace added in v0.26.0

func (c *Cursor) Replace(newExpr *expr.Expr)

Replace the current expression with a new expression.

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) Init

func (l *Lexer) Init(filter string)

Init initializes the lexer with the provided filter.

func (*Lexer) Lex

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 (*Lexer) LineOffsets

func (l *Lexer) LineOffsets() []int32

LineOffsets returns a monotonically increasing list of character offsets where newlines appear.

func (*Lexer) Position

func (l *Lexer) Position() Position

Position returns the current position of the lexer.

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) Init

func (p *Parser) Init(filter string)

Init (re-)initializes the parser to parse the provided filter.

func (*Parser) Parse

func (p *Parser) Parse() (*expr.ParsedExpr, error)

Parse the filter.

func (*Parser) ParseArg

func (p *Parser) ParseArg() (_ *expr.Expr, err error)

ParseArg parses an Arg.

EBNF

arg
  : comparable
  | composite
  ;

func (*Parser) ParseComparable

func (p *Parser) ParseComparable() (_ *expr.Expr, err error)

ParseComparable parses a Comparable.

EBNF

comparable
  : member
  | function
  | number (custom)
  ;

func (*Parser) ParseComposite

func (p *Parser) ParseComposite() (_ *expr.Expr, err error)

ParseComposite parses a Composite.

EBNF

composite
  : LPAREN expression RPAREN
  ;

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() (_ *expr.Expr, err error)

ParseExpression parses an Expression.

EBNF

expression
  : sequence {WS AND WS sequence}
  ;

func (*Parser) ParseFactor

func (p *Parser) ParseFactor() (_ *expr.Expr, err error)

ParseFactor parses a Factor.

EBNF

factor
  : term {WS OR WS term}
  ;

func (*Parser) ParseFloat

func (p *Parser) ParseFloat() (_ *expr.Expr, err error)

ParseFloat parses a float.

EBNF

float
  : MINUS? (NUMBER DOT NUMBER* | DOT NUMBER) EXP?
  ;

func (*Parser) ParseFunction

func (p *Parser) ParseFunction() (_ *expr.Expr, err error)

ParseFunction parses a Function.

EBNF

function
  : name {DOT name} LPAREN [argList] RPAREN
  ;

name
  : TEXT
  | keyword
  ;

func (*Parser) ParseInt

func (p *Parser) ParseInt() (_ *expr.Expr, err error)

ParseInt parses an int.

EBNF

int
  : MINUS? NUMBER
  | MINUS? HEX
  ;

func (*Parser) ParseMember

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 (*Parser) ParseNumber

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 (*Parser) ParseRestriction

func (p *Parser) ParseRestriction() (_ *expr.Expr, err error)

ParseRestriction parses a Restriction.

EBNF

restriction
  : comparable [comparator arg]
  ;

func (*Parser) ParseSequence

func (p *Parser) ParseSequence() (_ *expr.Expr, err error)

ParseSequence parses a Sequence.

EBNF

sequence
  : factor {WS factor}
  ;

func (*Parser) ParseSimple

func (p *Parser) ParseSimple() (_ *expr.Expr, err error)

ParseSimple parses a Simple.

EBNF

simple
  : restriction
  | composite
  ;

func (*Parser) ParseTerm

func (p *Parser) ParseTerm() (_ *expr.Expr, err error)

ParseTerm parses a Term.

EBNF

term
  : [(NOT WS | MINUS)] simple
  ;

func (*Parser) SourceInfo

func (p *Parser) SourceInfo() *expr.SourceInfo

func (*Parser) TryParseFloat

func (p *Parser) TryParseFloat() (*expr.Expr, bool)

func (*Parser) TryParseFunction

func (p *Parser) TryParseFunction() (*expr.Expr, bool)

func (*Parser) TryParseNumber

func (p *Parser) TryParseNumber() (*expr.Expr, bool)

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.

func (Position) String

func (p Position) String() string

String returns a string representation of the position on the format <line>:<column>.

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.

func (Token) Unquote

func (t Token) Unquote() string

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.

const (
	TokenTypeNumber    TokenType = "NUM"
	TokenTypeHexNumber TokenType = "HEX"
)

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) Function

func (t TokenType) Function() string

func (TokenType) IsComparator

func (t TokenType) IsComparator() bool

IsComparator returns true if the token is a valid comparator.

func (TokenType) IsField

func (t TokenType) IsField() bool

IsField returns true if the token is a valid field.

func (TokenType) IsKeyword

func (t TokenType) IsKeyword() bool

IsKeyword returns true if the token is a valid keyword.

func (TokenType) IsName

func (t TokenType) IsName() bool

IsName returns true if the token is a valid name.

func (TokenType) IsValue

func (t TokenType) IsValue() bool

IsField returns true if the token is a valid value.

func (TokenType) Test

func (t TokenType) Test(other TokenType) bool

type WalkFunc added in v0.26.0

type WalkFunc func(currExpr, parentExpr *expr.Expr) bool

WalkFunc is called for every expression while calling Walk. Return false to stop Walk.

Directories

Path Synopsis
Package exprs provides primitives for working with v1alpha1/expr values.
Package exprs provides primitives for working with v1alpha1/expr values.

Jump to

Keyboard shortcuts

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