Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] Update Parser to understand solo conditions #14918

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/ottl/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ import (

// parsedStatement represents a parsed statement. It is the entry point into the statement DSL.
type parsedStatement struct {
TransformationStatement *transformationStatement `parser:"( @@"`
ConditionStatement *conditionStatement `parser:"| @@ )"`
}

// transformationStatement represents a transformation statement.
type transformationStatement struct {
Invocation invocation `parser:"@@"`
WhereClause *booleanExpression `parser:"( 'where' @@ )?"`
}

// conditionStatement represents a condition statement.
type conditionStatement struct {
BooleanExpression *booleanExpression `parser:"@@"`
}

// booleanValue represents something that evaluates to a boolean --
// either an equality or inequality, explicit true or false, or
// a parenthesized subexpression.
Expand Down
84 changes: 63 additions & 21 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,36 @@ type Parser[K any] struct {
}

// Statement holds a top level statement for processing telemetry data.
type Statement[K any] struct {
type Statement[K any] interface {
// Execute is a function that will execute the statement's function if the statement's condition is met.
// Returns true if the function was run, returns false otherwise.
// If the statement contains no condition, the function will run and true will be returned.
// In addition, the functions return value is always returned.
Execute(ctx K) (any, bool)
}

type standardTransformationStatement[K any] struct {
function ExprFunc[K]
condition boolExpressionEvaluator[K]
}

// Execute is a function that will execute the statement's function if the statement's condition is met.
// Returns true if the function was run, returns false otherwise.
// If the statement contains no condition, the function will run and true will be returned.
// In addition, the functions return value is always returned.
func (s *Statement[K]) Execute(ctx K) (any, bool) {
condition := s.condition(ctx)
func (t standardTransformationStatement[K]) Execute(ctx K) (any, bool) {
condition := t.condition(ctx)
var result any
if condition {
result = s.function(ctx)
result = t.function(ctx)
}
return result, condition
}

type standardConditionStatement[K any] struct {
condition boolExpressionEvaluator[K]
}

func (t standardConditionStatement[K]) Execute(ctx K) (any, bool) {
return nil, t.condition(ctx)
}

func NewParser[K any](functions map[string]interface{}, pathParser PathExpressionParser[K], enumParser EnumParser, telemetrySettings component.TelemetrySettings) Parser[K] {
return Parser[K]{
functions: functions,
Expand All @@ -65,28 +77,58 @@ func (p *Parser[K]) ParseStatements(statements []string) ([]*Statement[K], error
errors = multierr.Append(errors, err)
continue
}
function, err := p.newFunctionCall(parsed.Invocation)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
expression, err := p.newBooleanExpressionEvaluator(parsed.WhereClause)
if err != nil {
errors = multierr.Append(errors, err)
continue

switch {
case parsed.TransformationStatement != nil:
tStatement, err := p.parseTransformationStatement(parsed.TransformationStatement)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
parsedStatements = append(parsedStatements, &tStatement)
case parsed.ConditionStatement != nil:
cStatement, err := p.parseConditionStatement(parsed.ConditionStatement)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
parsedStatements = append(parsedStatements, &cStatement)
}
parsedStatements = append(parsedStatements, &Statement[K]{
function: function,
condition: expression,
})
}

if errors != nil {
return nil, errors
}

return parsedStatements, nil
}

func (p *Parser[K]) parseTransformationStatement(parsedStatement *transformationStatement) (Statement[K], error) {
function, err := p.newFunctionCall(parsedStatement.Invocation)
if err != nil {
return nil, err
}
expression, err := p.newBooleanExpressionEvaluator(parsedStatement.WhereClause)
if err != nil {
return nil, err
}

return standardTransformationStatement[K]{
function: function,
condition: expression,
}, nil
}

func (p *Parser[K]) parseConditionStatement(parsedStatement *conditionStatement) (Statement[K], error) {
expression, err := p.newBooleanExpressionEvaluator(parsedStatement.BooleanExpression)
if err != nil {
return nil, err
}
return standardConditionStatement[K]{
condition: expression,
}, nil
}

var parser = newParser()

func parseStatement(raw string) (*parsedStatement, error) {
Copy link
Member

@kovrus kovrus Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably inline this method and in tests it should be ok to use parser.ParseString("", raw)

Expand Down
Loading