Skip to content

Commit

Permalink
feat: make can equals (==) or IN for type and service in delete asset…
Browse files Browse the repository at this point in the history
… expr
  • Loading branch information
Muhammad Luthfi Fahlevi committed Aug 13, 2024
1 parent e4903d0 commit b7afadc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions pkg/query_expr/delete_asset_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queryexpr

import (
"fmt"
"strings"

"github.com/goto/compass/core/asset"
generichelper "github.com/goto/compass/pkg/generic_helper"
Expand Down Expand Up @@ -29,11 +30,11 @@ func (d DeleteAssetExpr) Validate() error {
return fmt.Errorf("must exists these identifiers: refreshed_at, type, and service")
}

getOperator := func(jsonTag string) string {
return identifiersWithOperator[jsonTag]
isOperatorEqualsOrIn := func(jsonTag string) bool {
return identifiersWithOperator[jsonTag] == "==" || strings.ToUpper(identifiersWithOperator[jsonTag]) == "IN"
}
if getOperator("type") != "==" || getOperator("service") != "==" {
return fmt.Errorf("identifier type and service must be equals operator (==)")
if !isOperatorEqualsOrIn("type") || !isOperatorEqualsOrIn("service") {
return fmt.Errorf("identifier type and service must be equals (==) or IN operator")
}

identifiers := generichelper.GetMapKeys(identifiersWithOperator)
Expand Down
16 changes: 15 additions & 1 deletion pkg/query_expr/query_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queryexpr

import (
"fmt"
"strings"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
Expand Down Expand Up @@ -34,13 +35,26 @@ func ValidateAndGetQueryFromExpr(exprStr ExprStr) (string, error) {

// Visit is implementation Visitor interface from expr-lang/expr lib, used by ast.Walk
func (s *ExprVisitor) Visit(node *ast.Node) { //nolint:gocritic
if n, ok := (*node).(*ast.BinaryNode); ok {
switch n := (*node).(type) {
case *ast.BinaryNode:
if left, ok := (n.Left).(*ast.IdentifierNode); ok {
s.IdentifiersWithOperator[left.Value] = n.Operator
}
if right, ok := (n.Right).(*ast.IdentifierNode); ok {
s.IdentifiersWithOperator[right.Value] = n.Operator
}
case *ast.UnaryNode:
if binaryNode, ok := (n.Node).(*ast.BinaryNode); ok {
if strings.ToUpper(binaryNode.Operator) == "IN" {
notInOperator := "NOT IN"
if left, ok := (binaryNode.Left).(*ast.IdentifierNode); ok {
s.IdentifiersWithOperator[left.Value] = notInOperator
}
if right, ok := (binaryNode.Right).(*ast.IdentifierNode); ok {
s.IdentifiersWithOperator[right.Value] = notInOperator
}
}
}
}
}

Expand Down

0 comments on commit b7afadc

Please sign in to comment.