Skip to content

Commit

Permalink
patched criteria evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed May 1, 2024
1 parent 1a401a3 commit 7f85749
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
13 changes: 12 additions & 1 deletion model/criteria/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func Test_EvaluateCriteria(t *testing.T) {
HasError bool
}{

{
Description: "selector $key1:1",
Expression: "$checkAngular.Output:!/16.2.1/", //
DefaultResult: true,
Expected: true,
State: map[string]interface{}{
"checkAngular.": map[string]interface{}{
"Output": "3",
},
},
},
{
Description: "eq $key1:1",
Expression: "$key1:1", //
Expand Down Expand Up @@ -161,7 +172,7 @@ func Test_EvaluateCriteria(t *testing.T) {
},
}

for i, useCase := range useCases[len(useCases)-1:] {
for i, useCase := range useCases[:1] {
context := manager.NewContext(toolbox.NewContext())
state := context.State()
if len(useCase.State) > 0 {
Expand Down
30 changes: 17 additions & 13 deletions model/criteria/parser/criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func parseCriteria(cursor *parsly.Cursor, qualify *ast.Qualify) error {
binary := &ast.Binary{}
err := parseQualify(cursor, binary, false)
if binary.Op == "" && binary.Y == nil && binary.X != nil {
if unary, ok := binary.X.(*ast.Unary);ok {
if unary, ok := binary.X.(*ast.Unary); ok {
qualify.X = unary
} else {
qualify.X = &ast.Unary{X: binary.X, Op: ""}
Expand Down Expand Up @@ -77,20 +77,20 @@ func normalizeBinary(binary *ast.Binary) {
case "&&", "||":
default:
switch yOp := binary.Y.(type) {
case *ast.Binary:
swap := &ast.Binary{X: binary.X, Op: binary.Op, Y: yOp.X}
binary.Op = yOp.Op
binary.X = swap
binary.Y = yOp.Y
if binExpr, ok := binary.Y.(*ast.Binary); ok {
normalizeBinary(binExpr)
}
case *ast.Binary:
swap := &ast.Binary{X: binary.X, Op: binary.Op, Y: yOp.X}
binary.Op = yOp.Op
binary.X = swap
binary.Y = yOp.Y
if binExpr, ok := binary.Y.(*ast.Binary); ok {
normalizeBinary(binExpr)
}

}
}
}

var operands = []*parsly.Token{
var defaultsOperands = []*parsly.Token{
boolLiteralMatcher,
doubleQuotedStringLiteralMatcher,
singleQuotedStringLiteralMatcher,
Expand All @@ -101,13 +101,17 @@ var operands = []*parsly.Token{
selectorMatcher,
parenthesesMatcher,
stringLiteralMatcher,
terminatorMatcher,
}

func expectOperand(cursor *parsly.Cursor) (ast.Node, error) {

var err error
pos := cursor.Pos
match := cursor.MatchAfterOptional(whitespaceMatcher, operands...)
match := cursor.MatchAfterOptional(whitespaceMatcher, defaultsOperands...)
switch match.Code {
case terminatorCode:
return &ast.Literal{Value: match.Text(cursor), Type: "string"}, nil
case boolLiteral:
return &ast.Literal{Value: match.Text(cursor), Type: "bool"}, nil
case stringLiteral:
Expand Down Expand Up @@ -138,11 +142,11 @@ func expectOperand(cursor *parsly.Cursor) (ast.Node, error) {
if err != nil || qualify == nil {
return nil, err
}
return &ast.Group{X:qualify.X}, nil
return &ast.Group{X: qualify.X}, nil
case parsly.EOF:
return nil, nil
case parsly.Invalid:
return nil, cursor.NewError(operands...)
return nil, cursor.NewError(defaultsOperands...)
default:
cursor.Pos = pos
return nil, nil
Expand Down
3 changes: 1 addition & 2 deletions model/criteria/parser/declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func parseDeclare(input string, err error, cursor *parsly.Cursor, when string, expr string, elseExpr string) (string, string, string, error) {
binary := ast.Binary{}
err = parseQualify(cursor, &binary, true)
err = parseQualify(cursor, &binary, true)
if err != nil {
err = nil
return "", "", input, nil
Expand All @@ -33,4 +33,3 @@ func parseDeclare(input string, err error, cursor *parsly.Cursor, when string, e
}
return when, expr, elseExpr, nil
}

3 changes: 3 additions & 0 deletions model/criteria/parser/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (

questionMark

terminatorCode

colon
)

Expand All @@ -41,6 +43,7 @@ var doubleQuotedStringLiteralMatcher = parsly.NewToken(doubleQuotedStringLiteral
var numericLiteralMatcher = parsly.NewToken(numericLiteral, `NUMERIC`, matcher.NewNumber())
var stringLiteralMatcher = parsly.NewToken(stringLiteral, `STRING`, smatcher.NewFragment())
var selectorMatcher = parsly.NewToken(selectorCode, "SELECTOR", smatcher.NewSelector())
var terminatorMatcher = parsly.NewToken(terminatorCode, "/", matcher.NewTerminator('/', false))

var questionMarkMatcher = parsly.NewToken(questionMark, "?", matcher.NewByte('?'))
var colonMatcher = parsly.NewToken(colon, ":", matcher.NewByte(':'))
3 changes: 3 additions & 0 deletions service/workflow/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ func (s *Service) runNode(context *endly.Context, nodeType string, process *mode
context.Logging = original
}()
var state = context.State()
if strings.Contains(node.When, "Output") {
fmt.Printf("here")
}
canRun, err := criteria.Evaluate(context, context.State(), node.When, node.WhenEval(), fmt.Sprintf("%v.When", nodeType), true)
if err != nil || !canRun {
return err
Expand Down

0 comments on commit 7f85749

Please sign in to comment.